Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bfe0a0

Browse files
author
Alexander Besse
committed
wesbos#8 Completed."
1 parent a0bd9ae commit 7bfe0a0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

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

1258
<style>

0 commit comments

Comments
 (0)