Visitar URL original
Finish Day 8 · wesbos/JavaScript30@b21627c · GitHub
Skip to content

Commit b21627c

Browse files
committed
Finish Day 8
1 parent 3636530 commit b21627c

3 files changed

Lines changed: 68 additions & 92 deletions

File tree

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

Lines changed: 0 additions & 73 deletions
This file was deleted.

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>HTML5 Canvas</title>
6+
</head>
7+
<body>
8+
<canvas id="draw" width="800" height="800"></canvas>
9+
<script>
10+
const canvas = document.querySelector('#draw');
11+
const ctx = canvas.getContext('2d');
12+
13+
canvas.height = window.innerHeight;
14+
canvas.width = window.innerWidth;
15+
16+
ctx.strokeStyle = '#BADASS';
17+
ctx.lineJoin = 'round';
18+
ctx.lineCap = 'round';
19+
ctx.lineWidth = 60;
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+
hue++;
36+
37+
if( hue >= 360) {
38+
hue = 0;
39+
}
40+
41+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
42+
direction = !direction;
43+
}
44+
if (direction) {
45+
ctx.lineWidth++;
46+
} else {
47+
ctx.lineWidth--;
48+
}
49+
}
50+
51+
canvas.addEventListener('mousedown', (event) => {
52+
isDrawing = true;
53+
[lastX, lastY] = [event.offsetX, event.offsetY];
54+
});
55+
56+
canvas.addEventListener('mousemove', draw);
57+
canvas.addEventListener('mouseup', () => isDrawing = false);
58+
canvas.addEventListener('mouseout', () => isDrawing = false);
59+
</script>
60+
61+
<style>
62+
html, body {
63+
margin:0;
64+
}
65+
</style>
66+
67+
</body>
68+
</html>

0 commit comments

Comments
 (0)