Skip to content

Commit debdd36

Browse files
committed
Finished 08
1 parent 7c8496b commit debdd36

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,53 @@
1515
canvas.width = window.innerWidth;
1616
canvas.height = window.innerHeight;
1717
ctx.strokeStyle = '#BADA55';
18+
ctx.lineJoin = 'round';
1819
ctx.lineCap = 'round';
20+
ctx.lineWidth = 100;
21+
ctx.globalCompositeOperation = 'screen';
1922

2023
let isDrawing = false;
24+
let direction = true;
2125
let lastX = 0;
2226
let lastY = 0;
27+
let hue = 0;
2328

2429
function draw(e) {
25-
console.log('e', e);
30+
if (!isDrawing) {
31+
return;
32+
}
33+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
34+
ctx.beginPath();
35+
// start from:
36+
ctx.moveTo(lastX, lastY);
37+
// go to:
38+
ctx.lineTo(e.offsetX, e.offsetY);
39+
ctx.stroke();
40+
[lastX, lastY] = [e.offsetX, e.offsetY];
41+
if (hue > 360) hue = 0;
42+
hue += 7;
43+
44+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
45+
direction = !direction;
46+
}
47+
48+
if (direction) {
49+
ctx.lineWidth+= 3;
50+
} else {
51+
ctx.lineWidth-= 3;
52+
}
53+
2654
}
2755

56+
canvas.addEventListener('mousedown', (e) => {
57+
isDrawing = true
58+
console.log('*', e);
59+
[lastX, lastY] = [e.offsetX, e.offsetY];
60+
console.log('x, y', lastX, lastY);
61+
});
2862
canvas.addEventListener('mousemove', draw);
63+
canvas.addEventListener('mouseup', () => isDrawing = false);
64+
canvas.addEventListener('mouseout', () => isDrawing = false);
2965
</script>
3066

3167
<style>

0 commit comments

Comments
 (0)