Skip to content

Commit 99ca957

Browse files
committed
Finished wesbos#8
1 parent 25dcae9 commit 99ca957

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,67 @@
66
</head>
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
9+
910
<script>
11+
const canvas = document.querySelector('#draw');
12+
const ctx = canvas.getContext('2d');
13+
canvas.width = window.innerWidth;
14+
canvas.height = window.innerHeight;
15+
16+
ctx.strokeStyle = '#BADA55';
17+
ctx.lineJoin = 'round';
18+
ctx.lineCap = 'round';
19+
ctx.lineWidth = 11;
20+
// ctx.globalCompositeOperation = 'multiply';
21+
22+
let isDrawing = false;
23+
let lastX = 0;
24+
let lastY = 0;
25+
26+
// starting color (red)
27+
let hue = 0;
28+
let direction = true;
29+
30+
function draw(e) {
31+
if (!isDrawing) return;
32+
// console.log(e);
33+
34+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
35+
// ctx.lineWidth = hue;
36+
37+
ctx.beginPath();
38+
ctx.moveTo(lastX, lastY);
39+
ctx.lineTo(e.offsetX, e.offsetY);
40+
ctx.stroke();
41+
42+
// lastX = e.offsetX;
43+
// lastY = e.offsetY;
44+
[lastX, lastY] = [e.offsetX, e.offsetY];
45+
46+
// increment values
47+
hue++;
48+
if (hue >= 360) {
49+
hue = 0;
50+
}
51+
52+
if (ctx.lineWidth >= 60 || ctx.lineWidth <= 10) {
53+
direction = !direction;
54+
}
55+
if (direction) {
56+
ctx.lineWidth++;
57+
} else {
58+
ctx.lineWidth--;
59+
}
60+
}
61+
62+
canvas.addEventListener('mousedown', (e) => {
63+
isDrawing = true;
64+
[lastX, lastY] = [e.offsetX, e.offsetY];
65+
});
66+
67+
canvas.addEventListener('mousemove', draw);
68+
canvas.addEventListener('mouseup', () => isDrawing = false);
69+
canvas.addEventListener('mouseout', () => isDrawing = false);
1070
</script>
1171

1272
<style>

0 commit comments

Comments
 (0)