Skip to content

Commit b87ba06

Browse files
Show shape while drawing
1 parent b7f8c0a commit b87ba06

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

23 - Drawing Pad/script.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const line = document.querySelector("#line");
99
const rect = document.querySelector("#rect");
1010
const circle = document.querySelector("#circle");
1111

12+
let dataURL;
13+
1214
function setup() {
1315
// create a canvas which is full width and height
1416
createCanvas(window.innerWidth, window.innerHeight);
1517
// Add a white background to the canvas
1618
background(255);
19+
dataURL = canvas.toDataURL();
1720
}
1821

1922
function draw() {
@@ -59,6 +62,7 @@ clear.addEventListener('click', () => {
5962
paths.splice(0);
6063
// Clear the background
6164
background(255);
65+
dataURL = canvas.toDataURL();
6266
});
6367

6468

@@ -67,6 +71,7 @@ window.onload = () => {
6771
var ctx = c.getContext("2d");
6872
let x1,y1,x2,y2,x3,y3;
6973
let gradient;
74+
let isDown = false;
7075
const color2 = document.querySelector("#color2");
7176

7277
document.querySelector("#gradient").addEventListener('click', () => {
@@ -81,8 +86,24 @@ window.onload = () => {
8186
document.querySelector("#defaultCanvas0").addEventListener("mousedown", (e) => {
8287
x1 = e.clientX;
8388
y1 = e.clientY - 42;
89+
isDown = true
90+
});
91+
document.querySelector("#defaultCanvas0").addEventListener("mousemove", (e) => {
92+
if(isDown){
93+
const image = new Image();
94+
image.src = dataURL;
95+
image.addEventListener("load", () => {
96+
ctx.drawImage(image, 0, 0, window.innerWidth, window.innerHeight);
97+
});
98+
drawing(e);
99+
}
84100
});
85101
document.querySelector("#defaultCanvas0").addEventListener("mouseup", (e) => {
102+
isDown = false;
103+
drawing(e);
104+
dataURL = canvas.toDataURL();
105+
});
106+
function drawing(e){
86107
if(line.checked){
87108
x2 = e.clientX;
88109
y2 = e.clientY - 42;
@@ -139,5 +160,5 @@ window.onload = () => {
139160
}
140161
ctx.fill();
141162
}
142-
});
163+
}
143164
}

0 commit comments

Comments
 (0)