Skip to content

Commit 0456723

Browse files
committed
eight complete
1 parent 2b4b9ab commit 0456723

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,71 @@
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+
//resize canvas
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 = 'multiply';
20+
//this would be blending the colors on top of each other
21+
22+
let isDrawing = false;
23+
let lastX = 0;
24+
let lastY = 0;
25+
let hue = 0;
26+
let direction = true;
27+
//building up ^
28+
29+
function draw(e){
30+
if (!isDrawing) return;
31+
//function won't run if mouse is not down
32+
console.log(e);
33+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
34+
// ctx.lineWidth = hue;
35+
//ctx is where you do all the drawing for the canvas
36+
ctx.beginPath();
37+
//starting from
38+
ctx.moveTo(lastX, lastY);
39+
//going to
40+
ctx.lineTo(e.offsetX, e.offsetY);
41+
ctx.stroke();
42+
// lastX = e.offsetX;
43+
// lastY = e.offsetY;
44+
//or
45+
46+
//es6
47+
[lastX, lastY] = [e.offsetX, e.offsetY];
48+
//destructuring an array
49+
hue++;
50+
//if hsl goes over 360, it will keep going
51+
if (hue >= 360){
52+
hue = 0
53+
}
54+
//resetting it
55+
56+
if(ctx.lineWidth >= 100 || ctx.lineWidth <= 1){
57+
direction = !direction;
58+
}
59+
if (direction){
60+
ctx.lineWidth++;
61+
} else {
62+
ctx.lineWidth--;
63+
}
64+
}
65+
66+
canvas.addEventListener('mousedown', (e) => {
67+
isDrawing = true;
68+
[lastX, lastY] = [e.offsetX, e.offsetY];
69+
});
70+
canvas.addEventListener('mousemove', draw);
71+
canvas.addEventListener('mouseup', () =>
72+
isDrawing = false);
73+
canvas.addEventListener('mouseout', () =>
74+
isDrawing = false);
1075
</script>
1176

1277
<style>

0 commit comments

Comments
 (0)