Skip to content
This repository was archived by the owner on Feb 13, 2022. It is now read-only.

Commit e1fea60

Browse files
committed
solution wesbos#8
1 parent bbc36e7 commit e1fea60

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

08 - Fun with HTML5 Canvas/index-START.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,51 @@
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
99
<script>
10+
const canvas = document.querySelector('#draw');
11+
const ctx = canvas.getContext('2d');
12+
13+
canvas.width = window.innerWidth;
14+
canvas.height = window.innerHeight;
15+
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 = true;
25+
26+
function draw(evt) {
27+
if (!isDrawing) return;
28+
29+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
30+
31+
ctx.beginPath();
32+
ctx.moveTo(lastX, lastY);
33+
ctx.lineTo(evt.offsetX, evt.offsetY);
34+
ctx.stroke();
35+
36+
[lastX, lastY] = [evt.offsetX, evt.offsetY];
37+
38+
hue++;
39+
if (hue >= 360) hue = 0;
40+
41+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) direction = !direction;
42+
43+
if (direction) ctx.lineWidth++;
44+
else ctx.lineWidth--;
45+
}
46+
47+
addEventListener('mousemove', draw);
48+
addEventListener('mousedown', (evt) => {
49+
isDrawing = true;
50+
[lastX, lastY] = [evt.offsetX, evt.offsetY];
51+
});
52+
53+
addEventListener('mouseup', () => isDrawing = false);
54+
addEventListener('mouseout', () => isDrawing = false);
1055
</script>
1156

1257
<style>

0 commit comments

Comments
 (0)