threex-artoolkitsource.js 9.8 KB
Newer Older
J
Jerome Etienne 已提交
1 2 3 4 5 6 7 8 9 10
var THREEx = THREEx || {}

THREEx.ArToolkitSource = function(parameters){	
	// handle default parameters
	this.parameters = {
		// type of source - ['webcam', 'image', 'video']
		sourceType : parameters.sourceType !== undefined ? parameters.sourceType : 'webcam',
		// url of the source - valid if sourceType = image|video
		sourceUrl : parameters.sourceUrl !== undefined ? parameters.sourceUrl : null,
		
11
		// resolution of at which we initialize in the source image
J
Jerome Etienne 已提交
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
		sourceWidth: parameters.sourceWidth !== undefined ? parameters.sourceWidth : 640,
		sourceHeight: parameters.sourceHeight !== undefined ? parameters.sourceHeight : 480,
		// resolution displayed for the source 
		displayWidth: parameters.displayWidth !== undefined ? parameters.displayWidth : 640,
		displayHeight: parameters.displayHeight !== undefined ? parameters.displayHeight : 480,
	}

	this.ready = false
        this.domElement = null
}

//////////////////////////////////////////////////////////////////////////////
//		Code Separator
//////////////////////////////////////////////////////////////////////////////
THREEx.ArToolkitSource.prototype.init = function(onReady){
	var _this = this

        if( this.parameters.sourceType === 'image' ){
                var domElement = this._initSourceImage(onSourceReady)                        
        }else if( this.parameters.sourceType === 'video' ){
                var domElement = this._initSourceVideo(onSourceReady)                        
        }else if( this.parameters.sourceType === 'webcam' ){
                var domElement = this._initSourceWebcam(onSourceReady)                        
        }else{
                console.assert(false)
        }

	// attach
        this.domElement = domElement
J
Jerome Etienne 已提交
41
        this.domElement.style.position = 'absolute'
J
Jerome Etienne 已提交
42
        this.domElement.style.top = '0px'
J
Jerome Etienne 已提交
43
        this.domElement.style.left = '0px'
J
Jerome Etienne 已提交
44
        this.domElement.style.zIndex = '-2'
J
Jerome Etienne 已提交
45 46 47 48 49 50

	return this
        function onSourceReady(){
		document.body.appendChild(_this.domElement);

		_this.ready = true
J
Jerome Etienne 已提交
51

J
Jerome Etienne 已提交
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
		onReady && onReady()
        }
} 

////////////////////////////////////////////////////////////////////////////////
//          init image source
////////////////////////////////////////////////////////////////////////////////


THREEx.ArToolkitSource.prototype._initSourceImage = function(onReady) {
	// TODO make it static
        var domElement = document.createElement('img')
	domElement.src = this.parameters.sourceUrl

	domElement.width = this.parameters.sourceWidth
	domElement.height = this.parameters.sourceHeight
	domElement.style.width = this.parameters.displayWidth+'px'
	domElement.style.height = this.parameters.displayHeight+'px'

	// wait until the video stream is ready
	var interval = setInterval(function() {
		if (!domElement.naturalWidth)	return;
		onReady()
		clearInterval(interval)
	}, 1000/50);

	return domElement                
}

////////////////////////////////////////////////////////////////////////////////
//          init video source
////////////////////////////////////////////////////////////////////////////////


THREEx.ArToolkitSource.prototype._initSourceVideo = function(onReady) {
	// TODO make it static
	var domElement = document.createElement('video');
	domElement.src = this.parameters.sourceUrl

	domElement.style.objectFit = 'initial'

	domElement.autoplay = true;
	domElement.webkitPlaysinline = true;
	domElement.controls = false;
	domElement.loop = true;
J
Jerome Etienne 已提交
97
	domElement.muted = true
J
Jerome Etienne 已提交
98 99

	// trick to trigger the video on android
100
	document.body.addEventListener('click', function onClick(){
J
oopsa  
Jerome Etienne 已提交
101
		document.body.removeEventListener('click', onClick);
J
Jerome Etienne 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		domElement.play()
	})

	domElement.width = this.parameters.sourceWidth
	domElement.height = this.parameters.sourceHeight
	domElement.style.width = this.parameters.displayWidth+'px'
	domElement.style.height = this.parameters.displayHeight+'px'
	
	// wait until the video stream is ready
	var interval = setInterval(function() {
		if (!domElement.videoWidth)	return;
		onReady()
		clearInterval(interval)
	}, 1000/50);
	return domElement
}

////////////////////////////////////////////////////////////////////////////////
//          handle webcam source
////////////////////////////////////////////////////////////////////////////////


