Skip to content

Commit 7cc3b47

Browse files
committed
added solution for wesbos#8
1 parent 97d6ae5 commit 7cc3b47

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,49 @@
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
99
<script>
10+
11+
const canvas = document.querySelector('#draw');
12+
const context = canvas.getContext('2d');
13+
canvas.width = window.innerWidth;
14+
canvas.height = window.innerHeight;
15+
16+
context.strokeStyle = '#BADA55';
17+
context.lineJoin = 'round';
18+
context.lineCap = 'round';
19+
context.lineWidth = 5;
20+
21+
let isDrawing = false;
22+
let lastX = 0;
23+
let lastY = 0;
24+
let hue = 0;
25+
26+
function draw(e){
27+
if (!isDrawing) return; //stop function from running when mouse is not pressed down
28+
console.log(e);
29+
context.strokeStyle = `hsl(${hue},100%,50%)`;
30+
31+
context.beginPath();
32+
//start from
33+
context.moveTo(lastX,lastY);
34+
//go to
35+
context.lineTo(e.offsetX, e.offsetY);
36+
context.stroke();
37+
38+
lastX = e.offsetX;
39+
lastY = e.offsetY;
40+
41+
hue ++;
42+
43+
44+
}
45+
46+
canvas.addEventListener('mousemove', draw );
47+
canvas.addEventListener('mousedown',function(e){
48+
isDrawing = true;
49+
lastX = e.offsetX;
50+
lastY = e.offsetY;});
51+
canvas.addEventListener('mouseup', function(){isDrawing = false});
52+
canvas.addEventListener('mouseout', function(){isDrawing = false});
1053
</script>
1154

1255
<style>

0 commit comments

Comments
 (0)