Skip to content

Commit cb30176

Browse files
author
Yuri Brunetto
committed
complete wesbos#8
1 parent 44f1bf5 commit cb30176

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,56 @@
66
</head>
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
9+
910
<script>
11+
const canvas = document.querySelector('#draw')
12+
const ctx = canvas.getContext('2d')
13+
14+
canvas.width = window.innerWidth
15+
canvas.height = window.innerHeight
16+
17+
ctx.strokeStyle = '#BADA55'
18+
ctx.lineJoin = 'round'
19+
ctx.lineCap = 'round'
20+
ctx.lineWidth = 100
21+
ctx.globalCompositeOperation = 'ligthen'
22+
23+
let isDrawing = false
24+
let lastX = 0
25+
let lastY = 0
26+
let hue = 0
27+
let direction = true
28+
29+
function draw(e) {
30+
if (!isDrawing) return
31+
console.log(e)
32+
ctx.strokeStyle = `hsl(${hue}, 30%, 50%)`
33+
34+
ctx.beginPath()
35+
ctx.moveTo(lastX, lastY)
36+
ctx.lineTo(e.offsetX, e.offsetY)
37+
ctx.stroke()
38+
lastX = e.offsetX
39+
lastY = e.offsetY
40+
41+
hue++
42+
if (hue >= 360) hue = 0
43+
44+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) direction = !direction
45+
if (direction) {
46+
ctx.lineWidth++
47+
} else {
48+
ctx.lineWidth--
49+
}
50+
}
51+
canvas.addEventListener('mousedown', (e) => {
52+
isDrawing = true
53+
lastX = e.offsetX
54+
lastY = e.offsetY
55+
})
56+
canvas.addEventListener('mousemove', draw)
57+
canvas.addEventListener('mouseup', () => isDrawing = false)
58+
canvas.addEventListener('mouseout', () => isDrawing = false)
1059
</script>
1160

1261
<style>

0 commit comments

Comments
 (0)