Skip to content

Commit 3fe431e

Browse files
committed
complete 08 challenge
1 parent 248317d commit 3fe431e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
13+
canvas.width = window.innerWidth;
14+
canvas.height = window.innerHeight;
15+
16+
let isDrawing = false;
17+
let lastX = 0;
18+
let lastY = 0;
19+
20+
ctx.lineWidth = 90;
21+
ctx.lineCap = "round";
22+
ctx.lineJoin = "round";
23+
ctx.strokeStyle = "#f00";
24+
ctx.fillStyle = "#f00";
25+
26+
let hue = 0;
27+
let direction = true;
28+
let x = 0;
29+
let y = 0;
30+
31+
function draw(e) {
32+
// console.log(method);
33+
if(!isDrawing) return;
34+
35+
if(e.type == "mousemove"){
36+
x = e.offsetX;
37+
y = e.offsetY;
38+
} else {
39+
x = e.changedTouches[0].clientX;
40+
y = e.changedTouches[0].clientY;
41+
console.log(e);
42+
}
43+
// console.log(e.type);
44+
ctx.strokeStyle = `hsl(${ hue }, 80%, 50%)`;
45+
if(hue >= 360) hue = 0;
46+
hue++;
47+
48+
if(ctx.lineWidth > 120 || ctx.lineWidth < 10) {
49+
direction = !direction;
50+
}
51+
if (direction) {
52+
ctx.lineWidth++;
53+
} else {
54+
ctx.lineWidth--;
55+
}
56+
57+
ctx.beginPath();
58+
lastX = x;
59+
lastY = y;
60+
ctx.moveTo(lastX, lastY);
61+
ctx.lineTo(x, y);
62+
ctx.stroke();
63+
// lastX = e.offsetX;
64+
// lastY = e.offsetY;
65+
// console.log(ctx.lineWidth);
66+
// console.log(x +"-" + e.offsetX);
67+
}
68+
69+
function drawTouch(e) {
70+
if(!isDrawing) return;
71+
ctx.beginPath();
72+
lastX = e.changedTouches[0].clientX;
73+
lastY = e.changedTouches[0].clientY;
74+
ctx.moveTo(lastX, lastY);
75+
ctx.lineTo(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
76+
ctx.stroke();
77+
// lastX = e.offsetX;
78+
// lastY = e.offsetY;
79+
console.log();
80+
}
81+
82+
canvas.addEventListener('mousemove', draw);
83+
canvas.addEventListener('mousedown', () => isDrawing = true);
84+
canvas.addEventListener('mouseup', () => isDrawing = false);
85+
canvas.addEventListener('mouseout', () => isDrawing = false);
86+
87+
canvas.addEventListener('touchmove', draw);
88+
canvas.addEventListener('touchstart', () => isDrawing = true);
89+
canvas.addEventListener('touchend', () => isDrawing = false);
90+
canvas.addEventListener('touchcancel', () => isDrawing = false);
91+
</script>
92+
93+
<style>
94+
html, body {
95+
margin:0;
96+
}
97+
</style>
98+
99+
</body>
100+
</html>

0 commit comments

Comments
 (0)