THREEx.ArToolkitSource.prototype._initSourceWebcam = function(onReady) {
	var _this = this
	// TODO make it static
	navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

	var domElement = document.createElement('video');
	domElement.style.width = this.parameters.displayWidth+'px'
	domElement.style.height = this.parameters.displayHeight+'px'


J
Jerome Etienne 已提交
134 135 136 137 138 139
	if (navigator.getUserMedia === undefined ){
		alert("WebRTC issue! navigator.getUserMedia not present in your browser");		
	}
	if (navigator.mediaDevices === undefined || navigator.mediaDevices.enumerateDevices === undefined ){
		alert("WebRTC issue! navigator.mediaDevices.enumerateDevices not present in your browser");		
	}
J
Jerome Etienne 已提交
140 141 142 143 144 145 146 147 148 149 150 151

	navigator.mediaDevices.enumerateDevices().then(function(devices) {
                // define getUserMedia() constraints
                var constraints = {
			audio: false,
			video: {
				mandatory: {
					maxWidth: _this.parameters.sourceWidth,
					maxHeight: _this.parameters.sourceHeight
		    		}
		  	}
                }
J
Jerome Etienne 已提交
152

J
Jerome Etienne 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		// TODO super unclear how to get the backward facing camera...
		// use heuristic - on chrome android current algo is working
		// 
		// on macosx it isnt. figure out the algo, and do if(macosx)
		// - with one or two camera
		// 
		// some issue on window
		
		/**
		 * how to test
		 * - one or two camera on macbook
		 * - my phone
		 */
		var runOnMobile = 'ontouchstart' in window ? true : false
		if( runOnMobile === true ){
			pickDeviceAndroid()
		}else{
			pickDeviceMacosx()
		}
		

		function pickDeviceAndroid(){
			devices.forEach(function(device) {
				if( device.kind !== 'videoinput' )	return
				constraints.video.optional = [{sourceId: device.deviceId}]
			});			
		}
		function pickDeviceMacosx(){
			devices.forEach(function(device) {
				if( device.kind !== 'videoinput' )	return

				if( constraints.video.optional !== undefined )	return
				constraints.video.optional = [{sourceId: device.deviceId}]
			});			
		}
J
Jerome Etienne 已提交
188 189 190 191 192 193 194 195 196 197 198

		// OLD API
                // 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}]
                //         }
                // }

		navigator.getUserMedia(constraints, function success(stream) {
J
Jerome Etienne 已提交
199
			// console.log('success', stream);
J
Jerome Etienne 已提交
200 201 202 203 204
			domElement.src = window.URL.createObjectURL(stream);
			// to start the video, when it is possible to start it only on userevent. like in android
			document.body.addEventListener('click', function(){
				domElement.play();
			})
J
Jerome Etienne 已提交
205
			// domElement.play();
J
Jerome Etienne 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		
			// wait until the video stream is ready
			var interval = setInterval(function() {
				if (!domElement.videoWidth)	return;
				onReady()
				clearInterval(interval)
			}, 1000/50);
		}, function(error) {
			console.log("Can't access user media", error);
			alert("Can't access user media :()");
		});
	}).catch(function(err) {
		console.log(err.name + ": " + err.message);
	});

	return domElement
}

////////////////////////////////////////////////////////////////////////////////
//          handle resize
////////////////////////////////////////////////////////////////////////////////

J
Jerome Etienne 已提交
228 229
THREEx.ArToolkitSource.prototype.onResize = function(mirrorDomElements){
	var _this = this
J
Jerome Etienne 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	var screenWidth = window.innerWidth
	var screenHeight = window.innerHeight

	// compute sourceWidth, sourceHeight
	if( this.domElement.nodeName === "IMG" ){
		var sourceWidth = this.domElement.naturalWidth
		var sourceHeight = this.domElement.naturalHeight
	}else if( this.domElement.nodeName === "VIDEO" ){
		var sourceWidth = this.domElement.videoWidth
		var sourceHeight = this.domElement.videoHeight
	}else{
		console.assert(false)
	}
	
	// compute sourceAspect
	var sourceAspect = sourceWidth / sourceHeight
	// compute screenAspect
	var screenAspect = screenWidth / screenHeight

	// if screenAspect < sourceAspect, then change the width, else change the height
	if( screenAspect < sourceAspect ){
		// compute newWidth and set .width/.marginLeft
		var newWidth = sourceAspect * screenHeight
		this.domElement.style.width = newWidth+'px'
		this.domElement.style.marginLeft = -(newWidth-screenWidth)/2+'px'
		
		// init style.height/.marginTop to normal value
		this.domElement.style.height = screenHeight+'px'
		this.domElement.style.marginTop = '0px'
	}else{
		// compute newHeight and set .height/.marginTop
		var newHeight = 1 / (sourceAspect / screenWidth)
		this.domElement.style.height = newHeight+'px'
		this.domElement.style.marginTop = -(newHeight-screenHeight)/2+'px'
		
		// init style.width/.marginLeft to normal value
		this.domElement.style.width = screenWidth+'px'
		this.domElement.style.marginLeft = '0px'
	}
	
J
Jerome Etienne 已提交
270
	// honor default parameters
J
sync up  
Jerome Etienne 已提交
271
	// if( mirrorDomElements !== undefined )	console.warn('still use the old resize. fix it')
J
Jerome Etienne 已提交
272 273 274
	if( mirrorDomElements === undefined )	mirrorDomElements = []
	if( mirrorDomElements instanceof Array === false )	mirrorDomElements = [mirrorDomElements]	

J
Jerome Etienne 已提交
275 276
	// Mirror _this.domElement.style to mirrorDomElements
	mirrorDomElements.forEach(function(domElement){
277
		_this.copySizeTo(domElement)
J
Jerome Etienne 已提交
278
	})
J
Jerome Etienne 已提交
279
}
280 281 282 283 284 285 286

THREEx.ArToolkitSource.prototype.copySizeTo = function(otherElement){
	otherElement.style.width = this.domElement.style.width
	otherElement.style.height = this.domElement.style.height	
	otherElement.style.marginLeft = this.domElement.style.marginLeft
	otherElement.style.marginTop = this.domElement.style.marginTop
}