Skip to content

Commit b339ed0

Browse files
committed
feat: add challenge wesbos#8
1 parent 94c764a commit b339ed0

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed
Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,79 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>HTML5 Canvas</title>
67
</head>
8+
79
<body>
8-
<canvas id="draw" width="800" height="800"></canvas>
9-
<script>
10-
</script>
10+
<canvas id="draw" width="800" height="800"></canvas>
11+
<script>
12+
const canvas = document.querySelector('#draw');
13+
const ctx = canvas.getContext('2d');
14+
15+
canvas.width = window.innerWidth;
16+
canvas.height = window.innerHeight;
17+
18+
ctx.strokeStyle = '#BADA55';
19+
ctx.lineJoin = 'round';
20+
ctx.lineCap = 'round';
21+
ctx.lineWidth = 100;
22+
ctx.globalCompositeOperation = 'multiply';
23+
24+
let isDrawing = false;
25+
let lastX = 0;
26+
let lastY = 0;
27+
28+
let hue = 0;
29+
let direction = true;
30+
31+
function draw(e) {
32+
if (!isDrawing) return;
33+
34+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
35+
ctx.beginPath();
36+
// start from
37+
ctx.moveTo(lastX, lastY);
38+
// go to
39+
ctx.lineTo(e.offsetX, e.offsetY);
40+
ctx.stroke();
41+
[lastX, lastY] = [e.offsetX, e.offsetY];
42+
hue++;
1143

12-
<style>
13-
html, body {
14-
margin:0;
15-
}
16-
</style>
44+
if(hue >= 360) {
45+
hue = 0;
46+
}
47+
48+
if(ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
49+
direction = !direction;
50+
}
51+
52+
if(direction) {
53+
ctx.lineWidth++;
54+
} else {
55+
ctx.lineWidth--;
56+
}
57+
}
58+
59+
canvas.addEventListener('mousedown', (e) => {
60+
isDrawing = true;
61+
[lastX, lastY] = [e.offsetX, e.offsetY]
62+
63+
});
64+
canvas.addEventListener('mousemove', draw);
65+
canvas.addEventListener('mouseup', () => isDrawing = false);
66+
canvas.addEventListener('mouseout', () => isDrawing = false);
67+
68+
</script>
69+
70+
<style>
71+
html,
72+
body {
73+
margin: 0;
74+
}
75+
</style>
1776

1877
</body>
19-
</html>
78+
79+
</html>

0 commit comments

Comments
 (0)