Skip to content

Commit 290e570

Browse files
author
Tkum
committed
canvas
1 parent 4fdb65f commit 290e570

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,68 @@
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
99
<script>
10-
</script>
10+
const canvas = document.getElementById('draw');
11+
canvas.width = window.innerWidth;
12+
canvas.height = window.innerHeight;
13+
14+
const ctx = canvas.getContext('2d');
15+
ctx.strokeStyle = 'red';
16+
ctx.lineJoin = 'round';
17+
ctx.lineCap = 'round';
18+
ctx.lineWidth = 1;
19+
20+
let isDrawing = false;
21+
let hue = 0;
22+
let direction = 1;
23+
let maxSize = 30;
24+
25+
canvas.addEventListener('mouseout', (ev) => {
26+
isDrawing = false;
27+
ctx.lineWidth = 1;
28+
hue = 0;
29+
});
30+
canvas.addEventListener('mousedown', (ev) => {
31+
isDrawing = true;
32+
[lastX, lastY] = [ev.offsetX,ev.offsetY];
33+
} );
34+
canvas.addEventListener('mouseup', (ev) => {
35+
isDrawing = false;
36+
ctx.lineWidth = 1;
37+
hue = 0;
38+
});
39+
canvas.addEventListener('mousemove', draw);
40+
41+
let lastX = 0;
42+
let lastY = 0;
1143

44+
function draw(ev){
45+
if (!isDrawing){
46+
return;
47+
}
48+
else{
49+
if ((ctx.lineWidth > maxSize) || (ctx.lineWidth < 2)){
50+
direction = -direction;
51+
}
52+
53+
ctx.lineWidth+=direction;
54+
hue++;
55+
console.log(ctx.lineWidth);
56+
ctx.beginPath();
57+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
58+
ctx.moveTo(lastX,lastY);
59+
ctx.lineTo(ev.offsetX,ev.offsetY);
60+
ctx.stroke();
61+
[lastX, lastY] = [ev.offsetX,ev.offsetY];
62+
}
63+
}
64+
65+
66+
</script>
1267
<style>
1368
html, body {
1469
margin:0;
70+
overflow: hidden;
71+
1572
}
1673
</style>
1774

0 commit comments

Comments
 (0)