-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamvas.js
executable file
·66 lines (57 loc) · 3.08 KB
/
camvas.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
This code was taken from https://github.com/cbrandolino/camvas and modified to suit our needs
*/
/*
Copyright (c) 2012 Claudio Brandolino
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// The function takes a canvas context and a `drawFunc` function.
// `drawFunc` receives two parameters, the video and the time since
// the last time it was called.
function camvas(ctx, callback) {
var self = this
this.ctx = ctx
this.callback = callback
// We can't `new Video()` yet, so we'll resort to the vintage
// "hidden div" hack for dynamic loading.
var streamContainer = document.createElement('div')
this.video = document.createElement('video')
// If we don't do this, the stream will not be played.
// By the way, the play and pause controls work as usual
// for streamed videos.
this.video.setAttribute('autoplay', '1')
this.video.setAttribute('playsinline', '1') // important for iPhones
// The video should fill out all of the canvas
this.video.setAttribute('width', 1)
this.video.setAttribute('height', 1)
streamContainer.appendChild(this.video)
document.body.appendChild(streamContainer)
// The callback happens when we are starting to stream the video.
navigator.mediaDevices.getUserMedia({video: true, audio: false}).then(function(stream) {
// Yay, now our webcam input is treated as a normal video and
// we can start having fun
self.video.srcObject = stream
// Let's start drawing the canvas!
self.update()
}, function(err) {
throw err
})
// As soon as we can draw a new frame on the canvas, we call the `draw` function
// we passed as a parameter.
this.update = function() {
var self = this
var last = Date.now()
var loop = function() {
// For some effects, you might want to know how much time is passed
// since the last frame; that's why we pass along a Delta time `dt`
// variable (expressed in milliseconds)
var dt = Date.now() - last
self.callback(self.video, dt)
last = Date.now()
requestAnimationFrame(loop)
}
requestAnimationFrame(loop)
}
}