Skip to content

Commit c8d8fea

Browse files
committed
Exercise 8
1 parent 6ed4674 commit c8d8fea

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,61 @@
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+
// ctx.globalCompositeOperation = 'multiply';
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(e) {
27+
if(!isDrawing) return;
28+
// console.log(e);
29+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
30+
ctx.beginPath();
31+
// start from
32+
ctx.moveTo(lastX, lastY);
33+
//go to
34+
ctx.lineTo(e.offsetX, e.offsetY);
35+
ctx.stroke();
36+
[lastX, lastY] = [e.offsetX, e.offsetY];
37+
38+
hue++;
39+
if(hue >= 360) {
40+
hue = 0;
41+
}
42+
43+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
44+
direction = !direction;
45+
}
46+
47+
if (direction) {
48+
ctx.lineWidth++;
49+
} else {
50+
ctx.lineWidth--;
51+
}
52+
53+
}
54+
55+
56+
canvas.addEventListener('mousedown', (e) => {
57+
isDrawing = true;
58+
[lastX, lastY] = [e.offsetX, e.offsetY];
59+
});
60+
61+
canvas.addEventListener('mousemove', draw);
62+
canvas.addEventListener('mouseup', () => isDrawing = false);
63+
canvas.addEventListener('mouseout', () => isDrawing = false);
64+
1065
</script>
1166

1267
<style>

0 commit comments

Comments
 (0)