-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
79 lines (67 loc) · 2.02 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
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas</title>
</head>
<style>
html , body {
margin:0;
}
</style>
<body>
<canvas id="draw" width="800" height="800"></canvas>
<script>
const canvas = document.querySelector('#draw');
const ctx = canvas.getContext('2d');
//setting the canvas in accordance with the size of screen
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.strokeStyle = '#BADA55';
ctx.lineJoin = 'round'; //shape
ctx.lineCap = 'round';
ctx.lineWidth = 100;
let isDrawing = false;
let lastX = 0; //defining the axis (x)
let lastY = 0;////defining the axis (y)
let hue = 0;
let direction = true;//introduced to change saturation and all
function draw(e) {
if (!isDrawing) return;
console.log(e);
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
ctx.beginPath(); //starting point
ctx.moveTo(lastX, lastY); //till
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
hue++; //changing the hue value==changing the color!!
if (hue >= 360) {
hue = 0; //bringing back the hue value to the starting value
}
//to keep the linewidth in control.
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
direction = !direction;
}
if(direction) {
ctx.lineWidth++;
} else {
ctx.lineWidth--;
}
}
//setting origin point to the current axis where the mousedown event takes place
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
});
//calling the draw function when the mousemove event trigers
canvas.addEventListener('mousemove', draw);
//setting isdrawing to false so that the draw function returns null.
canvas.addEventListener('mouseup', () => isDrawing = false);
canvas.addEventListener('mouseout', () => isDrawing = false);
(function blah() {
alert("Hello\nThanks for showing up!\n The empty canvas is all yours!\nStart painting by pressing the mousedown!\n-Dhrubesh.");
})()
</script>
</body>
</html>