Skip to content

Commit efabcee

Browse files
author
Carlos Filoteo
committed
Add solution for project 08.
1 parent 23c7e7d commit efabcee

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,56 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>HTML5 Canvas</title>
6+
<style>
7+
#draw {
8+
height: 100vh;
9+
width: 100vw;
10+
}
11+
</style>
612
</head>
713
<body>
814
<canvas id="draw" width="800" height="800"></canvas>
915
<script>
16+
const canvas = document.getElementById('draw')
17+
canvas.width = window.innerWidth
18+
canvas.height = window.innerHeight
19+
20+
const ctx = canvas.getContext('2d')
21+
ctx.lineJoin = 'round'
22+
ctx.lineCap = 'round'
23+
24+
let isDrawing = false,
25+
lastX = 0,
26+
lastY = 0,
27+
hue = 0,
28+
direction = true
29+
30+
function draw({offsetX, offsetY}) {
31+
if (!isDrawing) return
32+
33+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`
34+
35+
ctx.beginPath()
36+
ctx.moveTo(lastX, lastY)
37+
ctx.lineTo(offsetX, offsetY)
38+
ctx.stroke()
39+
40+
lastX = offsetX
41+
lastY = offsetY
42+
hue = hue % 360 + 1
43+
if(ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
44+
direction = !direction
45+
}
46+
direction ? ctx.lineWidth++ : ctx.lineWidth--
47+
}
48+
49+
document.addEventListener('mousemove', draw)
50+
document.addEventListener('mousedown', ({offsetX, offsetY}) => {
51+
[lastX, lastY] = [offsetX, offsetY]
52+
isDrawing = true
53+
})
54+
document.addEventListener('mouseup', () => isDrawing = false)
55+
document.addEventListener('mouseout', () => isDrawing = false)
1056
</script>
1157

1258
<style>

0 commit comments

Comments
 (0)