-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy paththreex.webcamgrabbing.js
110 lines (95 loc) · 4.48 KB
/
threex.webcamgrabbing.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
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
var THREEx = THREEx || {}
// shim
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
/**
* Grab camera
* @constructor
*/
THREEx.WebcamGrabbing = function(){
//////////////////////////////////////////////////////////////////////////////////
// Comments
//////////////////////////////////////////////////////////////////////////////////
// create video element
var domElement = document.createElement('video')
domElement.setAttribute('autoplay', true)
// window.domElement = video
domElement.style.zIndex = -1;
domElement.style.position = 'absolute'
// domElement.style.top = '50%'
// domElement.style.left = '50%'
// domElement.style.marginRight = '50%'
// domElement.style.transform = 'translate(-50%, -50%)'
// domElement.style.minWidth = '100%'
domElement.style.top = '0px'
domElement.style.left = '0px'
domElement.style.width = '100%'
domElement.style.height = '100%'
/**
* Resize video element.
* - Made complex to handle the aspect change
* - it is frequently when the mobile is changing orientation
* - after a search on the internet, it seems hard/impossible to prevent browser from changing orientation
*/
function onResize(){
// is the size of the video available ?
if( domElement.videoHeight === 0 ) return
var videoAspect = domElement.videoWidth / domElement.videoHeight
var windowAspect = window.innerWidth / window.innerHeight
// var video = document.querySelector('video')
// if( videoAspect < windowAspect ){
// domElement.style.left = '0%'
// domElement.style.width = window.innerWidth + 'px'
// domElement.style.marginLeft = '0px'
//
// domElement.style.top = '50%'
// domElement.style.height = (window.innerWidth/videoAspect) + 'px'
// domElement.style.marginTop = -(window.innerWidth/videoAspect) /2 + 'px'
// console.log('videoAspect <<<<< windowAspect')
// }else{
// domElement.style.top = '0%'
// domElement.style.height = window.innerHeight+'px'
// domElement.style.marginTop = '0px'
//
// domElement.style.left = '50%'
// domElement.style.width = (window.innerHeight*videoAspect) + 'px'
// domElement.style.marginLeft = -(window.innerHeight*videoAspect)/2 + 'px'
// console.log('videoAspect >>>> windowAspect')
// }
}
window.addEventListener('resize', function(event){
onResize()
})
// just to be sure - resize on mobile is funky to say the least
setInterval(function(){
onResize()
}, 500)
// get the media sources
MediaStreamTrack.getSources(function(sourceInfos) {
// define getUserMedia() constraints
var constraints = {
video: true,
audio: false,
}
// to mirror the video element when it isnt 'environment'
// domElement.style.transform = 'scaleX(-1)'
// it it finds the videoSource 'environment', modify constraints.video
for (var i = 0; i != sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
if(sourceInfo.kind == "video" && sourceInfo.facing == "environment") {
constraints.video = {
optional: [{sourceId: sourceInfo.id}]
}
// not to mirror the video element when it is 'environment'
// domElement.style.transform = ''
}
}
// try to get user media
navigator.getUserMedia( constraints, function(stream){
domElement.src = URL.createObjectURL(stream);
}, function(error) {
console.error("Cant getUserMedia()! due to ", error);
});
});
this.domElement = domElement
}