Skip to content

Commit d42f09a

Browse files
committed
add day08
1 parent 5273333 commit d42f09a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
canvas.width = window.innerWidth;
13+
canvas.height = window.innerHeight;
14+
ctx.strokeStyle = '#BADA55';
15+
ctx.lineJoin = 'round';
16+
ctx.lineCap = 'round';
17+
ctx.lineWidth = 50;
18+
19+
let isDrawing = true;
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+
let lineX = e.offsetX;
28+
let lineY = e.offsetY;
29+
ctx.strokeStyle = `hsl(${hue},100%,50%)`;
30+
ctx.beginPath();
31+
ctx.moveTo(lastX,lastY);
32+
ctx.lineTo(lineX,lineY);
33+
ctx.stroke();
34+
[lastX,lastY] = [e.offsetX,e.offsetY];
35+
hue++;
36+
hue %= 361;
37+
if(direction){
38+
ctx.lineWidth--;
39+
}else{
40+
ctx.lineWidth++;
41+
}
42+
// if(ctx.lineWidth<=1){
43+
// direction = false;
44+
// }else if(ctx.lineWidth > 50){
45+
// direction = true;
46+
// }
47+
48+
if (ctx.lineWidth >= 50 || ctx.lineWidth <= 1) {
49+
direction = !direction;
50+
}
51+
console.log(ctx.lineWidth);
52+
// hue = (hue++)%360; //ERROR!
53+
// if(hue > 360){
54+
// hue = 0;
55+
// }
56+
}
57+
58+
canvas.addEventListener('mousemove',draw,false);
59+
canvas.addEventListener('mousedown',(e) => {
60+
[lastX,lastY] = [e.offsetX,e.offsetY];
61+
isDrawing = true;
62+
},false);
63+
canvas.addEventListener('mouseup',() => { isDrawing = false;},false);
64+
canvas.addEventListener('mouseover',() => { isDrawing = false;},false);
65+
</script>
66+
67+
<style>
68+
html, body {
69+
margin:0;
70+
}
71+
</style>
72+
73+
</body>
74+
</html>

0 commit comments

Comments
 (0)