laya.device.min.js 16 KB

1
  1. !function(e,t){"use strict";class i{constructor(){}}class n{constructor(){}}class a extends t.EventDispatcher{constructor(e){super(),this.onDeviceOrientationChange=this.onDeviceOrientationChange.bind(this)}static get instance(){return a._instance=a._instance||new a(0),a._instance}on(e,i,n,a=null){return super.on(e,i,n,a),t.ILaya.Browser.window.addEventListener("devicemotion",this.onDeviceOrientationChange),this}off(e,i,n,a=!1){return this.hasListener(e)||t.ILaya.Browser.window.removeEventListener("devicemotion",this.onDeviceOrientationChange),super.off(e,i,n,a)}onDeviceOrientationChange(e){var i=e.interval;a.acceleration.x=e.acceleration.x,a.acceleration.y=e.acceleration.y,a.acceleration.z=e.acceleration.z,a.accelerationIncludingGravity.x=e.accelerationIncludingGravity.x,a.accelerationIncludingGravity.y=e.accelerationIncludingGravity.y,a.accelerationIncludingGravity.z=e.accelerationIncludingGravity.z,a.rotationRate.alpha=-1*e.rotationRate.gamma,a.rotationRate.beta=-1*e.rotationRate.alpha,a.rotationRate.gamma=e.rotationRate.beta,t.ILaya.Browser.onAndroid?(t.ILaya.Browser.userAgent.indexOf("Chrome")>-1&&(a.rotationRate.alpha*=180/Math.PI,a.rotationRate.beta*=180/Math.PI,a.rotationRate.gamma*=180/Math.PI),a.acceleration.x*=-1,a.accelerationIncludingGravity.x*=-1):t.ILaya.Browser.onIOS&&(a.acceleration.y*=-1,a.acceleration.z*=-1,a.accelerationIncludingGravity.y*=-1,a.accelerationIncludingGravity.z*=-1,i*=1e3),this.event(t.Event.CHANGE,[a.acceleration,a.accelerationIncludingGravity,a.rotationRate,i])}static getTransformedAcceleration(e){var n;return a.transformedAcceleration=a.transformedAcceleration||new i,a.transformedAcceleration.z=e.z,90==t.ILaya.Browser.window.orientation?(a.transformedAcceleration.x=e.y,a.transformedAcceleration.y=-e.x):-90==t.ILaya.Browser.window.orientation?(a.transformedAcceleration.x=-e.y,a.transformedAcceleration.y=e.x):t.ILaya.Browser.window.orientation?180==t.ILaya.Browser.window.orientation&&(a.transformedAcceleration.x=-e.x,a.transformedAcceleration.y=-e.y):(a.transformedAcceleration.x=e.x,a.transformedAcceleration.y=e.y),-90==t.ILaya.stage.canvasDegree?(n=a.transformedAcceleration.x,a.transformedAcceleration.x=-a.transformedAcceleration.y,a.transformedAcceleration.y=n):90==t.ILaya.stage.canvasDegree&&(n=a.transformedAcceleration.x,a.transformedAcceleration.x=a.transformedAcceleration.y,a.transformedAcceleration.y=-n),a.transformedAcceleration}}a.acceleration=new i,a.accelerationIncludingGravity=new i,a.rotationRate=new n;class o extends t.EventDispatcher{constructor(){super()}static get instance(){return o._instance=o._instance||new o,o._instance}start(e,i){this.throushold=e,this.shakeInterval=i,this.lastX=this.lastY=this.lastZ=NaN,a.instance.on(t.Event.CHANGE,this,this.onShake)}stop(){a.instance.off(t.Event.CHANGE,this,this.onShake)}onShake(e,i,n,a){if(isNaN(this.lastX))return this.lastX=i.x,this.lastY=i.y,this.lastZ=i.z,void(this.lastMillSecond=t.ILaya.Browser.now());var o=Math.abs(this.lastX-i.x),r=Math.abs(this.lastY-i.y),s=Math.abs(this.lastZ-i.z);this.isShaked(o,r,s)&&(t.ILaya.Browser.now()-this.lastMillSecond>this.shakeInterval&&(this.event(t.Event.CHANGE),this.lastMillSecond=t.ILaya.Browser.now()));this.lastX=i.x,this.lastY=i.y,this.lastZ=i.z}isShaked(e,t,i){return e>this.throushold&&t>this.throushold||e>this.throushold&&i>this.throushold||t>this.throushold&&i>this.throushold}}class r{setPosition(e){this.pos=e,this.coords=e.coords}get latitude(){return this.coords.latitude}get longitude(){return this.coords.longitude}get altitude(){return this.coords.altitude}get accuracy(){return this.coords.accuracy}get altitudeAccuracy(){return this.coords.altitudeAccuracy}get heading(){return this.coords.heading}get speed(){return this.coords.speed}get timestamp(){return this.pos.timestamp}}class s{constructor(){}static getCurrentPosition(e,t=null){s.navigator.geolocation.getCurrentPosition((function(t){s.position.setPosition(t),e.runWith(s.position)}),(function(e){t.runWith(e)}),{enableHighAccuracy:s.enableHighAccuracy,timeout:s.timeout,maximumAge:s.maximumAge})}static watchPosition(e,t){return s.navigator.geolocation.watchPosition((function(t){s.position.setPosition(t),e.runWith(s.position)}),(function(e){t.runWith(e)}),{enableHighAccuracy:s.enableHighAccuracy,timeout:s.timeout,maximumAge:s.maximumAge})}static clearWatch(e){s.navigator.geolocation.clearWatch(e)}}s.navigator=navigator,s.position=new r,s.PERMISSION_DENIED=1,s.POSITION_UNAVAILABLE=2,s.TIMEOUT=3,s.supported=!!s.navigator.geolocation,s.enableHighAccuracy=!1,s.timeout=1e10,s.maximumAge=0;class d extends t.Bitmap{constructor(){super(),this._w=0,this._h=0,this._width=1,this._height=1,this.createDomElement()}createDomElement(){this._source=this.video=t.ILaya.Browser.createElement("video");var e=this.video.style;e.position="absolute",e.top="0px",e.left="0px",this.video.addEventListener("loadedmetadata",()=>{this._w=this.video.videoWidth,this._h=this.video.videoHeight})}setSource(e,t){for(;this.video.childElementCount;)this.video.firstChild.remove();1&t&&this.appendSource(e,"video/mp4"),2&t&&this.appendSource(e+".ogg","video/ogg")}appendSource(e,i){var n=t.ILaya.Browser.createElement("source");n.src=e,n.type=i,this.video.appendChild(n)}getVideo(){return this.video}_getSource(){return this._source}destroy(){super.destroy(),t.ILaya.Render.isConchApp&&this.video._destroy()}}d.create=function(){return new d};class l extends d{constructor(){super();var e=t.LayaGL.instance;this.gl=t.ILaya.Render.isConchApp?window.LayaGLContext.instance:t.WebGLContext.mainContext,this._source=this.gl.createTexture(),t.WebGLContext.bindTexture(this.gl,e.TEXTURE_2D,this._source),this.gl.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),this.gl.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),this.gl.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),this.gl.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),t.WebGLContext.bindTexture(this.gl,e.TEXTURE_2D,null)}updateTexture(){var e=t.LayaGL.instance;t.WebGLContext.bindTexture(this.gl,e.TEXTURE_2D,this._source),this.gl.texImage2D(e.TEXTURE_2D,0,e.RGB,e.RGB,e.UNSIGNED_BYTE,this.video),l.curBindSource=this._source}get _glTexture(){return this._source}destroy(){this._source&&(this.gl=t.ILaya.Render.isConchApp?window.LayaGLContext.instance:t.WebGLContext.mainContext,this.gl&&(l.curBindSource==this._source&&(t.WebGLContext.bindTexture(this.gl,this.gl.TEXTURE_2D,null),l.curBindSource=null),this.gl.deleteTexture(this._source))),super.destroy()}}class h extends t.Sprite{constructor(e=320,i=240){super(),this.htmlVideo=new l,this.videoElement=this.htmlVideo.getVideo(),this.videoElement.layaTarget=this,this.internalTexture=new t.Texture(this.htmlVideo),this.videoElement.addEventListener("abort",h.onAbort),this.videoElement.addEventListener("canplay",h.onCanplay),this.videoElement.addEventListener("canplaythrough",h.onCanplaythrough),this.videoElement.addEventListener("durationchange",h.onDurationchange),this.videoElement.addEventListener("emptied",h.onEmptied),this.videoElement.addEventListener("error",h.onError),this.videoElement.addEventListener("loadeddata",h.onLoadeddata),this.videoElement.addEventListener("loadedmetadata",h.onLoadedmetadata),this.videoElement.addEventListener("loadstart",h.onLoadstart),this.videoElement.addEventListener("pause",h.onPause),this.videoElement.addEventListener("play",h.onPlay),this.videoElement.addEventListener("playing",h.onPlaying),this.videoElement.addEventListener("progress",h.onProgress),this.videoElement.addEventListener("ratechange",h.onRatechange),this.videoElement.addEventListener("seeked",h.onSeeked),this.videoElement.addEventListener("seeking",h.onSeeking),this.videoElement.addEventListener("stalled",h.onStalled),this.videoElement.addEventListener("suspend",h.onSuspend),this.videoElement.addEventListener("timeupdate",h.onTimeupdate),this.videoElement.addEventListener("volumechange",h.onVolumechange),this.videoElement.addEventListener("waiting",h.onWaiting),this.videoElement.addEventListener("ended",this.onPlayComplete.bind(this)),this.size(e,i),t.ILaya.Browser.onMobile&&(this.videoElement["x5-playsInline"]=!0,this.videoElement["x5-playsinline"]=!0,this.videoElement.x5PlaysInline=!0,this.videoElement.playsInline=!0,this.videoElement["webkit-playsInline"]=!0,this.videoElement["webkit-playsinline"]=!0,this.videoElement.webkitPlaysInline=!0,this.videoElement.playsinline=!0,this.videoElement.style.playsInline=!0,this.videoElement.crossOrigin="anonymous",this.videoElement.setAttribute("crossorigin","anonymous"),this.videoElement.setAttribute("playsinline","true"),this.videoElement.setAttribute("x5-playsinline","true"),this.videoElement.setAttribute("webkit-playsinline","true"),this.videoElement.autoplay=!0,this._clickhandle=this.onDocumentClick.bind(this),t.ILaya.Browser.document.addEventListener("touchend",this._clickhandle))}static onAbort(e){e.target.layaTarget.event("abort")}static onCanplay(e){e.target.layaTarget.event("canplay")}static onCanplaythrough(e){e.target.layaTarget.event("canplaythrough")}static onDurationchange(e){e.target.layaTarget.event("durationchange")}static onEmptied(e){e.target.layaTarget.event("emptied")}static onError(e){e.target.layaTarget.event("error")}static onLoadeddata(e){e.target.layaTarget.event("loadeddata")}static onLoadedmetadata(e){e.target.layaTarget.event("loadedmetadata")}static onLoadstart(e){e.target.layaTarget.event("loadstart")}static onPause(e){e.target.layaTarget.event("pause")}static onPlay(e){e.target.layaTarget.event("play")}static onPlaying(e){e.target.layaTarget.event("playing")}static onProgress(e){e.target.layaTarget.event("progress")}static onRatechange(e){e.target.layaTarget.event("ratechange")}static onSeeked(e){e.target.layaTarget.event("seeked")}static onSeeking(e){e.target.layaTarget.event("seeking")}static onStalled(e){e.target.layaTarget.event("stalled")}static onSuspend(e){e.target.layaTarget.event("suspend")}static onTimeupdate(e){e.target.layaTarget.event("timeupdate")}static onVolumechange(e){e.target.layaTarget.event("volumechange")}static onWaiting(e){e.target.layaTarget.event("waiting")}onPlayComplete(e){t.ILaya.Render.isConchApp&&this.videoElement&&this.videoElement.loop||t.ILaya.timer.clear(this,this.renderCanvas),this.event("ended")}load(e){0==e.indexOf("blob:")?this.videoElement.src=e:this.htmlVideo.setSource(e,1)}play(){this.videoElement.play(),t.ILaya.timer.frameLoop(1,this,this.renderCanvas)}pause(){this.videoElement.pause(),t.ILaya.timer.clear(this,this.renderCanvas)}reload(){this.videoElement.load()}canPlayType(e){var t;switch(e){case 1:t="video/mp4";break;case 2:t="video/ogg";break;case 8:t="video/webm"}return this.videoElement.canPlayType(t)}renderCanvas(){0!==this.readyState&&(this.htmlVideo.updateTexture(),this.graphics.clear(),this.graphics.drawTexture(this.internalTexture,0,0,this.width,this.height))}onDocumentClick(){this.videoElement&&0==this.videoElement&&(t.Browser.onIOS?this.videoElement.load():(this.videoElement.play(),this.videoElement.pause()),t.ILaya.Browser.document.removeEventListener("touchend",this._clickhandle))}get buffered(){return this.videoElement.buffered}get currentSrc(){return this.videoElement.currentSrc}get currentTime(){return this.videoElement.currentTime}set currentTime(e){this.videoElement.currentTime=e,this.renderCanvas()}set volume(e){this.videoElement.volume=e}get volume(){return this.videoElement.volume}get readyState(){return this.videoElement.readyState}get videoWidth(){return this.videoElement.videoWidth}get videoHeight(){return this.videoElement.videoHeight}get duration(){return this.videoElement.duration}get ended(){return this.videoElement.ended}get error(){return this.videoElement.error}get loop(){return this.videoElement.loop}set loop(e){this.videoElement.loop=e}set x(e){if(super.x=e,t.ILaya.Render.isConchApp){var i=t.ILaya.Utils.getTransformRelativeToWindow(this,0,0);this.videoElement.style.left=i.x}}get x(){return super.x}set y(e){if(super.y=e,t.ILaya.Render.isConchApp){var i=t.ILaya.Utils.getTransformRelativeToWindow(this,0,0);this.videoElement.style.top=i.y}}get y(){return super.y}get playbackRate(){return this.videoElement.playbackRate}set playbackRate(e){this.videoElement.playbackRate=e}get muted(){return this.videoElement.muted}set muted(e){this.videoElement.muted=e}get paused(){return this.videoElement.paused}get preload(){return this.videoElement.preload}set preload(e){this.videoElement.preload=e}get seekable(){return this.videoElement.seekable}get seeking(){return this.videoElement.seeking}size(e,i){if(super.size(e,i),t.ILaya.Render.isConchApp){var n=t.ILaya.Utils.getTransformRelativeToWindow(this,0,0);this.videoElement.width=e*n.scaleX}else this.videoElement.width=e/t.ILaya.Browser.pixelRatio,this.videoElement.height=i/t.Browser.pixelRatio;return this.paused&&this.renderCanvas(),this}set width(e){if(t.ILaya.Render.isConchApp){var i=t.ILaya.Utils.getTransformRelativeToWindow(this,0,0);this.videoElement.width=e*i.scaleX}else this.videoElement.width=this.width/t.ILaya.Browser.pixelRatio;super.width=e,this.paused&&this.renderCanvas()}get width(){return super.width}set height(e){if(t.ILaya.Render.isConchApp){var i=t.ILaya.Utils.getTransformRelativeToWindow(this,0,0);this.videoElement.height=e*i.scaleY}else this.videoElement.height=this.height/t.ILaya.Browser.pixelRatio;super.height=e}get height(){return super.height}destroy(e=!0){super.destroy(e),this.videoElement.removeEventListener("abort",h.onAbort),this.videoElement.removeEventListener("canplay",h.onCanplay),this.videoElement.removeEventListener("canplaythrough",h.onCanplaythrough),this.videoElement.removeEventListener("durationchange",h.onDurationchange),this.videoElement.removeEventListener("emptied",h.onEmptied),this.videoElement.removeEventListener("error",h.onError),this.videoElement.removeEventListener("loadeddata",h.onLoadeddata),this.videoElement.removeEventListener("loadedmetadata",h.onLoadedmetadata),this.videoElement.removeEventListener("loadstart",h.onLoadstart),this.videoElement.removeEventListener("pause",h.onPause),this.videoElement.removeEventListener("play",h.onPlay),this.videoElement.removeEventListener("playing",h.onPlaying),this.videoElement.removeEventListener("progress",h.onProgress),this.videoElement.removeEventListener("ratechange",h.onRatechange),this.videoElement.removeEventListener("seeked",h.onSeeked),this.videoElement.removeEventListener("seeking",h.onSeeking),this.videoElement.removeEventListener("stalled",h.onStalled),this.videoElement.removeEventListener("suspend",h.onSuspend),this.videoElement.removeEventListener("timeupdate",h.onTimeupdate),this.videoElement.removeEventListener("volumechange",h.onVolumechange),this.videoElement.removeEventListener("waiting",h.onWaiting),this.videoElement.removeEventListener("ended",this.onPlayComplete),this.pause(),this.videoElement.layaTarget=null,this.videoElement=null,this.htmlVideo.destroy()}syncVideoPosition(){var e,i=t.ILaya.stage;e=t.ILaya.Utils.getGlobalPosAndScale(this);var n=i._canvasTransform.a,a=i._canvasTransform.d,o=e.x*i.clientScaleX*n+i.offset.x,r=e.y*i.clientScaleY*a+i.offset.y;this.videoElement.style.left=o+"px",this.videoElement.style.top=r+"px",this.videoElement.width=this.width/t.ILaya.Browser.pixelRatio,this.videoElement.height=this.height/t.ILaya.Browser.pixelRatio}}h.MP4=1,h.OGG=2,h.CAMERA=4,h.WEBM=8,h.SUPPORT_PROBABLY="probably",h.SUPPORT_MAYBY="maybe",h.SUPPORT_NO="";class c extends t.EventDispatcher{constructor(e){super(),this.onDeviceOrientationChange=this.onDeviceOrientationChange.bind(this)}static get instance(){return c._instance=c._instance||new c(0),c._instance}on(e,i,n,a=null){return super.on(e,i,n,a),t.ILaya.Browser.window.addEventListener("deviceorientation",this.onDeviceOrientationChange),this}off(e,i,n,a=!1){return this.hasListener(e)||t.ILaya.Browser.window.removeEventListener("deviceorientation",this.onDeviceOrientationChange),super.off(e,i,n,a)}onDeviceOrientationChange(e){c.info.alpha=e.alpha,c.info.beta=e.beta,c.info.gamma=e.gamma,e.webkitCompassHeading&&(c.info.alpha=-1*e.webkitCompassHeading,c.info.compassAccuracy=e.webkitCompassAccuracy),this.event(t.Event.CHANGE,[e.absolute,c.info])}}c.info=new n,e.AccelerationInfo=i,e.Accelerator=a,e.Geolocation=s,e.GeolocationInfo=r,e.Gyroscope=c,e.HtmlVideo=d,e.Media=class{constructor(){}static supported(){return!!t.ILaya.Browser.window.navigator.getUserMedia}static getMedia(e,i,n){t.ILaya.Browser.window.navigator.getUserMedia&&t.ILaya.Browser.window.navigator.getUserMedia(e,(function(e){i.runWith(t.ILaya.Browser.window.URL.createObjectURL(e))}),(function(e){n.runWith(e)}))}},e.RotationInfo=n,e.Shake=o,e.Video=h,e.WebGLVideo=l}(window.Laya=window.Laya||{},Laya);