Skip to content

Commit 8f1ceac

Browse files
committed
code along wesbos#8 fun with html canvas
1 parent 9f731af commit 8f1ceac

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,54 @@
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+
19+
let isDrawing = false;
20+
let lastX = 0;
21+
let lastY = 0;
22+
let hue = 0;
23+
let direction = true;
24+
25+
function draw(e) {
26+
if(!isDrawing) return;
27+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
28+
ctx.beginPath();
29+
ctx.moveTo(lastX, lastY);
30+
ctx.lineTo(e.offsetX, e.offsetY);
31+
ctx.stroke();
32+
[lastX, lastY] = [e.offsetX, e.offsetY];
33+
34+
hue++;
35+
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+
54+
canvas.addEventListener('mousemove', draw);
55+
canvas.addEventListener('mouseup', () => isDrawing = false);
56+
canvas.addEventListener('mouseout', () => isDrawing = false);
57+
1058
</script>
1159

1260
<style>

0 commit comments

Comments
 (0)