Skip to content

Commit fe99156

Browse files
committed
drawing on canvas
1 parent 9320407 commit fe99156

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@
88
<body>
99
<canvas id="draw" width="800" height="800"></canvas>
1010
<script>
11+
const canvas = document.querySelector('#draw');
12+
const ctx = canvas.getContext('2d');
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+
20+
let isDrawing = false;
21+
let lastX = 0;
22+
let lastY = 0;
23+
24+
function draw(e){
25+
if(!isDrawing) return;
26+
console.log(e);
27+
ctx.beginPath();
28+
ctx.moveTo(lastX, lastY);
29+
ctx.lineTo(e.offsetX, e.offsetY);
30+
ctx.stroke();
31+
[lastX, lastY] = [e.offsetX, e.offsetY];
32+
}
33+
34+
canvas.addEventListener('mousemove', draw);
35+
canvas.addEventListener('mousedown', (e) => {
36+
isDrawing = true;
37+
[lastX, lastY] = [e.offsetX, e.offsetY];
38+
});
39+
canvas.addEventListener('mouseup', () => isDrawing = false);
40+
canvas.addEventListener('mouseout', () => isDrawing = false);
1141
</script>
1242

1343
<style>

0 commit comments

Comments
 (0)