laya.device.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 = 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. 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. var gl = Laya.LayaGL.instance;
  292. Laya.WebGLContext.bindTexture(this.gl, gl.TEXTURE_2D, this._source);
  293. this.gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this.video);
  294. WebGLVideo.curBindSource = this._source;
  295. }
  296. get _glTexture() {
  297. return this._source;
  298. }
  299. destroy() {
  300. if (this._source) {
  301. this.gl = Laya.ILaya.Render.isConchApp ? window.LayaGLContext.instance : Laya.WebGLContext.mainContext;
  302. if (this.gl) {
  303. if (WebGLVideo.curBindSource == this._source) {
  304. Laya.WebGLContext.bindTexture(this.gl, this.gl.TEXTURE_2D, null);
  305. WebGLVideo.curBindSource = null;
  306. }
  307. this.gl.deleteTexture(this._source);
  308. }
  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.videoElement["x5-playsInline"] = true;
  345. this.videoElement["x5-playsinline"] = true;
  346. this.videoElement.x5PlaysInline = true;
  347. this.videoElement.playsInline = true;
  348. this.videoElement["webkit-playsInline"] = true;
  349. this.videoElement["webkit-playsinline"] = true;
  350. this.videoElement.webkitPlaysInline = true;
  351. this.videoElement.playsinline = true;
  352. this.videoElement.style.playsInline = true;
  353. this.videoElement.crossOrigin = "anonymous";
  354. this.videoElement.setAttribute('crossorigin', "anonymous");
  355. this.videoElement.setAttribute('playsinline', 'true');
  356. this.videoElement.setAttribute('x5-playsinline', 'true');
  357. this.videoElement.setAttribute('webkit-playsinline', 'true');
  358. this.videoElement.autoplay = true;
  359. this._clickhandle = this.onDocumentClick.bind(this);
  360. Laya.ILaya.Browser.document.addEventListener("touchend", this._clickhandle);
  361. }
  362. }
  363. static onAbort(e) { e.target.layaTarget.event("abort"); }
  364. static onCanplay(e) { e.target.layaTarget.event("canplay"); }
  365. static onCanplaythrough(e) { e.target.layaTarget.event("canplaythrough"); }
  366. static onDurationchange(e) { e.target.layaTarget.event("durationchange"); }
  367. static onEmptied(e) { e.target.layaTarget.event("emptied"); }
  368. static onError(e) { e.target.layaTarget.event("error"); }
  369. static onLoadeddata(e) { e.target.layaTarget.event("loadeddata"); }
  370. static onLoadedmetadata(e) { e.target.layaTarget.event("loadedmetadata"); }
  371. static onLoadstart(e) { e.target.layaTarget.event("loadstart"); }
  372. static onPause(e) { e.target.layaTarget.event("pause"); }
  373. static onPlay(e) { e.target.layaTarget.event("play"); }
  374. static onPlaying(e) { e.target.layaTarget.event("playing"); }
  375. static onProgress(e) { e.target.layaTarget.event("progress"); }
  376. static onRatechange(e) { e.target.layaTarget.event("ratechange"); }
  377. static onSeeked(e) { e.target.layaTarget.event("seeked"); }
  378. static onSeeking(e) { e.target.layaTarget.event("seeking"); }
  379. static onStalled(e) { e.target.layaTarget.event("stalled"); }
  380. static onSuspend(e) { e.target.layaTarget.event("suspend"); }
  381. static onTimeupdate(e) { e.target.layaTarget.event("timeupdate"); }
  382. static onVolumechange(e) { e.target.layaTarget.event("volumechange"); }
  383. static onWaiting(e) { e.target.layaTarget.event("waiting"); }
  384. onPlayComplete(e) {
  385. if (!Laya.ILaya.Render.isConchApp || !this.videoElement || !this.videoElement.loop)
  386. Laya.ILaya.timer.clear(this, this.renderCanvas);
  387. this.event("ended");
  388. }
  389. load(url) {
  390. if (url.indexOf("blob:") == 0)
  391. this.videoElement.src = url;
  392. else
  393. this.htmlVideo.setSource(url, 1);
  394. }
  395. play() {
  396. this.videoElement.play();
  397. Laya.ILaya.timer.frameLoop(1, this, this.renderCanvas);
  398. }
  399. pause() {
  400. this.videoElement.pause();
  401. Laya.ILaya.timer.clear(this, this.renderCanvas);
  402. }
  403. reload() {
  404. this.videoElement.load();
  405. }
  406. canPlayType(type) {
  407. var typeString;
  408. switch (type) {
  409. case 1:
  410. typeString = "video/mp4";
  411. break;
  412. case 2:
  413. typeString = "video/ogg";
  414. break;
  415. case 8:
  416. typeString = "video/webm";
  417. break;
  418. }
  419. return this.videoElement.canPlayType(typeString);
  420. }
  421. renderCanvas() {
  422. if (this.readyState === 0)
  423. return;
  424. this.htmlVideo['updateTexture']();
  425. this.graphics.clear();
  426. this.graphics.drawTexture(this.internalTexture, 0, 0, this.width, this.height);
  427. }
  428. onDocumentClick() {
  429. if (!this.videoElement || this.videoElement != 0)
  430. return;
  431. if (Laya.Browser.onIOS) {
  432. this.videoElement.load();
  433. }
  434. else {
  435. this.videoElement.play();
  436. this.videoElement.pause();
  437. }
  438. Laya.ILaya.Browser.document.removeEventListener("touchend", this._clickhandle);
  439. }
  440. get buffered() {
  441. return this.videoElement.buffered;
  442. }
  443. get currentSrc() {
  444. return this.videoElement.currentSrc;
  445. }
  446. get currentTime() {
  447. return this.videoElement.currentTime;
  448. }
  449. set currentTime(value) {
  450. this.videoElement.currentTime = value;
  451. this.renderCanvas();
  452. }
  453. set volume(value) {
  454. this.videoElement.volume = value;
  455. }
  456. get volume() {
  457. return this.videoElement.volume;
  458. }
  459. get readyState() {
  460. return this.videoElement.readyState;
  461. }
  462. get videoWidth() {
  463. return this.videoElement.videoWidth;
  464. }
  465. get videoHeight() {
  466. return this.videoElement.videoHeight;
  467. }
  468. get duration() {
  469. return this.videoElement.duration;
  470. }
  471. get ended() {
  472. return this.videoElement.ended;
  473. }
  474. get error() {
  475. return this.videoElement.error;
  476. }
  477. get loop() {
  478. return this.videoElement.loop;
  479. }
  480. set loop(value) {
  481. this.videoElement.loop = value;
  482. }
  483. set x(val) {
  484. super.x = val;
  485. if (Laya.ILaya.Render.isConchApp) {
  486. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  487. this.videoElement.style.left = transform.x;
  488. }
  489. }
  490. get x() {
  491. return super.x;
  492. }
  493. set y(val) {
  494. super.y = val;
  495. if (Laya.ILaya.Render.isConchApp) {
  496. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  497. this.videoElement.style.top = transform.y;
  498. }
  499. }
  500. get y() {
  501. return super.y;
  502. }
  503. get playbackRate() {
  504. return this.videoElement.playbackRate;
  505. }
  506. set playbackRate(value) {
  507. this.videoElement.playbackRate = value;
  508. }
  509. get muted() {
  510. return this.videoElement.muted;
  511. }
  512. set muted(value) {
  513. this.videoElement.muted = value;
  514. }
  515. get paused() {
  516. return this.videoElement.paused;
  517. }
  518. get preload() {
  519. return this.videoElement.preload;
  520. }
  521. set preload(value) {
  522. this.videoElement.preload = value;
  523. }
  524. get seekable() {
  525. return this.videoElement.seekable;
  526. }
  527. get seeking() {
  528. return this.videoElement.seeking;
  529. }
  530. size(width, height) {
  531. super.size(width, height);
  532. if (Laya.ILaya.Render.isConchApp) {
  533. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  534. this.videoElement.width = width * transform.scaleX;
  535. }
  536. else {
  537. this.videoElement.width = width / Laya.ILaya.Browser.pixelRatio;
  538. this.videoElement.height = height / Laya.Browser.pixelRatio;
  539. }
  540. if (this.paused)
  541. this.renderCanvas();
  542. return this;
  543. }
  544. set width(value) {
  545. if (Laya.ILaya.Render.isConchApp) {
  546. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  547. this.videoElement.width = value * transform.scaleX;
  548. }
  549. else {
  550. this.videoElement.width = this.width / Laya.ILaya.Browser.pixelRatio;
  551. }
  552. super.width = value;
  553. if (this.paused)
  554. this.renderCanvas();
  555. }
  556. get width() {
  557. return super.width;
  558. }
  559. set height(value) {
  560. if (Laya.ILaya.Render.isConchApp) {
  561. var transform = Laya.ILaya.Utils.getTransformRelativeToWindow(this, 0, 0);
  562. this.videoElement.height = value * transform.scaleY;
  563. }
  564. else {
  565. this.videoElement.height = this.height / Laya.ILaya.Browser.pixelRatio;
  566. }
  567. super.height = value;
  568. }
  569. get height() {
  570. return super.height;
  571. }
  572. destroy(detroyChildren = true) {
  573. super.destroy(detroyChildren);
  574. this.videoElement.removeEventListener("abort", Video.onAbort);
  575. this.videoElement.removeEventListener("canplay", Video.onCanplay);
  576. this.videoElement.removeEventListener("canplaythrough", Video.onCanplaythrough);
  577. this.videoElement.removeEventListener("durationchange", Video.onDurationchange);
  578. this.videoElement.removeEventListener("emptied", Video.onEmptied);
  579. this.videoElement.removeEventListener("error", Video.onError);
  580. this.videoElement.removeEventListener("loadeddata", Video.onLoadeddata);
  581. this.videoElement.removeEventListener("loadedmetadata", Video.onLoadedmetadata);
  582. this.videoElement.removeEventListener("loadstart", Video.onLoadstart);
  583. this.videoElement.removeEventListener("pause", Video.onPause);
  584. this.videoElement.removeEventListener("play", Video.onPlay);
  585. this.videoElement.removeEventListener("playing", Video.onPlaying);
  586. this.videoElement.removeEventListener("progress", Video.onProgress);
  587. this.videoElement.removeEventListener("ratechange", Video.onRatechange);
  588. this.videoElement.removeEventListener("seeked", Video.onSeeked);
  589. this.videoElement.removeEventListener("seeking", Video.onSeeking);
  590. this.videoElement.removeEventListener("stalled", Video.onStalled);
  591. this.videoElement.removeEventListener("suspend", Video.onSuspend);
  592. this.videoElement.removeEventListener("timeupdate", Video.onTimeupdate);
  593. this.videoElement.removeEventListener("volumechange", Video.onVolumechange);
  594. this.videoElement.removeEventListener("waiting", Video.onWaiting);
  595. this.videoElement.removeEventListener("ended", this.onPlayComplete);
  596. this.pause();
  597. this.videoElement.layaTarget = null;
  598. this.videoElement = null;
  599. this.htmlVideo.destroy();
  600. }
  601. syncVideoPosition() {
  602. var stage = Laya.ILaya.stage;
  603. var rec;
  604. rec = Laya.ILaya.Utils.getGlobalPosAndScale(this);
  605. var a = stage._canvasTransform.a, d = stage._canvasTransform.d;
  606. var x = rec.x * stage.clientScaleX * a + stage.offset.x;
  607. var y = rec.y * stage.clientScaleY * d + stage.offset.y;
  608. this.videoElement.style.left = x + 'px';
  609. this.videoElement.style.top = y + 'px';
  610. this.videoElement.width = this.width / Laya.ILaya.Browser.pixelRatio;
  611. this.videoElement.height = this.height / Laya.ILaya.Browser.pixelRatio;
  612. }
  613. }
  614. Video.MP4 = 1;
  615. Video.OGG = 2;
  616. Video.CAMERA = 4;
  617. Video.WEBM = 8;
  618. Video.SUPPORT_PROBABLY = "probably";
  619. Video.SUPPORT_MAYBY = "maybe";
  620. Video.SUPPORT_NO = "";
  621. class Gyroscope extends Laya.EventDispatcher {
  622. constructor(singleton) {
  623. super();
  624. this.onDeviceOrientationChange = this.onDeviceOrientationChange.bind(this);
  625. }
  626. static get instance() {
  627. Gyroscope._instance = Gyroscope._instance || new Gyroscope(0);
  628. return Gyroscope._instance;
  629. }
  630. on(type, caller, listener, args = null) {
  631. super.on(type, caller, listener, args);
  632. Laya.ILaya.Browser.window.addEventListener('deviceorientation', this.onDeviceOrientationChange);
  633. return this;
  634. }
  635. off(type, caller, listener, onceOnly = false) {
  636. if (!this.hasListener(type))
  637. Laya.ILaya.Browser.window.removeEventListener('deviceorientation', this.onDeviceOrientationChange);
  638. return super.off(type, caller, listener, onceOnly);
  639. }
  640. onDeviceOrientationChange(e) {
  641. Gyroscope.info.alpha = e.alpha;
  642. Gyroscope.info.beta = e.beta;
  643. Gyroscope.info.gamma = e.gamma;
  644. if (e.webkitCompassHeading) {
  645. Gyroscope.info.alpha = e.webkitCompassHeading * -1;
  646. Gyroscope.info.compassAccuracy = e.webkitCompassAccuracy;
  647. }
  648. this.event(Laya.Event.CHANGE, [e.absolute, Gyroscope.info]);
  649. }
  650. }
  651. Gyroscope.info = new RotationInfo();
  652. exports.AccelerationInfo = AccelerationInfo;
  653. exports.Accelerator = Accelerator;
  654. exports.Geolocation = Geolocation;
  655. exports.GeolocationInfo = GeolocationInfo;
  656. exports.Gyroscope = Gyroscope;
  657. exports.HtmlVideo = HtmlVideo;
  658. exports.Media = Media;
  659. exports.RotationInfo = RotationInfo;
  660. exports.Shake = Shake;
  661. exports.Video = Video;
  662. exports.WebGLVideo = WebGLVideo;
  663. }(window.Laya = window.Laya || {}, Laya));