-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscript.js
47 lines (39 loc) · 1.15 KB
/
script.js
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
const canvas = document.getElementById('canvas')
const context = canvas.getContext('2d')
const tracker = new tracking.ObjectTracker('face')
const flowerCrownButton = document.getElementById('flower-crown')
const bunnyEarsButton = document.getElementById('bunny-ears')
const img = new Image()
let filterX = 0
let filterY = 0
let filterWidth = 0
let filterHeight = 0
function changePic (x, y, width, height, src) {
img.src = src
filterX = x
filterY = y
filterWidth = width
filterHeight = height
}
function flowerCrown () {
changePic(0, -0.5, 1, 1, 'flower-crown.png')
}
flowerCrown()
flowerCrownButton.addEventListener('click', flowerCrown)
bunnyEarsButton.addEventListener('click', () => {
changePic(-0.5, -0.9, 2, 2, 'bunny-ears.png')
})
tracker.setInitialScale(4)
tracker.setStepSize(2)
tracker.setEdgesDensity(0.1)
tracking.track('#video', tracker, { camera: true })
tracker.on('track', event => {
context.clearRect(0, 0, canvas.width, canvas.height)
event.data.forEach(rect => {
context.drawImage(img, rect.x + (filterX * rect.width),
rect.y + (filterY * rect.height),
rect.width * filterWidth,
rect.height * filterHeight
)
})
})