-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (63 loc) · 1.66 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var canvas = document.querySelector("canvas");
var clear = document.querySelector("#clear");
var red = document.querySelector("#red");
var green = document.querySelector("#green");
var blue = document.querySelector("#blue");
var yellow = document.querySelector("#yellow");
var pink = document.querySelector("#pink");
var black = document.querySelector("#black");
var ctx = canvas.getContext("2d");
var drawing = false;
var oldX = 0;
var oldY = 0;
red.addEventListener("click", () => {
ctx.strokeStyle = "red";
ctx.beginPath();
})
green.addEventListener("click", () => {
ctx.strokeStyle = "green";
ctx.beginPath();
})
blue.addEventListener("click", () => {
ctx.strokeStyle = "blue";
ctx.beginPath();
})
yellow.addEventListener("click", () => {
ctx.strokeStyle = "yellow";
ctx.beginPath();
})
pink.addEventListener("click", () => {
ctx.strokeStyle = "pink";
ctx.beginPath();
})
black.addEventListener("click", () => {
ctx.strokeStyle = "black";
ctx.beginPath();
})
canvas.style.cursor = "crosshair";
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
canvas.addEventListener("mousedown", e => {
drawing = true;
oldX = e.offsetX;
oldY = e.offsetY;
});
canvas.addEventListener("mouseup", e => {
drawing = false;
});
canvas.addEventListener("mousemove", e => {
if (!drawing){
return false;
}
ctx.lineWidth = 5;
ctx.lineCap = "round";
ctx.moveTo(oldX, oldY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
oldX = e.offsetX;
oldY = e.offsetY;
});
clear.addEventListener ("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
})