業(yè)務(wù)咨詢(xún):158-0164-8880
發(fā)布時(shí)間:2017-09-19 瀏覽:
function getMedia() {
if (navigator.getUserMedia) {
navigator.getUserMedia({
'video': {
'optional': [{
}]
}
}, successFunc, errorFunc);
}
else {
alert('Native device media streaming (getUserMedia) not supported in this browser.');
}
}
var localStream;
function successFunc(stream) {
document.getElementById('video').src = window.URL && window.URL.createObjectURL(stream) || stream;
localStream = stream;
}
function errorFunc(e) {
alert('Error!'+e);
}
function closeMedia() {
localStream.stop();
document.getElementById('video').src = '';
}
(http://blog.csdn.net/journey191/article/details/40744015)
var exArray = []; //存儲(chǔ)設(shè)備源ID
if (navigator.getUserMedia) {
MediaStreamTrack.getSources(function (sourceInfos) {
for (var i = 0; i != sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
//這里會(huì)遍歷audio,video,所以要加以區(qū)分
if (sourceInfo.kind === 'video') {
exArray.push(sourceInfo.id);
}
}
navigator.getUserMedia({
'video': {
'optional': [{
'sourceId': exArray[1] //0為前置攝像頭,1為后置
}]
},
'audio':false
}, successFunc, errorFunc);
});
}
<style>
#videoBox{position: absolute;left: 50%;top: 50%;z-index: 1;-webkit-transform: translate(-50%,-50%);-webkit-transform-origin: 50% 50%;}
#videoWrap{position: relative;left: 0;top: 0;background: #4CAABE;overflow: hidden;}
</style>
<div id="videoWrap">
<video autoplay="autoplay" id="videoBox"></video>
</div>
<script>
var videoWrap = document.getElementById('videoWrap');
videoWrap.style.width = window.innerWidth + 'px';
videoWrap.style.height = window.innerHeight + 'px';
</script>
/***瞄準(zhǔn)判定***/
var v = camera.getWorldDirection();
//左右判斷
var a1 = new THREE.Vector2( a.position.x, a.position.z );
var b1 = new THREE.Vector2( v.x, v.z );
dt1 = a1.angle()-b1.angle();
//上下判斷
var a2 = new THREE.Vector2( a.position.y, a.position.z );
var b2 = new THREE.Vector2( v.y, v.z );
dt2 = a2.angle()-b2.angle();
if(Math.abs(dt1)<0.1 && Math.abs(dt2)<0.1){
//居中啦!
}
var v = camera.getWorldDirection();
var geometry = new THREE.SphereGeometry( 1000, 16, 8 );
geometry.scale( -1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {
map: new THREE.TextureLoader().load( 'bg.jpg' )
} );
var stage = new THREE.Mesh( geometry, material );
stage.lookAt(new THREE.Vector3( v.x, 0, v.z ));
stage.rotateY((85 * Math.PI)/180);
stage.rotateZ((-4 * Math.PI)/180);
scene.add( stage );
if(!hasMoved && typeof(defaultCameraDirection)!='undefined'){
var v = camera.getWorldDirection();
if( v.x != defaultCameraDirection.x
|| v.y != defaultCameraDirection.y
|| v.z != defaultCameraDirection.z
){
hasMoved = true;
resetLocationAndRotation();//初始化
}
}
function getMedia() {
if (navigator.getUserMedia) {
}, successFunc, errorFunc);//errorFunc是設(shè)備支持getUserMedia但是用戶(hù)不同意調(diào)用攝像頭的情況
}
else {
//設(shè)備不支持getUserMedia
}
}
function errorFunc(e) {
alert('Error!'+e);
}
<script src="https://github.com/mrdoob/three.js/blob/master/examples/js/Detector.js"></script>
<script>
if(!Detector.webgl) {
//不支持WebGL
}else{
//支持WebGL
}
</script>
console.error = (function(origin){
return function(errorlog) {
if(/THREE/.test(errorlog)) {
//Three報(bào)錯(cuò)
}
}
})(console.error);
console.warn = (function(origin){
return function(errorlog) {
if(/^THREE.WebGLRenderer:$/.test(errorlog)) {
//Three渲染時(shí)報(bào)錯(cuò)
}
}
})(console.warn);
if(window.DeviceOrientationEvent) {
//支持陀螺儀
}else{
//不支持陀螺儀
}
var agent = navigator.userAgent.toLowerCase() ;
var version;
if(agent.indexOf("like mac os x") > 0){
//ios
var regStr_saf = /os [\d._]*/gi ;
var verinfo = agent.match(regStr_saf) ;
version = (verinfo+"").replace(/[^0-9|_.]/ig,"").replace(/_/ig,".");
}
var version_str = version+"";
if(version_str != "undefined" && version_str.length >0){
version=version.substring(0,1);
if(version>5 && version <9){
//版本為iOS8以下直接跳轉(zhuǎn)落地頁(yè)
}
}
做新技術(shù)研究和實(shí)踐的過(guò)程中會(huì)遇到?jīng)]有先例的坑,去查API文檔,曲線(xiàn)救國(guó)的方式解決問(wèn)題(有時(shí)候覺(jué)得重構(gòu)/前端是一個(gè)需要帶著小聰明的工種XD)。很感謝游戲公眾號(hào)的同事們給予的大力支持讓我們的頁(yè)面能順利上線(xiàn)并被推送給玩家,看著數(shù)據(jù)蹭蹭蹭的漲,終于覺(jué)得努力沒(méi)有白費(fèi)了。
最終頁(yè)面的數(shù)據(jù)展示出,部分用戶(hù)設(shè)備是支持拉起攝像頭的,但可能出于安全問(wèn)題的考慮,他們拒絕拉起。這是我們后續(xù)工作要考慮的一個(gè)問(wèn)題,如何保護(hù)用戶(hù)隱私以及讓用戶(hù)信任我們。
本文章為龍禧科技(u12192.cn)原創(chuàng),如轉(zhuǎn)載請(qǐng)注明出處,謝謝。
我們專(zhuān)注:北京網(wǎng)站建設(shè)/北京網(wǎng)站制作/北京網(wǎng)站設(shè)計(jì)的北京網(wǎng)站建設(shè)公司。