Visitar URL original
Merge pull request #8 from cydseelen/canvas · wesbos/JavaScript30@b8ba033 · GitHub
Skip to content

Commit b8ba033

Browse files
authored
Merge pull request #8 from cydseelen/canvas
Completed challenge, canvas works the way it should.
2 parents 4148545 + de5cf47 commit b8ba033

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

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

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

1261
<style>

0 commit comments

Comments
 (0)