Skip to content

Commit 4ffd249

Browse files
author
Richard Hart
committed
Day 8
1 parent 14fa4d8 commit 4ffd249

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,52 @@
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+
canvas.width = window.innerWidth;
13+
canvas.height = window.innerHeight;
14+
ctx.strokeStyle = '#BADA55';
15+
ctx.lineJoin = 'round';
16+
ctx.lineCap = 'round';
17+
ctx.lineWidth = 100;
18+
let isDrawing = false;
19+
let lastX = 0;
20+
let lastY = 0;
21+
let hue = 0;
22+
let direction = true;
23+
24+
function draw(e) {
25+
if(!isDrawing) return;
26+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
27+
28+
ctx.beginPath();
29+
ctx.moveTo(lastX, lastY);
30+
ctx.lineTo(e.offsetX, e.offsetY);
31+
32+
ctx.stroke();
33+
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('mousemove', draw);
50+
canvas.addEventListener('mousedown', (e) => {
51+
isDrawing = true;
52+
[lastX, lastY] = [e.offsetX, e.offsetY];
53+
});
54+
canvas.addEventListener('mouseup', () => isDrawing = false);
55+
canvas.addEventListener('mouseout', () => isDrawing = false);
1056
</script>
1157

1258
<style>

0 commit comments

Comments
 (0)