Skip to content

Commit 4fd3c67

Browse files
author
Justin Ramel
committed
Fun with canvas
1 parent 477540d commit 4fd3c67

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

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

1254
<style>

0 commit comments

Comments
 (0)