Skip to content

Commit 45f960c

Browse files
committed
Completed Day #8
1 parent 91ec4f1 commit 45f960c

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>HTML5 Canvas</title>
6+
</head>
7+
<body>
8+
<canvas id="draw" width="800" height="800"></canvas>
9+
<script>
10+
const canvas = document.querySelector('#draw');
11+
const ctx = canvas.getContext('2d');
12+
canvas.width = window.innerWidth;
13+
canvas.height = window.innerHeight;
14+
15+
ctx.strokeStyle = '#BADA55';
16+
ctx.lineJoin = 'round';
17+
ctx.lineCap = 'round';
18+
ctx.lineWidth = 100;
19+
// ctx.globalCompositeOperation = 'multiply';
20+
21+
let isDrawing = false;
22+
let lastX = 0;
23+
let lastY = 0;
24+
let hue = 0;
25+
let direction = true;
26+
27+
const draw = event => {
28+
if(!isDrawing) return;
29+
30+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
31+
ctx.beginPath();
32+
ctx.moveTo(lastX, lastY);
33+
ctx.lineTo(event.offsetX, event.offsetY);
34+
ctx.stroke();
35+
[lastX, lastY] = [event.offsetX, event.offsetY];
36+
hue = hue++ >= 360 ? 0 : hue++;
37+
38+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
39+
direction = !direction;
40+
}
41+
42+
if(direction) {
43+
ctx.lineWidth++;
44+
} else {
45+
ctx.lineWidth--;
46+
}
47+
}
48+
49+
canvas.addEventListener('mousemove', draw);
50+
canvas.addEventListener('mousedown', event => {
51+
isDrawing = true;
52+
[lastX, lastY] = [event.offsetX, event.offsetY];
53+
});
54+
canvas.addEventListener('mouseup', () => isDrawing = false);
55+
canvas.addEventListener('mouseout', () => isDrawing = false);
56+
57+
58+
</script>
59+
60+
<style>
61+
html, body {
62+
margin:0;
63+
}
64+
</style>
65+
66+
</body>
67+
</html>

0 commit comments

Comments
 (0)