Skip to content

Commit a96b39b

Browse files
author
=
committed
JS30 wesbos#8 completed
1 parent 1d15162 commit a96b39b

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed
Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,67 @@
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+
canvas.width = window.innerWidth;
15+
canvas.height = window.innerHeight;
16+
ctx.strokeStyle = '#BADA55';
17+
ctx.lineJoin = 'round';
18+
ctx.lineCap = 'round';
19+
20+
let isDrawing = false;
21+
let lastX = 0;
22+
let lastY = 0;
23+
let hue = 0;
24+
let direction = false;
1125

12-
<style>
13-
html, body {
14-
margin: 0;
15-
}
16-
</style>
26+
function draw(e) {
27+
if (!isDrawing) return;
28+
console.log(e);
29+
ctx.strokeStyle = `hsl(${hue},100%,50%)`;
30+
ctx.beginPath();
31+
ctx.moveTo(lastX,lastY);
32+
ctx.lineTo(e.offsetX,e.offsetY);
33+
ctx.stroke();
34+
[lastX,lastY] = [e.offsetX,e.offsetY];
35+
hue++;
36+
if(hue >= 360) {
37+
hue = 0;
38+
}
39+
if(ctx.lineWidth>=100 || ctx.lineWidth<=1) {
40+
direction = !direction;
41+
}
42+
if(direction) {
43+
ctx.lineWidth++;
44+
} else {
45+
ctx.lineWidth--;
46+
}
47+
}
48+
49+
canvas.addEventListener('mousedown', (e) => {
50+
isDrawing = true;
51+
[lastX,lastY] = [e.offsetX,e.offsetY];
52+
})
53+
canvas.addEventListener('mousemove', draw);
54+
canvas.addEventListener('mouseup', () => isDrawing = false)
55+
canvas.addEventListener('mouseout', () => isDrawing = false)
56+
</script>
57+
58+
<style>
59+
html,
60+
body {
61+
margin: 0;
62+
}
63+
</style>
1764

1865
</body>
19-
</html>
66+
67+
</html>

0 commit comments

Comments
 (0)