-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathWebcam.js
More file actions
110 lines (75 loc) · 2.4 KB
/
Copy pathWebcam.js
File metadata and controls
110 lines (75 loc) · 2.4 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Provides access to the Webcam (if available)
* @class Phaser.Plugin.Webcam
*/
Phaser.Plugin.Webcam = function (game, parent) {
Phaser.Plugin.call(this, game, parent);
if (!game.device.getUserMedia)
{
return false;
}
// Obsolete
// navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
this.context = null;
this.stream = null;
this.video = document.createElement('video');
this.video.autoplay = true;
this.onConnect = new Phaser.Signal();
this.onError = new Phaser.Signal();
};
Phaser.Plugin.Webcam.prototype = Object.create(Phaser.Plugin.prototype);
Phaser.Plugin.Webcam.prototype.constructor = Phaser.Plugin.Webcam;
Phaser.Plugin.Webcam.prototype.start = function (width, height, context) {
// console.log('Webcam start', width, height);
this.context = context;
if (!this.stream)
{
// Obsolete
// navigator.getUserMedia( { video: { mandatory: { minWidth: width, minHeight: height } } }, this.connectCallback.bind(this), this.errorCallback.bind(this));
navigator.mediaDevices.getUserMedia({
video: true
}).then(stream => {
this.connectCallback(stream).bind(this);
}).catch(this.errorCallback.bind(this));
}
};
Phaser.Plugin.Webcam.prototype.stop = function () {
if (this.stream)
{
this.stream.stop();
this.stream = null;
}
};
Phaser.Plugin.Webcam.prototype.connectCallback = function (stream) {
this.stream = stream;
// Obsolete
// this.video.src = window.URL.createObjectURL(this.stream);
this.video.srcObject = this.stream;
this.video.play();
this.onConnect.dispatch(this.video);
};
Phaser.Plugin.Webcam.prototype.errorCallback = function (event) {
this.onError.dispatch(event);
};
Phaser.Plugin.Webcam.prototype.grab = function (context, x, y) {
if (this.stream)
{
context.drawImage(this.video, x, y);
}
};
Phaser.Plugin.Webcam.prototype.update = function () {
if (this.stream)
{
this.context.drawImage(this.video, 0, 0);
}
};
/**
* @name Phaser.Plugin.Webcam#active
* @property {boolean} active - Is this Webcam plugin capturing a video stream or not?
* @readonly
*/
Object.defineProperty(Phaser.Plugin.Webcam.prototype, "active", {
get: function() {
return (this.stream);
}
});