laya.device.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. (function (exports, Laya) {
  2. 'use strict';
  3. class AccelerationInfo {
  4. constructor() {
  5. }
  6. }
  7. class RotationInfo {
  8. constructor() {
  9. }
  10. }
  11. class Accelerator extends Laya.EventDispatcher {
  12. constructor(singleton) {
  13. super();
  14. this.onDeviceOrientationChange = this.onDeviceOrientationChange.bind(this);
  15. }
  16. static get instance() {
  17. Accelerator._instance = Accelerator._instance || new Accelerator(0);
  18. return Accelerator._instance;
  19. }
  20. on(type, caller, listener, args = null) {
  21. super.on(type, caller, listener, args);
  22. Laya.ILaya.Browser.window.addEventListener('devicemotion', this.onDeviceOrientationChange);
  23. return this;
  24. }
  25. off(type, caller, listener, onceOnly = false) {
  26. if (!this.hasListener(type))
  27. Laya.ILaya.Browser.window.removeEventListener('devicemotion', this.onDeviceOrientationChange);
  28. return super.off(type, caller, listener, onceOnly);
  29. }
  30. onDeviceOrientationChange(e) {
  31. var interval = e.interval;
  32. Accelerator.acceleration.x = e.acceleration.x;
  33. Accelerator.acceleration.y = e.acceleration.y;
  34. Accelerator.acceleration.z = e.acceleration.z;
  35. Accelerator.accelerationIncludingGravity.x = e.accelerationIncludingGravity.x;
  36. Accelerator.accelerationIncludingGravity.y = e.accelerationIncludingGravity.y;
  37. Accelerator.accelerationIncludingGravity.z = e.accelerationIncludingGravity.z;
  38. Accelerator.rotationRate.alpha = e.rotationRate.gamma * -1;
  39. Accelerator.rotationRate.beta = e.rotationRate.alpha * -1;
  40. Accelerator.rotationRate.gamma = e.rotationRate.beta;
  41. if (Laya.ILaya.Browser.onAndroid) {
  42. if (Laya.ILaya.Browser.userAgent.indexOf("Chrome") > -1) {
  43. Accelerator.rotationRate.alpha *= 180 / Math.PI;
  44. Accelerator.rotationRate.beta *= 180 / Math.PI;
  45. Accelerator.rotationRate.gamma *= 180 / Math.PI;
  46. }
  47. Accelerator.acceleration.x *= -1;
  48. Accelerator.accelerationIncludingGravity.x *= -1;
  49. }
  50. else if (Laya.ILaya.Browser.onIOS) {
  51. Accelerator.acceleration.y *= -1;
  52. Accelerator.acceleration.z *= -1;
  53. Accelerator.accelerationIncludingGravity.y *= -1;
  54. Accelerator.accelerationIncludingGravity.z *= -1;
  55. interval *= 1000;
  56. }
  57. this.event(Laya.Event.CHANGE, [Accelerator.acceleration, Accelerator.accelerationIncludingGravity, Accelerator.rotationRate, interval]);
  58. }
  59. static getTransformedAcceleration(acceleration) {
  60. Accelerator.transformedAcceleration = Accelerator.transformedAcceleration || new AccelerationInfo();
  61. Accelerator.transformedAcceleration.z = acceleration.z;
  62. if (Laya.ILaya.Browser.window.orientation == 90) {
  63. Accelerator.transformedAcceleration.x = acceleration.y;
  64. Accelerator.transformedAcceleration.y = -acceleration.x;
  65. }
  66. else if (Laya.ILaya.Browser.window.orientation == -90) {
  67. Accelerator.transformedAcceleration.x = -acceleration.y;
  68. Accelerator.transformedAcceleration.y = acceleration.x;
  69. }
  70. else if (!Laya.ILaya.Browser.window.orientation) {
  71. Accelerator.transformedAcceleration.x = acceleration.x;
  72. Accelerator.transformedAcceleration.y = acceleration.y;
  73. }
  74. else if (Laya.ILaya.Browser.window.orientation == 180) {
  75. Accelerator.transformedAcceleration.x = -acceleration.x;
  76. Accelerator.transformedAcceleration.y = -acceleration.y;
  77. }
  78. var tx;
  79. if (Laya.ILaya.stage.canvasDegree == -90) {
  80. tx = Accelerator.transformedAcceleration.x;
  81. Accelerator.transformedAcceleration.x = -Accelerator.transformedAcceleration.y;
  82. Accelerator.transformedAcceleration.y = tx;
  83. }
  84. else if (Laya.ILaya.stage.canvasDegree == 90) {
  85. tx = Accelerator.transformedAcceleration.x;
  86. Accelerator.transformedAcceleration.x = Accelerator.transformedAcceleration.y;
  87. Accelerator.transformedAcceleration.y = -tx;
  88. }
  89. return Accelerator.transformedAcceleration;
  90. }
  91. }
  92. Accelerator.acceleration = new AccelerationInfo();
  93. Accelerator.accelerationIncludingGravity = new AccelerationInfo();
  94. Accelerator.rotationRate = new RotationInfo();
  95. class Shake extends Laya.EventDispatcher {
  96. constructor() {
  97. super();
  98. }
  99. static get instance() {
  100. Shake._instance = Shake._instance || new Shake();
  101. return Shake._instance;
  102. }
  103. start(throushold, interval) {
  104. this.throushold = throushold;
  105. this.shakeInterval = interval;
  106. this.lastX = this.lastY = this.lastZ = NaN;
  107. Accelerator.instance.on(Laya.Event.CHANGE, this, this.onShake);
  108. }
  109. stop() {
  110. Accelerator.instance.off(Laya.Event.CHANGE, this, this.onShake);
  111. }
  112. onShake(acceleration, accelerationIncludingGravity, rotationRate, interval) {
  113. if (isNaN(this.lastX)) {
  114. this.lastX = accelerationIncludingGravity.x;
  115. this.lastY = accelerationIncludingGravity.y;
  116. this.lastZ = accelerationIncludingGravity.z;
  117. this.lastMillSecond = Laya.ILaya.Browser.now();
  118. return;
  119. }
  120. var deltaX = Math.abs(this.lastX - accelerationIncludingGravity.x);
  121. var deltaY = Math.abs(this.lastY - accelerationIncludingGravity.y);
  122. var deltaZ = Math.abs(this.lastZ - accelerationIncludingGravity.z);
  123. if (this.isShaked(deltaX, deltaY, deltaZ)) {
  124. var deltaMillSecond = Laya.ILaya.Browser.now() - this.lastMillSecond;
  125. if (deltaMillSecond > this.shakeInterval) {
  126. this.event(Laya.Event.CHANGE);
  127. this.lastMillSecond = Laya.ILaya.Browser.now();
  128. }
  129. }
  130. this.lastX = accelerationIncludingGravity.x;
  131. this.lastY = accelerationIncludingGravity.y;
  132. this.lastZ = accelerationIncludingGravity.z;
  133. }
  134. isShaked(deltaX, deltaY, deltaZ) {
  135. return (deltaX > this.throushold && deltaY > this.throushold) ||
  136. (deltaX > this.throushold && deltaZ > this.throushold) ||
  137. (deltaY > this.throushold && deltaZ > this.throushold);
  138. }
  139. }
  140. class GeolocationInfo {
  141. setPosition(pos) {
  142. this.pos = pos;
  143. this.coords = pos.coords;
  144. }
  145. get latitude() {
  146. return this.coords.latitude;
  147. }
  148. get longitude() {
  149. return this.coords.longitude;
  150. }
  151. get altitude() {
  152. return this.coords.altitude;
  153. }
  154. get accuracy() {
  155. return this.coords.accuracy;
  156. }
  157. get altitudeAccuracy() {
  158. return this.coords.altitudeAccuracy;
  159. }
  160. get heading() {
  161. return this.coords.heading;
  162. }
  163. get speed() {
  164. return this.coords.speed;
  165. }
  166. get timestamp() {
  167. return this.pos.timestamp;
  168. }
  169. }
  170. class Geolocation {
  171. constructor() {
  172. }
  173. static getCurrentPosition(onSuccess, onError = null) {
  174. Geolocation.navigator.geolocation.getCurrentPosition(function (pos) {
  175. Geolocation.position.setPosition(pos);
  176. onSuccess.runWith(Geolocation.position);
  177. }, function (error) {
  178. onError.runWith(error);
  179. }, {
  180. enableHighAccuracy: Geolocation.enableHighAccuracy,
  181. timeout: Geolocation.timeout,
  182. maximumAge: Geolocation.maximumAge
  183. });
  184. }
  185. static watchPosition(onSuccess, onError) {
  186. return Geolocation.navigator.geolocation.watchPosition(function (pos) {
  187. Geolocation.position.setPosition(pos);
  188. onSuccess.runWith(Geolocation.position);
  189. }, function (error) {
  190. onError.runWith(error);
  191. }, {
  192. enableHighAccuracy: Geolocation.enableHighAccuracy,
  193. timeout: Geolocation.timeout,
  194. maximumAge: Geolocation.maximumAge
  195. });
  196. }
  197. static clearWatch(id) {
  198. Geolocation.navigator.geolocation.clearWatch(id);
  199. }
  200. }
  201. Geolocation.navigator = Laya.ILaya.Browser.window.navigator;
  202. Geolocation.position = new GeolocationInfo();
  203. Geolocation.PERMISSION_DENIED = 1;
  204. Geolocation.POSITION_UNAVAILABLE = 2;
  205. Geolocation.TIMEOUT = 3;
  206. Geolocation.supported = !!Geolocation.navigator.geolocation;
  207. Geolocation.enableHighAccuracy = false;
  208. Geolocation.timeout = 1E10;
  209. Geolocation.maximumAge = 0;
  210. class HtmlVideo extends Laya.Bitmap {
  211. constructor() {
  212. super();
  213. this._width = 1;
  214. this._height = 1;
  215. this.createDomElement();
  216. }
  217. createDomElement() {
  218. this._source = this.video = Laya.ILaya.Browser.createElement("video");
  219. var style = this.video.style;
  220. style.position = 'absolute';
  221. style.top = '0px';
  222. style.left = '0px';
  223. this.video.addEventListener("loadedmetadata", (function () {
  224. this._w = this.video.videoWidth;
  225. this._h = this.video.videoHeight;
  226. })['bind'](this));
  227. }
  228. setSource(url, extension) {
  229. while (this.video.childElementCount)
  230. this.video.firstChild.remove();
  231. if (extension & 1)
  232. this.appendSource(url, "video/mp4");
  233. if (extension & 2)
  234. this.appendSource(url + ".ogg", "video/ogg");
  235. }
  236. appendSource(source, type) {
  237. var sourceElement = Laya.ILaya.Browser.createElement("source");
  238. sourceElement.src = source;
  239. sourceElement.type = type;
  240. this.video.appendChild(sourceElement);
  241. }
  242. getVideo() {
  243. return this.video;
  244. }
  245. _getSource() {
  246. return this._source;
  247. }
  248. destroy() {
  249. super.destroy();
  250. var isConchApp = Laya.ILaya.Render.isConchApp;
  251. if (isConchApp) {
  252. this.video._destroy();
  253. }
  254. }
  255. }
  256. HtmlVideo.create = function () {
  257. return new HtmlVideo();
  258. };
  259. class Media {
  260. constructor() {
  261. }
  262. static supported() {
  263. return !!Laya.ILaya.Browser.window.navigator.getUserMedia;
  264. }
  265. static getMedia(options, onSuccess, onError) {
  266. if (Laya.ILaya.Browser.window.navigator.getUserMedia) {
  267. Laya.ILaya.Browser.window.navigator.getUserMedia(options, function (stream) {
  268. onSuccess.runWith(Laya.ILaya.Browser.window.URL.createObjectURL(stream));
  269. }, function (err) {
  270. onError.runWith(err);
  271. });
  272. }
  273. }
  274. }
  275. class WebGLVideo extends HtmlVideo {
  276. constructor() {
  277. super();
  278. var gl = Laya.LayaGL.instance;
  279. if (!Laya.ILaya.Render.isConchApp && Laya.ILaya.Browser.onIPhone)
  280. return;
  281. this.gl = Laya.ILaya.Render.isConchApp ? window.LayaGLContext.instance : Laya.WebGLContext.mainContext;
  282. this._source = this.gl.createTexture();
  283. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, this._source);
  284. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  285. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  286. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  287. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  288. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, null);
  289. }
  290. updateTexture() {
  291. if (!Laya.ILaya.Render.isConchApp && Laya.ILaya.Browser.onIPhone)
  292. return;
  293. var gl = Laya.LayaGL.instance;
  294. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, this._source);
  295. this.gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.video);
  296. WebGLVideo.curBindSource = this._source;
  297. }
  298. get _glTexture() {
  299. return this._source;
  300. }
  301. destroy() {
  302. if (this._source) {
  303. this.gl = Laya.ILaya.Render.isConchApp ? window.LayaGLContext.instance : Laya.WebGLContext.mainContext;
  304. if (WebGLVideo.curBindSource == this._source) {
  305. Laya.WebGLContext.bindTexture(this.gl, this.gl.TEXTURE_2D, null);
  306. WebGLVideo.curBindSource = null;
  307. }
  308. this.gl.deleteTexture(this._source);
  309. }
  310. super.destroy();
  311. }
  312. }
  313. class Video extends Laya.Sprite {
  314. constructor(width = 320, height = 240) {
  315. super();
  316. this.htmlVideo = new WebGLVideo();
  317. this.videoElement = this.htmlVideo.getVideo();
  318. this.videoElement.layaTarget = this;
  319. this.internalTexture = new Laya.Texture(this.htmlVideo);
  320. this.videoElement.addEventListener("abort", Video.onAbort);
  321. this.videoElement.addEventListener("canplay", Video.onCanplay);
  322. this.videoElement.addEventListener("canplaythrough", Video.onCanplaythrough);
  323. this.videoElement.addEventListener("durationchange", Video.onDurationchange);
  324. this.videoElement.addEventListener("emptied", Video.onEmptied);
  325. this.videoElement.addEventListener("error", Video.onError);
  326. this.videoElement.addEventListener("loadeddata", Video.onLoadeddata);
  327. this.videoElement.addEventListener("loadedmetadata", Video.onLoadedmetadata);
  328. this.videoElement.addEventListener("loadstart", Video.onLoadstart);
  329. this.videoElement.addEventListener("pause", Video.onPause);
  330. this.videoElement.addEventListener("play", Video.onPlay);
  331. this.videoElement.addEventListener("playing", Video.onPlaying);
  332. this.videoElement.addEventListener("progress", Video.onProgress);
  333. this.videoElement.addEventListener("ratechange", Video.onRatechange);
  334. this.videoElement.addEventListener("seeked", Video.onSeeked);
  335. this.videoElement.addEventListener("seeking", Video.onSeeking);
  336. this.videoElement.addEventListener("stalled", Video.onStalled);
  337. this.videoElement.addEventListener("suspend", Video.onSuspend);
  338. this.videoElement.addEventListener("timeupdate", Video.onTimeupdate);
  339. this.videoElement.addEventListener("volumechange", Video.onVolumechange);
  340. this.videoElement.addEventListener("waiting", Video.onWaiting);
  341. this.videoElement.addEventListener("ended", this.onPlayComplete['bind'](this));
  342. this.size(width, height);
  343. if (Laya.ILaya.Browser.onMobile) {
  344. this.onDocumentClick = this.onDocumentClick.bind(this);
  345. Laya.ILaya.Browser.document.addEventListener("touchend", this.onDocumentClick);
  346. }
  347. }
  348. static onAbort(e) { e.target.layaTarget.event("abort"); }
  349. static onCanplay(e) { e.target.layaTarget.event("canplay"); }
  350. static onCanplaythrough(e) { e.target.layaTarget.event("canplaythrough"); }
  351. static onDurationchange(e) { e.target.layaTarget.event("durationchange"); }
  352. static onEmptied(e) { e.target.layaTarget.event("emptied"); }
  353. static onError(e) { e.target.layaTarget.event("error"); }
  354. static onLoadeddata(e) { e.target.layaTarget.event("loadeddata"); }
  355. static onLoadedmetadata(e) { e.target.layaTarget.event("loadedmetadata"); }
  356. static onLoadstart(e) { e.target.layaTarget.event("loadstart"); }
  357. static onPause(e) { e.target.layaTarget.event("pause"); }
  358. static onPlay(e) { e.target.layaTarget.event("play"); }
  359. static onPlaying(e) { e.target.layaTarget.event("playing"); }
  360. static onProgress(e) { e.target.layaTarget.event("progress"); }
  361. static onRatechange(e) { e.target.layaTarget.event("ratechange"); }
  362. static onSeeked(e) { e.target.layaTarget.event("seeked"); }
  363. static onSeeking(e) { e.target.layaTarget.event("seeking"); }
  364. static onStalled(e) { e.target.layaTarget.event("stalled"); }
  365. static onSuspend(e) { e.target.layaTarget.event("suspend"); }
  366. static onTimeupdate(e) { e.target.layaTarget.event("timeupdate"); }
  367. static onVolumechange(e) { e.target.layaTarget.event("volumechange"); }
  368. static onWaiting(e) { e.target.layaTarget.event("waiting"); }
  369. onPlayComplete(e) {
  370. this.event("ended");
  371. if (!Laya.ILaya.Render.isConchApp || !this.videoElement || !this.videoElement.loop)
  372. Laya.ILaya.timer.clear(this, this.renderCanvas);
  373. }
  374. load(url) {
  375. if (url.indexOf("blob:") == 0)
  376. this.videoElement.src = url;
  377. else
  378. this.htmlVideo.setSource(url, 1);
  379. }
  380. play() {
  381. this.videoElement.play();
  382. Laya.ILaya.timer.frameLoop(1, this, this.renderCanvas);
  383. }
  384. pause() {
  385. this.videoElement.pause();
  386. Laya.ILaya.timer.clear(this, this.renderCanvas);
  387. }
  388. reload() {
  389. this.videoElement.load();
  390. }
  391. canPlayType(type) {
  392. var typeString;
  393. switch (type) {
  394. case 1:
  395. typeString = "video/mp4";
  396. break;
  397. case 2:
  398. typeString = "video/ogg";
  399. break;
  400. case 8:
  401. typeString = "video/webm";
  402. break;
  403. }
  404. return this.videoElement.canPlayType(typeString);
  405. }
  406. renderCanvas() {
  407. if (this.readyState === 0)
  408. return;
  409. this.htmlVideo['updateTexture']();
  410. this.graphics.clear();
  411. this.graphics.drawTexture(this.internalTexture, 0, 0, this.width, this.height);
  412. }
  413. onDocumentClick() {
  414. this.videoElement.play();
  415. this.videoElement.pause();
  416. Laya.ILaya.Browser.document.removeEventListener("touchend", this.onDocumentClick);
  417. }
  418. get buffered() {
  419. return this.videoElement.buffered;
  420. }
  421. get currentSrc() {
  422. return this.videoElement.currentSrc;
  423. }
  424. get currentTime() {
  425. return this.videoElement.currentTime;
  426. }
  427. set currentTime(value) {
  428. this.videoElement.currentTime = value;
  429. this.renderCanvas();
  430. }
  431. set volume(value) {
  432. this.videoElement.volume = value;
  433. }
  434. get volume() {
  435. return this.videoElement.volume;
  436. }
  437. get readyState() {
  438. return this.videoElement.readyState;
  439. }
  440. get videoWidth() {
  441. return this.videoElement.videoWidth;
  442. }
  443. get videoHeight() {
  444. return this.videoElement.videoHeight;
  445. }
  446. get duration() {
  447. return this.videoElement.duration;
  448. }
  449. get ended() {
  450. return this.videoElement.ended;
  451. }
  452. get error() {
  453. return this.videoElement.error;
  454. }
  455. get loop() {
  456. return this.videoElement.loop;
  457. }
  458. set loop(value) {
  459. this.videoElement.loop = value;
  460. }
  461. set x(val) {
  462. super.x = val;
  463. if (Laya.ILaya.Render.isConchApp) {
  464. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  465. this.videoElement.style.left = transform.x;
  466. }
  467. }
  468. get x() {
  469. return super.x;
  470. }
  471. set y(val) {
  472. super.y = val;
  473. if (Laya.ILaya.Render.isConchApp) {
  474. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  475. this.videoElement.style.top = transform.y;
  476. }
  477. }
  478. get y() {
  479. return super.y;
  480. }
  481. get playbackRate() {
  482. return this.videoElement.playbackRate;
  483. }
  484. set playbackRate(value) {
  485. this.videoElement.playbackRate = value;
  486. }
  487. get muted() {
  488. return this.videoElement.muted;
  489. }
  490. set muted(value) {
  491. this.videoElement.muted = value;
  492. }
  493. get paused() {
  494. return this.videoElement.paused;
  495. }
  496. get preload() {
  497. return this.videoElement.preload;
  498. }
  499. set preload(value) {
  500. this.videoElement.preload = value;
  501. }
  502. get seekable() {
  503. return this.videoElement.seekable;
  504. }
  505. get seeking() {
  506. return this.videoElement.seeking;
  507. }
  508. size(width, height) {
  509. super.size(width, height);
  510. if (Laya.ILaya.Render.isConchApp) {
  511. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  512. this.videoElement.width = width * transform.scaleX;
  513. }
  514. else {
  515. this.videoElement.width = width / Laya.ILaya.Browser.pixelRatio;
  516. }
  517. if (this.paused)
  518. this.renderCanvas();
  519. return this;
  520. }
  521. set width(value) {
  522. if (Laya.ILaya.Render.isConchApp) {
  523. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  524. this.videoElement.width = value * transform.scaleX;
  525. }
  526. else {
  527. this.videoElement.width = this.width / Laya.ILaya.Browser.pixelRatio;
  528. }
  529. super.width = value;
  530. if (this.paused)
  531. this.renderCanvas();
  532. }
  533. get width() {
  534. return super.width;
  535. }
  536. set height(value) {
  537. if (Laya.ILaya.Render.isConchApp) {
  538. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  539. this.videoElement.height = value * transform.scaleY;
  540. }
  541. else {
  542. this.videoElement.height = this.height / Laya.ILaya.Browser.pixelRatio;
  543. }
  544. super.height = value;
  545. }
  546. get height() {
  547. return super.height;
  548. }
  549. destroy(detroyChildren = true) {
  550. super.destroy(detroyChildren);
  551. this.videoElement.removeEventListener("abort", Video.onAbort);
  552. this.videoElement.removeEventListener("canplay", Video.onCanplay);
  553. this.videoElement.removeEventListener("canplaythrough", Video.onCanplaythrough);
  554. this.videoElement.removeEventListener("durationchange", Video.onDurationchange);
  555. this.videoElement.removeEventListener("emptied", Video.onEmptied);
  556. this.videoElement.removeEventListener("error", Video.onError);
  557. this.videoElement.removeEventListener("loadeddata", Video.onLoadeddata);
  558. this.videoElement.removeEventListener("loadedmetadata", Video.onLoadedmetadata);
  559. this.videoElement.removeEventListener("loadstart", Video.onLoadstart);
  560. this.videoElement.removeEventListener("pause", Video.onPause);
  561. this.videoElement.removeEventListener("play", Video.onPlay);
  562. this.videoElement.removeEventListener("playing", Video.onPlaying);
  563. this.videoElement.removeEventListener("progress", Video.onProgress);
  564. this.videoElement.removeEventListener("ratechange", Video.onRatechange);
  565. this.videoElement.removeEventListener("seeked", Video.onSeeked);
  566. this.videoElement.removeEventListener("seeking", Video.onSeeking);
  567. this.videoElement.removeEventListener("stalled", Video.onStalled);
  568. this.videoElement.removeEventListener("suspend", Video.onSuspend);
  569. this.videoElement.removeEventListener("timeupdate", Video.onTimeupdate);
  570. this.videoElement.removeEventListener("volumechange", Video.onVolumechange);
  571. this.videoElement.removeEventListener("waiting", Video.onWaiting);
  572. this.videoElement.removeEventListener("ended", this.onPlayComplete);
  573. this.pause();
  574. this.videoElement.layaTarget = null;
  575. this.videoElement = null;
  576. this.htmlVideo.destroy();
  577. }
  578. syncVideoPosition() {
  579. var stage = Laya.ILaya.stage;
  580. var rec;
  581. rec = Laya.ILaya.Utils.getGlobalPosAndScale(this);
  582. var a = stage._canvasTransform.a, d = stage._canvasTransform.d;
  583. var x = rec.x * stage.clientScaleX * a + stage.offset.x;
  584. var y = rec.y * stage.clientScaleY * d + stage.offset.y;
  585. this.videoElement.style.left = x + 'px';
  586. this.videoElement.style.top = y + 'px';
  587. this.videoElement.width = this.width / Laya.ILaya.Browser.pixelRatio;
  588. this.videoElement.height = this.height / Laya.ILaya.Browser.pixelRatio;
  589. }
  590. }
  591. Video.MP4 = 1;
  592. Video.OGG = 2;
  593. Video.CAMERA = 4;
  594. Video.WEBM = 8;
  595. Video.SUPPORT_PROBABLY = "probably";
  596. Video.SUPPORT_MAYBY = "maybe";
  597. Video.SUPPORT_NO = "";
  598. class Gyroscope extends Laya.EventDispatcher {
  599. constructor(singleton) {
  600. super();
  601. this.onDeviceOrientationChange = this.onDeviceOrientationChange.bind(this);
  602. }
  603. static get instance() {
  604. Gyroscope._instance = Gyroscope._instance || new Gyroscope(0);
  605. return Gyroscope._instance;
  606. }
  607. on(type, caller, listener, args = null) {
  608. super.on(type, caller, listener, args);
  609. Laya.ILaya.Browser.window.addEventListener('deviceorientation', this.onDeviceOrientationChange);
  610. return this;
  611. }
  612. off(type, caller, listener, onceOnly = false) {
  613. if (!this.hasListener(type))
  614. Laya.ILaya.Browser.window.removeEventListener('deviceorientation', this.onDeviceOrientationChange);
  615. return super.off(type, caller, listener, onceOnly);
  616. }
  617. onDeviceOrientationChange(e) {
  618. Gyroscope.info.alpha = e.alpha;
  619. Gyroscope.info.beta = e.beta;
  620. Gyroscope.info.gamma = e.gamma;
  621. if (e.webkitCompassHeading) {
  622. Gyroscope.info.alpha = e.webkitCompassHeading * -1;
  623. Gyroscope.info.compassAccuracy = e.webkitCompassAccuracy;
  624. }
  625. this.event(Laya.Event.CHANGE, [e.absolute, Gyroscope.info]);
  626. }
  627. }
  628. Gyroscope.info = new RotationInfo();
  629. exports.AccelerationInfo = AccelerationInfo;
  630. exports.Accelerator = Accelerator;
  631. exports.Geolocation = Geolocation;
  632. exports.GeolocationInfo = GeolocationInfo;
  633. exports.Gyroscope = Gyroscope;
  634. exports.HtmlVideo = HtmlVideo;
  635. exports.Media = Media;
  636. exports.RotationInfo = RotationInfo;
  637. exports.Shake = Shake;
  638. exports.Video = Video;
  639. exports.WebGLVideo = WebGLVideo;
  640. }(window.Laya = window.Laya|| {}, Laya));