Skip to content

Commit 830a613

Browse files
authored
Merge pull request #4 from benschac/08
finish 08
2 parents aaf3d3c + 95047a8 commit 830a613

2 files changed

Lines changed: 80 additions & 19 deletions

File tree

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
let hue = 0;
13+
canvas.width = window.innerWidth;
14+
canvas.height = window.innerHeight;
15+
16+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
17+
ctx.lineJoin = 'round';
18+
ctx.lineCap = 'round';
19+
ctx.lineWidth = 100;
20+
21+
let isDrawing = false;
22+
let lastX = 0;
23+
let lastY = 0;
24+
25+
function draw(e) {
26+
if (!isDrawing) return; // stop the fn from running when they are not moused down
27+
console.log(e);
28+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
29+
ctx.beginPath();
30+
// start from
31+
ctx.moveTo(lastX, lastY);
32+
// go to
33+
ctx.lineTo(e.offsetX, e.offsetY);
34+
ctx.stroke();
35+
[lastX, lastY] = [e.offsetX, e.offsetY];
36+
37+
hue++;
38+
// if (hue >= 360) {
39+
// hue = 0;
40+
// }
41+
// if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
42+
// direction = !direction;
43+
// }
44+
//
45+
// if(direction) {
46+
// ctx.lineWidth++;
47+
// } else {
48+
// ctx.lineWidth--;
49+
// }
50+
51+
}
52+
53+
54+
55+
canvas.addEventListener('mousedown', (e) => {
56+
isDrawing = true
57+
[lastX, lastY] = [e.offsetX, e.offsetY];
58+
59+
});
60+
61+
canvas.addEventListener('mousedown', (e) => {
62+
isDrawing = true;
63+
[lastX, lastY] = [e.offsetX, e.offsetY];
64+
});
65+
66+
67+
canvas.addEventListener('mousemove', draw);
68+
canvas.addEventListener('mouseup', () => isDrawing = false);
69+
canvas.addEventListener('mouseout', () => isDrawing = false);
70+
</script>
71+
72+
<style>
73+
body,
74+
html {
75+
margin: 0;
76+
}
77+
78+
</style>
79+
</body>
80+
</html>

0 commit comments

Comments
 (0)