-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
98 lines (61 loc) · 2.14 KB
/
Copy pathindex.html
File metadata and controls
98 lines (61 loc) · 2.14 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Canvas Draw</title>
<link rel="stylesheet" href="/css/styles.css" />
</head>
<body>
<!-- Heading Canvas Draw App -->
<h1>Canvas Drawing App</h1>
<div id="controls">
<!-- Label for the color picker -->
<label for="colorpicker">Color:</label>
<input type="color" value="#000000" id="colorpicker" />
<!-- Brush -->
<label for="brush"> Brush Size:</label>
<input type="range" min="1" max="50" value="5" id="brushSize" />
<!-- Button -->
<button id="clearBtn">Clear Canvas</button>
</div>
<!-- Canvas -->
<canvas id="canvas" width="600" height="300"> </canvas>
<!-- Internal JavaScript -->
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const colorPicker = document.getElementById("colorpicker");
const brushSize = document.getElementById("brushSize");
const clearBtn = document.getElementById("clearBtn");
let painting = false;
// Draw dot where a user should start from
function startPosition(e) {
painting = true;
draw(e);
}
// Stop drawing
function endPosition() {
painting = false;
ctx.beginPath();
}
// Draw
function draw(e) {
if (!painting) return;
ctx.lineWidth = brushSize.value;
ctx.lineCap = "round"; // It makes the line caps rounded
ctx.strokeStyle = colorPicker.value;
ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
}
canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', endPosition);
canvas.addEventListener('mousemove', draw);
clearBtn.addEventListener('click', ()=>{
ctx.clearRect(5, 5, canvas.width, canvas.height);
});
</script>
</body>
</html>