-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
69 lines (55 loc) · 1.78 KB
/
canvas.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
var color = document.getElementById("colorChoice").value;
var colorPicker = document.getElementById("colorChoice");
colorPicker.addEventListener("input", changeColors);
function changeColors() {
color = document.getElementById("colorChoice").value;
}
var eraser = document.getElementById("eraser");
eraser.addEventListener("click", erase);
function erase() {
color = "#FFFFFF";
}
window.addEventListener("load", () => {
const drawingCanvas = document.querySelector("#can");
const canvas = drawingCanvas.getContext("2d");
drawingCanvas.height = window.innerHeight*0.8;
drawingCanvas.width = window.innerWidth;
canvas.fillStyle="#FFFFFF";
canvas.fillRect(0, 0, drawingCanvas.width, drawingCanvas.height);
var painting = false;
drawingCanvas.addEventListener("mousedown", start);
drawingCanvas.addEventListener("mouseup", end);
drawingCanvas.addEventListener("mousemove", draw);
function start() {
canvas.beginPath();
painting = true;
}
function end() {
painting = false;
}
function draw(e) {
if (!painting) return;
canvas.lineCap="round";
canvas.strokeStyle=color;
canvas.lineTo(e.clientX, e.clientY-80);
canvas.stroke();
canvas.beginPath();
canvas.moveTo(e.clientX, e.clientY-80);
}
// for styling
var increase = document.getElementById("increaseSize");
increase.addEventListener("click", increaseWidth);
var decrease = document.getElementById("decreaseSize");
decrease.addEventListener("click", decreaseWidth);
function increaseWidth() {
canvas.lineWidth++;
}
function decreaseWidth() {
canvas.lineWidth--;
}
var clear = document.getElementById("clearScreen");
clear.addEventListener("click", clearS);
function clearS() {
canvas.fillRect(0, 0, drawingCanvas.width, drawingCanvas.height);
}
});