-
Notifications
You must be signed in to change notification settings - Fork 23
/
basic.html
73 lines (62 loc) · 1.98 KB
/
basic.html
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
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html lang="en">
<head>
<title>lazy-brush Basic Example</title>
<style>
body {
margin: 0;
}
canvas {
display: block;
cursor: crosshair;
}
</style>
</head>
<body>
<div id="app">
<canvas id="canvas" width="1024" height="768"></canvas>
</div>
<script type="module">
import { LazyBrush } from 'https://cdn.jsdelivr.net/npm/lazy-brush@2.0.0/lib/lazy-brush.js'
// The lazy radius.
const LAZY_RADIUS = 60
const BRUSH_RADIUS = 30
// Create new LazyBrush instance.
const lazy = new LazyBrush({
enabled: true,
radius: LAZY_RADIUS
})
const canvas = document.getElementById('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
// While not always reccommended, we can directly draw in your event
// handler here. Generally it is better to do that in an animation loop.
// See the "friction.html" example on how to do that.
canvas.addEventListener('mousemove', function (e) {
const x = e.clientX
const y = e.clientY
lazy.update({ x, y })
const brushHasMoved = lazy.brushHasMoved()
// We only need to update the canvas if the brush has moved.
if (brushHasMoved) {
// Get the updated brush coordinates.
const brush = lazy.getBrushCoordinates()
// Draw the canvas.
const ctx = canvas.getContext('2d')
// Clear entire canvaas.
ctx.clearRect(0, 0, canvas.width, canvas.height)
// Draw brush point
ctx.beginPath()
ctx.fillStyle = 'red'
ctx.arc(brush.x, brush.y, BRUSH_RADIUS, 0, Math.PI * 2, true)
ctx.fill()
// Draw the lazy radius.
ctx.beginPath()
ctx.strokeStyle = '#ccc'
ctx.arc(brush.x, brush.y, LAZY_RADIUS, 0, Math.PI * 2, true)
ctx.stroke()
}
})
</script>
</body>
</html>