Visitar URL original
8th exercise done · wesbos/JavaScript30@847cff9 · GitHub
Skip to content

Commit 847cff9

Browse files
committed
8th exercise done
1 parent c96fd7b commit 847cff9

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,55 @@
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+
ctx.strokeStyle = '#BADA55';
16+
ctx.lineJoin = 'round';
17+
ctx.lineCap = 'round';
18+
ctx.lineWidth = 100;
19+
ctx.globalCompositeOperation = 'luminosity';
20+
21+
let isDrawing = false;
22+
let lastX = 0;
23+
let lastY = 0;
24+
let hue = 0;
25+
let direction = true;
26+
27+
function draw(e) {
28+
if (!isDrawing) return;
29+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
30+
ctx.beginPath();
31+
ctx.moveTo(lastX, lastY);
32+
ctx.lineTo(e.offsetX, e.offsetY);
33+
ctx.stroke();
34+
[lastX, lastY] = [e.offsetX, e.offsetY];
35+
36+
hue++;
37+
if(hue >= 360) {
38+
hue = 0;
39+
}
40+
41+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
42+
direction = !direction;
43+
}
44+
45+
if (direction) {
46+
ctx.lineWidth++;
47+
} else {
48+
ctx.lineWidth--;
49+
}
50+
}
51+
52+
canvas.addEventListener('mousemove', draw);
53+
canvas.addEventListener('mousedown', (e) => {
54+
isDrawing = true;
55+
[lastX, lastY] = [e.offsetX, e.offsetY];
56+
});
57+
canvas.addEventListener('mouseup', () => isDrawing = false);
58+
canvas.addEventListener('mouseout', () => isDrawing = false);
1059
</script>
1160

1261
<style>

0 commit comments

Comments
 (0)