Skip to content

Commit 78ce90a

Browse files
authored
Update index-FINISHED.html
1 parent 3345dfa commit 78ce90a

File tree

1 file changed

+54
-46
lines changed

1 file changed

+54
-46
lines changed

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

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,69 @@
55
<title>HTML5 Canvas</title>
66
</head>
77
<body>
8-
<canvas id="draw" width="800" height="800"></canvas>
8+
<canvas id="js-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';
10+
11+
const canvas = document.querySelector('#js-draw');
12+
const ctx = canvas.getContext('2d'); // have to note if the artwork context is 2d or 3d
13+
canvas.width = window.innerWidth; // fill viewport width
14+
canvas.height = window.innerHeight; // fill viewport height
15+
ctx.strokeStyle = '#BADA55'; // default color
16+
ctx.lineJoin = 'round'; // canvas stroke-linejoin options: mitre | round | bevel | inherit
17+
ctx.lineCap = 'round'; // canvas stroke-linecap options: butt | round | square | inherit
18+
ctx.lineWidth = 100; // brush size
19+
// ctx.globalCompositeOperation = 'multiply';
1920

20-
let isDrawing = false;
21-
let lastX = 0;
22-
let lastY = 0;
23-
let hue = 0;
24-
let direction = true;
21+
let isDrawing = false;
22+
let lastX = 0;
23+
let lastY = 0;
24+
let hue = 0;
25+
let direction = true;
2526

26-
function draw(e) {
27-
if (!isDrawing) return; // stop the fn from running when they are not moused down
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];
27+
function draw(e) {
28+
if (!isDrawing) return; // stop the fn from running when they are not moused down
29+
console.log(e);
30+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
31+
ctx.beginPath();
32+
// start from
33+
ctx.moveTo(lastX, lastY);
34+
// go to
35+
ctx.lineTo(e.offsetX, e.offsetY);
36+
ctx.stroke();
37+
// destructuring an array, values of lastX, lastY are set in a single line
38+
[lastX, lastY] = [e.offsetX, e.offsetY]; // where the mouse ended up when it stopped moving
3739

38-
hue++;
39-
if (hue >= 360) {
40-
hue = 0;
41-
}
42-
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
43-
direction = !direction;
44-
}
40+
hue++; // starts at 0 and increments to orange, yellow, green, blue, indigo, violet, back to red
4541

46-
if(direction) {
47-
ctx.lineWidth++;
48-
} else {
49-
ctx.lineWidth--;
50-
}
42+
// hue goes from red (0) to red (360) across the rainbow as the mouse is held down
43+
if (hue >= 360) {
44+
hue = 0;
45+
}
5146

52-
}
47+
// Increment 0 to 100, then 100 down to 0
48+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
49+
direction = !direction;
50+
}
51+
52+
// make the line width thicker or thinner as the mouse is held down
53+
if(direction) {
54+
ctx.lineWidth++;
55+
} else {
56+
ctx.lineWidth--;
57+
}
58+
59+
}
5360

54-
canvas.addEventListener('mousedown', (e) => {
55-
isDrawing = true;
56-
[lastX, lastY] = [e.offsetX, e.offsetY];
57-
});
61+
// before mousemove mousedown fires, so track lastX, lastY
62+
canvas.addEventListener('mousedown', (e) => {
63+
isDrawing = true;
64+
[lastX, lastY] = [e.offsetX, e.offsetY];
65+
});
5866

5967

60-
canvas.addEventListener('mousemove', draw);
61-
canvas.addEventListener('mouseup', () => isDrawing = false);
62-
canvas.addEventListener('mouseout', () => isDrawing = false);
68+
canvas.addEventListener('mousemove', draw);
69+
canvas.addEventListener('mouseup', () => isDrawing = false);
70+
canvas.addEventListener('mouseout', () => isDrawing = false);
6371

6472
</script>
6573

0 commit comments

Comments
 (0)