-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
73 lines (64 loc) · 1.82 KB
/
index.html
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
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>Online Paint</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="welcome">
<h1>Welcome to the Online Paint!</h1>
<p>Choose your colour and stroke type and simply start drawing. Enjoy!</p>
</div>
<div class="controls">
<div class="color-picker">
<label for="color">Pick your colour:</label>
<input type="color" name="color" onchange="changeColor(this.value)">
</div>
<div class="width-change">
<label for="range">Choose line width:</label>
<input type="range" name="stroke-width" value="15" min="10" max="100" onchange="changeWidth(this.value)">
</div>
<button data-type="clear">clear</button>
</div>
<canvas id="draw"></canvas>
<script>
const canvas = document.querySelector('#draw');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth * 0.8;
canvas.height = window.innerHeight * 0.6;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.lineWidth = 15;
isDrawing = false;
let lastX = 0;
let lastY = 0;
let clearButton = document.querySelector('button');
function draw(e) {
if(!isDrawing) return;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
};
function clearCanva() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
};
function changeWidth(val) {
ctx.lineWidth = val;
}
function changeColor(val) {
ctx.strokeStyle = val;
}
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
});
canvas.addEventListener('mouseup', () => isDrawing = false);
canvas.addEventListener('mouseout', () => isDrawing = false)
clearButton.addEventListener('click', clearCanva);
</script>
</body>
</html>