-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (58 loc) · 1.69 KB
/
script.js
File metadata and controls
72 lines (58 loc) · 1.69 KB
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
window.addEventListener("load", () => {
document.querySelector("canvas");
var c = canvas.getContext("2d");
let button = document.getElementById("clear");
let fill = document.getElementById("fill");
let slider = document.getElementById("myRange");
let brushSize = 10; //Changes the size of the brush
let brushColor = "black"; //Changes the color of the brush
// Resizing the Canvas
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
var theInput = document.getElementById("favcolor");
theInput.addEventListener(
"input",
function () {
brushColor = theInput.value;
},
false
);
//Event Listeners
canvas.addEventListener("mousedown", startDrawing);
canvas.addEventListener("mouseup", stopDrawing);
canvas.addEventListener("mousemove", draw);
let drawing = false;
function startDrawing() {
drawing = true;
c.lineWidth = brushSize;
c.lineCap = "round";
c.strokeStyle = brushColor;
c.lineTo(e.clientX, e.clientY);
c.stroke();
}
function stopDrawing() {
drawing = false;
c.beginPath();
}
function draw(e) {
if (!drawing) return;
c.lineWidth = brushSize;
c.lineCap = "round";
c.strokeStyle = brushColor;
c.lineTo(e.clientX, e.clientY);
c.stroke();
//console.log(e.clientX, e.clientY);
}
//Clear Screen Button
button.onclick = function () {
c.clearRect(0, 0, innerWidth, innerHeight);
};
fill.onclick = function () {
c.fillStyle = brushColor;
c.fillRect(0, 0, innerWidth, innerHeight);
};
//Slider for BrushSize
slider.oninput = function () {
brushSize = slider.value;
};
});