-
Notifications
You must be signed in to change notification settings - Fork 5
/
canvas.jade
61 lines (48 loc) · 1.58 KB
/
canvas.jade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
extends base
block vars
- var title = 'HTML5 Canvas'
block styles
link(rel="stylesheet" type="text/css" href="styles/canvas.css")
block body
canvas#draw(width="800" height="800")
.controls
label(for="width") Stroke Width:
input(type="range" name="width" min="0" max="200" value="100")
script(type="text/javascript").
const $canvas = document.querySelector('#draw')
const ctx = $canvas.getContext('2d')
const $input = document.querySelector('.controls input')
$canvas.width = window.innerWidth
$canvas.height = window.innerHeight
ctx.strokeStyle = '#BADA55'
ctx.lineCap = 'round'
ctx.lineJoin = 'round'
ctx.lineWidth = $input.value || 100
ctx.globalCompositeOperation = 'multiply'
let isDrawing = false
let lastX = 0
let lastY = 0
let hue = 0
let direction = true
function draw(e) {
if (!isDrawing) return
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`
ctx.beginPath()
ctx.moveTo(lastX, lastY)
ctx.lineTo(e.offsetX, e.offsetY)
ctx.stroke()
lastX = e.offsetX
lastY = e.offsetY
hue++
if (hue >= 360) hue = 0
if (ctx.lineWidth >= $input.value || ctx.lineWidth <= 1) direction = !direction
if (direction) ctx.lineWidth++; else ctx.lineWidth--
}
$canvas.addEventListener('mousedown', (e) => {
isDrawing = true
lastX = e.offsetX
lastY = e.offsetY
})
$canvas.addEventListener('mousemove', draw)
$canvas.addEventListener('mouseup', () => isDrawing = false)
$canvas.addEventListener('mouseout', () => isDrawing = false)