laya.device.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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._w = 0;
  214. this._h = 0;
  215. this._width = 1;
  216. this._height = 1;
  217. this.createDomElement();
  218. }
  219. createDomElement() {
  220. this._source = this.video = Laya.ILaya.Browser.createElement("video");
  221. var style = this.video.style;
  222. style.position = 'absolute';
  223. style.top = '0px';
  224. style.left = '0px';
  225. this.video.addEventListener("loadedmetadata", () => {
  226. this._w = this.video.videoWidth;
  227. this._h = this.video.videoHeight;
  228. });
  229. }
  230. setSource(url, extension) {
  231. while (this.video.childElementCount)
  232. this.video.firstChild.remove();
  233. if (extension & 1)
  234. this.appendSource(url, "video/mp4");
  235. if (extension & 2)
  236. this.appendSource(url + ".ogg", "video/ogg");
  237. }
  238. appendSource(source, type) {
  239. var sourceElement = Laya.ILaya.Browser.createElement("source");
  240. sourceElement.src = source;
  241. sourceElement.type = type;
  242. this.video.appendChild(sourceElement);
  243. }
  244. getVideo() {
  245. return this.video;
  246. }
  247. _getSource() {
  248. return this._source;
  249. }
  250. destroy() {
  251. super.destroy();
  252. var isConchApp = Laya.ILaya.Render.isConchApp;
  253. if (isConchApp) {
  254. this.video._destroy();
  255. }
  256. }
  257. }
  258. HtmlVideo.create = function () {
  259. return new HtmlVideo();
  260. };
  261. class Media {
  262. constructor() {
  263. }
  264. static supported() {
  265. return !!Laya.ILaya.Browser.window.navigator.getUserMedia;
  266. }
  267. static getMedia(options, onSuccess, onError) {
  268. if (Laya.ILaya.Browser.window.navigator.getUserMedia) {
  269. Laya.ILaya.Browser.window.navigator.getUserMedia(options, function (stream) {
  270. onSuccess.runWith(Laya.ILaya.Browser.window.URL.createObjectURL(stream));
  271. }, function (err) {
  272. onError.runWith(err);
  273. });
  274. }
  275. }
  276. }
  277. class WebGLVideo extends HtmlVideo {
  278. constructor() {
  279. super();
  280. var gl = Laya.LayaGL.instance;
  281. if (!Laya.ILaya.Render.isConchApp && Laya.ILaya.Browser.onIPhone)
  282. return;
  283. this.gl = Laya.ILaya.Render.isConchApp ? window.LayaGLContext.instance : Laya.WebGLContext.mainContext;
  284. this._source = this.gl.createTexture();
  285. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, this._source);
  286. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  287. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  288. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  289. this.gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  290. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, null);
  291. }
  292. updateTexture() {
  293. if (!Laya.ILaya.Render.isConchApp && Laya.ILaya.Browser.onIPhone)
  294. return;
  295. var gl = Laya.LayaGL.instance;
  296. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, this._source);
  297. this.gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.video);
  298. WebGLVideo.curBindSource = this._source;
  299. }
  300. get _glTexture() {
  301. return this._source;
  302. }
  303. destroy() {
  304. if (this._source) {
  305. this.gl = Laya.ILaya.Render.isConchApp ? window.LayaGLContext.instance : Laya.WebGLContext.mainContext;
  306. if (WebGLVideo.curBindSource == this._source) {
  307. Laya.WebGLContext.bindTexture(this.gl, this.gl.TEXTURE_2D, null);
  308. WebGLVideo.curBindSource = null;
  309. }
  310. this.gl.deleteTexture(this._source);
  311. }
  312. super.destroy();
  313. }
  314. }
  315. class Video extends Laya.Sprite {
  316. constructor(width = 320, height = 240) {
  317. super();
  318. this.htmlVideo = new WebGLVideo();
  319. this.videoElement = this.htmlVideo.getVideo();
  320. this.videoElement.layaTarget = this;
  321. this.internalTexture = new Laya.Texture(this.htmlVideo);
  322. this.videoElement.addEventListener("abort", Video.onAbort);
  323. this.videoElement.addEventListener("canplay", Video.onCanplay);
  324. this.videoElement.addEventListener("canplaythrough", Video.onCanplaythrough);
  325. this.videoElement.addEventListener("durationchange", Video.onDurationchange);
  326. this.videoElement.addEventListener("emptied", Video.onEmptied);
  327. this.videoElement.addEventListener("error", Video.onError);
  328. this.videoElement.addEventListener("loadeddata", Video.onLoadeddata);
  329. this.videoElement.addEventListener("loadedmetadata", Video.onLoadedmetadata);
  330. this.videoElement.addEventListener("loadstart", Video.onLoadstart);
  331. this.videoElement.addEventListener("pause", Video.onPause);
  332. this.videoElement.addEventListener("play", Video.onPlay);
  333. this.videoElement.addEventListener("playing", Video.onPlaying);
  334. this.videoElement.addEventListener("progress", Video.onProgress);
  335. this.videoElement.addEventListener("ratechange", Video.onRatechange);
  336. this.videoElement.addEventListener("seeked", Video.onSeeked);
  337. this.videoElement.addEventListener("seeking", Video.onSeeking);
  338. this.videoElement.addEventListener("stalled", Video.onStalled);
  339. this.videoElement.addEventListener("suspend", Video.onSuspend);
  340. this.videoElement.addEventListener("timeupdate", Video.onTimeupdate);
  341. this.videoElement.addEventListener("volumechange", Video.onVolumechange);
  342. this.videoElement.addEventListener("waiting", Video.onWaiting);
  343. this.videoElement.addEventListener("ended", this.onPlayComplete['bind'](this));
  344. this.size(width, height);
  345. if (Laya.ILaya.Browser.onMobile) {
  346. this.onDocumentClick = this.onDocumentClick.bind(this);
  347. Laya.ILaya.Browser.document.addEventListener("touchend", this.onDocumentClick);
  348. }
  349. }
  350. static onAbort(e) { e.target.layaTarget.event("abort"); }
  351. static onCanplay(e) { e.target.layaTarget.event("canplay"); }
  352. static onCanplaythrough(e) { e.target.layaTarget.event("canplaythrough"); }
  353. static onDurationchange(e) { e.target.layaTarget.event("durationchange"); }
  354. static onEmptied(e) { e.target.layaTarget.event("emptied"); }
  355. static onError(e) { e.target.layaTarget.event("error"); }
  356. static onLoadeddata(e) { e.target.layaTarget.event("loadeddata"); }
  357. static onLoadedmetadata(e) { e.target.layaTarget.event("loadedmetadata"); }
  358. static onLoadstart(e) { e.target.layaTarget.event("loadstart"); }
  359. static onPause(e) { e.target.layaTarget.event("pause"); }
  360. static onPlay(e) { e.target.layaTarget.event("play"); }
  361. static onPlaying(e) { e.target.layaTarget.event("playing"); }
  362. static onProgress(e) { e.target.layaTarget.event("progress"); }
  363. static onRatechange(e) { e.target.layaTarget.event("ratechange"); }
  364. static onSeeked(e) { e.target.layaTarget.event("seeked"); }
  365. static onSeeking(e) { e.target.layaTarget.event("seeking"); }
  366. static onStalled(e) { e.target.layaTarget.event("stalled"); }
  367. static onSuspend(e) { e.target.layaTarget.event("suspend"); }
  368. static onTimeupdate(e) { e.target.layaTarget.event("timeupdate"); }
  369. static onVolumechange(e) { e.target.layaTarget.event("volumechange"); }
  370. static onWaiting(e) { e.target.layaTarget.event("waiting"); }
  371. onPlayComplete(e) {
  372. if (!Laya.ILaya.Render.isConchApp || !this.videoElement || !this.videoElement.loop)
  373. Laya.ILaya.timer.clear(this, this.renderCanvas);
  374. this.event("ended");
  375. }
  376. load(url) {
  377. if (url.indexOf("blob:") == 0)
  378. this.videoElement.src = url;
  379. else
  380. this.htmlVideo.setSource(url, 1);
  381. }
  382. play() {
  383. this.videoElement.play();
  384. Laya.ILaya.timer.frameLoop(1, this, this.renderCanvas);
  385. }
  386. pause() {
  387. this.videoElement.pause();
  388. Laya.ILaya.timer.clear(this, this.renderCanvas);
  389. }
  390. reload() {
  391. this.videoElement.load();
  392. }
  393. canPlayType(type) {
  394. var typeString;
  395. switch (type) {
  396. case 1:
  397. typeString = "video/mp4";
  398. break;
  399. case 2:
  400. typeString = "video/ogg";
  401. break;
  402. case 8:
  403. typeString = "video/webm";
  404. break;
  405. }
  406. return this.videoElement.canPlayType(typeString);
  407. }
  408. renderCanvas() {
  409. if (this.readyState === 0)
  410. return;
  411. this.htmlVideo['updateTexture']();
  412. this.graphics.clear();
  413. this.graphics.drawTexture(this.internalTexture, 0, 0, this.width, this.height);
  414. }
  415. onDocumentClick() {
  416. this.videoElement.play();
  417. this.videoElement.pause();
  418. Laya.ILaya.Browser.document.removeEventListener("touchend", this.onDocumentClick);
  419. }
  420. get buffered() {
  421. return this.videoElement.buffered;
  422. }
  423. get currentSrc() {
  424. return this.videoElement.currentSrc;
  425. }
  426. get currentTime() {
  427. return this.videoElement.currentTime;
  428. }
  429. set currentTime(value) {
  430. this.videoElement.currentTime = value;
  431. this.renderCanvas();
  432. }
  433. set volume(value) {
  434. this.videoElement.volume = value;
  435. }
  436. get volume() {
  437. return this.videoElement.volume;
  438. }
  439. get readyState() {
  440. return this.videoElement.readyState;
  441. }
  442. get videoWidth() {
  443. return this.videoElement.videoWidth;
  444. }
  445. get videoHeight() {
  446. return this.videoElement.videoHeight;
  447. }
  448. get duration() {
  449. return this.videoElement.duration;
  450. }
  451. get ended() {
  452. return this.videoElement.ended;
  453. }
  454. get error() {
  455. return this.videoElement.error;
  456. }
  457. get loop() {
  458. return this.videoElement.loop;
  459. }
  460. set loop(value) {
  461. this.videoElement.loop = value;
  462. }
  463. set x(val) {
  464. super.x = val;
  465. if (Laya.ILaya.Render.isConchApp) {
  466. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  467. this.videoElement.style.left = transform.x;
  468. }
  469. }
  470. get x() {
  471. return super.x;
  472. }
  473. set y(val) {
  474. super.y = val;
  475. if (Laya.ILaya.Render.isConchApp) {
  476. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  477. this.videoElement.style.top = transform.y;
  478. }
  479. }
  480. get y() {
  481. return super.y;
  482. }
  483. get playbackRate() {
  484. return this.videoElement.playbackRate;
  485. }
  486. set playbackRate(value) {
  487. this.videoElement.playbackRate = value;
  488. }
  489. get muted() {
  490. return this.videoElement.muted;
  491. }
  492. set muted(value) {
  493. this.videoElement.muted = value;
  494. }
  495. get paused() {
  496. return this.videoElement.paused;
  497. }
  498. get preload() {
  499. return this.videoElement.preload;
  500. }
  501. set preload(value) {
  502. this.videoElement.preload = value;
  503. }
  504. get seekable() {
  505. return this.videoElement.seekable;
  506. }
  507. get seeking() {
  508. return this.videoElement.seeking;
  509. }
  510. size(width, height) {
  511. super.size(width, height);
  512. if (Laya.ILaya.Render.isConchApp) {
  513. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  514. this.videoElement.width = width * transform.scaleX;
  515. }
  516. else {
  517. this.videoElement.width = width / Laya.ILaya.Browser.pixelRatio;
  518. }
  519. if (this.paused)
  520. this.renderCanvas();
  521. return this;
  522. }
  523. set width(value) {
  524. if (Laya.ILaya.Render.isConchApp) {
  525. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  526. this.videoElement.width = value * transform.scaleX;
  527. }
  528. else {
  529. this.videoElement.width = this.width / Laya.ILaya.Browser.pixelRatio;
  530. }
  531. super.width = value;
  532. if (this.paused)
  533. this.renderCanvas();
  534. }
  535. get width() {
  536. return super.width;
  537. }
  538. set height(value) {
  539. if (Laya.ILaya.Render.isConchApp) {
  540. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  541. this.videoElement.height = value * transform.scaleY;
  542. }
  543. else {
  544. this.videoElement.height = this.height / Laya.ILaya.Browser.pixelRatio;
  545. }
  546. super.height = value;
  547. }
  548. get height() {
  549. return super.height;
  550. }
  551. destroy(detroyChildren = true) {
  552. super.destroy(detroyChildren);
  553. this.videoElement.removeEventListener("abort", Video.onAbort);
  554. this.videoElement.removeEventListener("canplay", Video.onCanplay);
  555. this.videoElement.removeEventListener("canplaythrough", Video.onCanplaythrough);
  556. this.videoElement.removeEventListener("durationchange", Video.onDurationchange);
  557. this.videoElement.removeEventListener("emptied", Video.onEmptied);
  558. this.videoElement.removeEventListener("error", Video.onError);
  559. this.videoElement.removeEventListener("loadeddata", Video.onLoadeddata);
  560. this.videoElement.removeEventListener("loadedmetadata", Video.onLoadedmetadata);
  561. this.videoElement.removeEventListener("loadstart", Video.onLoadstart);
  562. this.videoElement.removeEventListener("pause", Video.onPause);
  563. this.videoElement.removeEventListener("play", Video.onPlay);
  564. this.videoElement.removeEventListener("playing", Video.onPlaying);
  565. this.videoElement.removeEventListener("progress", Video.onProgress);
  566. this.videoElement.removeEventListener("ratechange", Video.onRatechange);
  567. this.videoElement.removeEventListener("seeked", Video.onSeeked);
  568. this.videoElement.removeEventListener("seeking", Video.onSeeking);
  569. this.videoElement.removeEventListener("stalled", Video.onStalled);
  570. this.videoElement.removeEventListener("suspend", Video.onSuspend);
  571. this.videoElement.removeEventListener("timeupdate", Video.onTimeupdate);
  572. this.videoElement.removeEventListener("volumechange", Video.onVolumechange);
  573. this.videoElement.removeEventListener("waiting", Video.onWaiting);
  574. this.videoElement.removeEventListener("ended", this.onPlayComplete);
  575. this.pause();
  576. this.videoElement.layaTarget = null;
  577. this.videoElement = null;
  578. this.htmlVideo.destroy();
  579. }
  580. syncVideoPosition() {
  581. var stage = Laya.ILaya.stage;
  582. var rec;
  583. rec = Laya.ILaya.Utils.getGlobalPosAndScale(this);
  584. var a = stage._canvasTransform.a, d = stage._canvasTransform.d;
  585. var x = rec.x * stage.clientScaleX * a + stage.offset.x;
  586. var y = rec.y * stage.clientScaleY * d + stage.offset.y;
  587. this.videoElement.style.left = x + 'px';
  588. this.videoElement.style.top = y + 'px';
  589. this.videoElement.width = this.width / Laya.ILaya.Browser.pixelRatio;
  590. this.videoElement.height = this.height / Laya.ILaya.Browser.pixelRatio;
  591. }
  592. }
  593. Video.MP4 = 1;
  594. Video.OGG = 2;
  595. Video.CAMERA = 4;
  596. Video.WEBM = 8;
  597. Video.SUPPORT_PROBABLY = "probably";
  598. Video.SUPPORT_MAYBY = "maybe";
  599. Video.SUPPORT_NO = "";
  600. class Gyroscope extends Laya.EventDispatcher {
  601. constructor(singleton) {
  602. super();
  603. this.onDeviceOrientationChange = this.onDeviceOrientationChange.bind(this);
  604. }
  605. static get instance() {
  606. Gyroscope._instance = Gyroscope._instance || new Gyroscope(0);
  607. return Gyroscope._instance;
  608. }
  609. on(type, caller, listener, args = null) {
  610. super.on(type, caller, listener, args);
  611. Laya.ILaya.Browser.window.addEventListener('deviceorientation', this.onDeviceOrientationChange);
  612. return this;
  613. }
  614. off(type, caller, listener, onceOnly = false) {
  615. if (!this.hasListener(type))
  616. Laya.ILaya.Browser.window.removeEventListener('deviceorientation', this.onDeviceOrientationChange);
  617. return super.off(type, caller, listener, onceOnly);
  618. }
  619. onDeviceOrientationChange(e) {
  620. Gyroscope.info.alpha = e.alpha;
  621. Gyroscope.info.beta = e.beta;
  622. Gyroscope.info.gamma = e.gamma;
  623. if (e.webkitCompassHeading) {
  624. Gyroscope.info.alpha = e.webkitCompassHeading * -1;
  625. Gyroscope.info.compassAccuracy = e.webkitCompassAccuracy;
  626. }
  627. this.event(Laya.Event.CHANGE, [e.absolute, Gyroscope.info]);
  628. }
  629. }
  630. Gyroscope.info = new RotationInfo();
  631. exports.AccelerationInfo = AccelerationInfo;
  632. exports.Accelerator = Accelerator;
  633. exports.Geolocation = Geolocation;
  634. exports.GeolocationInfo = GeolocationInfo;
  635. exports.Gyroscope = Gyroscope;
  636. exports.HtmlVideo = HtmlVideo;
  637. exports.Media = Media;
  638. exports.RotationInfo = RotationInfo;
  639. exports.Shake = Shake;
  640. exports.Video = Video;
  641. exports.WebGLVideo = WebGLVideo;
  642. }(window.Laya = window.Laya|| {}, Laya));