-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
41 lines (33 loc) · 813 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
canvas.width = 550;
canvas.height = 550;
ctx.strokeStyle = "#2c2c2c";
ctx.lineWidth = 2.5;
let painting = false;
function stopPainting(){
painting = false;
}
function startPainting(){
painting = true;
}
function onMouseMove(event){
const x = event.offsetX;
const y = event.offsetY;
if(!painting){
ctx.beginPath();
ctx.moveTo(x,y);
} else{
ctx.lineTo(x,y);
ctx.stroke();
}
}
function onMouseDown(event){
painting=true;
}
if(canvas){
canvas.addEventListener("mousemove",onMouseMove);
canvas.addEventListener("mousedown",startPainting);
canvas.addEventListener("mouseup",stopPainting);
canvas.addEventListener("mouseleave",stopPainting);
}