vrview.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.VRView = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. 'use strict';
  3. var has = Object.prototype.hasOwnProperty;
  4. //
  5. // We store our EE objects in a plain object whose properties are event names.
  6. // If `Object.create(null)` is not supported we prefix the event names with a
  7. // `~` to make sure that the built-in object properties are not overridden or
  8. // used as an attack vector.
  9. // We also assume that `Object.create(null)` is available when the event name
  10. // is an ES6 Symbol.
  11. //
  12. var prefix = typeof Object.create !== 'function' ? '~' : false;
  13. /**
  14. * Representation of a single EventEmitter function.
  15. *
  16. * @param {Function} fn Event handler to be called.
  17. * @param {Mixed} context Context for function execution.
  18. * @param {Boolean} [once=false] Only emit once
  19. * @api private
  20. */
  21. function EE(fn, context, once) {
  22. this.fn = fn;
  23. this.context = context;
  24. this.once = once || false;
  25. }
  26. /**
  27. * Minimal EventEmitter interface that is molded against the Node.js
  28. * EventEmitter interface.
  29. *
  30. * @constructor
  31. * @api public
  32. */
  33. function EventEmitter() { /* Nothing to set */ }
  34. /**
  35. * Hold the assigned EventEmitters by name.
  36. *
  37. * @type {Object}
  38. * @private
  39. */
  40. EventEmitter.prototype._events = undefined;
  41. /**
  42. * Return an array listing the events for which the emitter has registered
  43. * listeners.
  44. *
  45. * @returns {Array}
  46. * @api public
  47. */
  48. EventEmitter.prototype.eventNames = function eventNames() {
  49. var events = this._events
  50. , names = []
  51. , name;
  52. if (!events) return names;
  53. for (name in events) {
  54. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  55. }
  56. if (Object.getOwnPropertySymbols) {
  57. return names.concat(Object.getOwnPropertySymbols(events));
  58. }
  59. return names;
  60. };
  61. /**
  62. * Return a list of assigned event listeners.
  63. *
  64. * @param {String} event The events that should be listed.
  65. * @param {Boolean} exists We only need to know if there are listeners.
  66. * @returns {Array|Boolean}
  67. * @api public
  68. */
  69. EventEmitter.prototype.listeners = function listeners(event, exists) {
  70. var evt = prefix ? prefix + event : event
  71. , available = this._events && this._events[evt];
  72. if (exists) return !!available;
  73. if (!available) return [];
  74. if (available.fn) return [available.fn];
  75. for (var i = 0, l = available.length, ee = new Array(l); i < l; i++) {
  76. ee[i] = available[i].fn;
  77. }
  78. return ee;
  79. };
  80. /**
  81. * Emit an event to all registered event listeners.
  82. *
  83. * @param {String} event The name of the event.
  84. * @returns {Boolean} Indication if we've emitted an event.
  85. * @api public
  86. */
  87. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  88. var evt = prefix ? prefix + event : event;
  89. if (!this._events || !this._events[evt]) return false;
  90. var listeners = this._events[evt]
  91. , len = arguments.length
  92. , args
  93. , i;
  94. if ('function' === typeof listeners.fn) {
  95. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  96. switch (len) {
  97. case 1: return listeners.fn.call(listeners.context), true;
  98. case 2: return listeners.fn.call(listeners.context, a1), true;
  99. case 3: return listeners.fn.call(listeners.context, a1, a2), true;
  100. case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
  101. case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  102. case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  103. }
  104. for (i = 1, args = new Array(len -1); i < len; i++) {
  105. args[i - 1] = arguments[i];
  106. }
  107. listeners.fn.apply(listeners.context, args);
  108. } else {
  109. var length = listeners.length
  110. , j;
  111. for (i = 0; i < length; i++) {
  112. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  113. switch (len) {
  114. case 1: listeners[i].fn.call(listeners[i].context); break;
  115. case 2: listeners[i].fn.call(listeners[i].context, a1); break;
  116. case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
  117. default:
  118. if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
  119. args[j - 1] = arguments[j];
  120. }
  121. listeners[i].fn.apply(listeners[i].context, args);
  122. }
  123. }
  124. }
  125. return true;
  126. };
  127. /**
  128. * Register a new EventListener for the given event.
  129. *
  130. * @param {String} event Name of the event.
  131. * @param {Function} fn Callback function.
  132. * @param {Mixed} [context=this] The context of the function.
  133. * @api public
  134. */
  135. EventEmitter.prototype.on = function on(event, fn, context) {
  136. var listener = new EE(fn, context || this)
  137. , evt = prefix ? prefix + event : event;
  138. if (!this._events) this._events = prefix ? {} : Object.create(null);
  139. if (!this._events[evt]) this._events[evt] = listener;
  140. else {
  141. if (!this._events[evt].fn) this._events[evt].push(listener);
  142. else this._events[evt] = [
  143. this._events[evt], listener
  144. ];
  145. }
  146. return this;
  147. };
  148. /**
  149. * Add an EventListener that's only called once.
  150. *
  151. * @param {String} event Name of the event.
  152. * @param {Function} fn Callback function.
  153. * @param {Mixed} [context=this] The context of the function.
  154. * @api public
  155. */
  156. EventEmitter.prototype.once = function once(event, fn, context) {
  157. var listener = new EE(fn, context || this, true)
  158. , evt = prefix ? prefix + event : event;
  159. if (!this._events) this._events = prefix ? {} : Object.create(null);
  160. if (!this._events[evt]) this._events[evt] = listener;
  161. else {
  162. if (!this._events[evt].fn) this._events[evt].push(listener);
  163. else this._events[evt] = [
  164. this._events[evt], listener
  165. ];
  166. }
  167. return this;
  168. };
  169. /**
  170. * Remove event listeners.
  171. *
  172. * @param {String} event The event we want to remove.
  173. * @param {Function} fn The listener that we need to find.
  174. * @param {Mixed} context Only remove listeners matching this context.
  175. * @param {Boolean} once Only remove once listeners.
  176. * @api public
  177. */
  178. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  179. var evt = prefix ? prefix + event : event;
  180. if (!this._events || !this._events[evt]) return this;
  181. var listeners = this._events[evt]
  182. , events = [];
  183. if (fn) {
  184. if (listeners.fn) {
  185. if (
  186. listeners.fn !== fn
  187. || (once && !listeners.once)
  188. || (context && listeners.context !== context)
  189. ) {
  190. events.push(listeners);
  191. }
  192. } else {
  193. for (var i = 0, length = listeners.length; i < length; i++) {
  194. if (
  195. listeners[i].fn !== fn
  196. || (once && !listeners[i].once)
  197. || (context && listeners[i].context !== context)
  198. ) {
  199. events.push(listeners[i]);
  200. }
  201. }
  202. }
  203. }
  204. //
  205. // Reset the array, or remove it completely if we have no more listeners.
  206. //
  207. if (events.length) {
  208. this._events[evt] = events.length === 1 ? events[0] : events;
  209. } else {
  210. delete this._events[evt];
  211. }
  212. return this;
  213. };
  214. /**
  215. * Remove all listeners or only the listeners for the specified event.
  216. *
  217. * @param {String} event The event want to remove all listeners for.
  218. * @api public
  219. */
  220. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  221. if (!this._events) return this;
  222. if (event) delete this._events[prefix ? prefix + event : event];
  223. else this._events = prefix ? {} : Object.create(null);
  224. return this;
  225. };
  226. //
  227. // Alias methods names because people roll like that.
  228. //
  229. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  230. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  231. //
  232. // This function doesn't apply anymore.
  233. //
  234. EventEmitter.prototype.setMaxListeners = function setMaxListeners() {
  235. return this;
  236. };
  237. //
  238. // Expose the prefix.
  239. //
  240. EventEmitter.prefixed = prefix;
  241. //
  242. // Expose the module.
  243. //
  244. if ('undefined' !== typeof module) {
  245. module.exports = EventEmitter;
  246. }
  247. },{}],2:[function(_dereq_,module,exports){
  248. /*
  249. * Copyright 2016 Google Inc. All Rights Reserved.
  250. * Licensed under the Apache License, Version 2.0 (the "License");
  251. * you may not use this file except in compliance with the License.
  252. * You may obtain a copy of the License at
  253. *
  254. * http://www.apache.org/licenses/LICENSE-2.0
  255. *
  256. * Unless required by applicable law or agreed to in writing, software
  257. * distributed under the License is distributed on an "AS IS" BASIS,
  258. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  259. * See the License for the specific language governing permissions and
  260. * limitations under the License.
  261. */
  262. var Message = _dereq_('../message');
  263. /**
  264. * Sends events to the embedded VR view IFrame via postMessage. Also handles
  265. * messages sent back from the IFrame:
  266. *
  267. * click: When a hotspot was clicked.
  268. * modechange: When the user changes viewing mode (VR|Fullscreen|etc).
  269. */
  270. function IFrameMessageSender(iframe) {
  271. if (!iframe) {
  272. console.error('No iframe specified');
  273. return;
  274. }
  275. this.iframe = iframe;
  276. // On iOS, if the iframe is across domains, also send DeviceMotion data.
  277. if (this.isIOS_()) {
  278. window.addEventListener('devicemotion', this.onDeviceMotion_.bind(this), false);
  279. }
  280. }
  281. /**
  282. * Sends a message to the associated VR View IFrame.
  283. */
  284. IFrameMessageSender.prototype.send = function(message) {
  285. var iframeWindow = this.iframe.contentWindow;
  286. iframeWindow.postMessage(message, '*');
  287. };
  288. IFrameMessageSender.prototype.onDeviceMotion_ = function(e) {
  289. var message = {
  290. type: Message.DEVICE_MOTION,
  291. deviceMotionEvent: this.cloneDeviceMotionEvent_(e)
  292. };
  293. this.send(message);
  294. };
  295. IFrameMessageSender.prototype.cloneDeviceMotionEvent_ = function(e) {
  296. return {
  297. acceleration: {
  298. x: e.acceleration.x,
  299. y: e.acceleration.y,
  300. z: e.acceleration.z,
  301. },
  302. accelerationIncludingGravity: {
  303. x: e.accelerationIncludingGravity.x,
  304. y: e.accelerationIncludingGravity.y,
  305. z: e.accelerationIncludingGravity.z,
  306. },
  307. rotationRate: {
  308. alpha: e.rotationRate.alpha,
  309. beta: e.rotationRate.beta,
  310. gamma: e.rotationRate.gamma,
  311. },
  312. interval: e.interval,
  313. timeStamp: e.timeStamp
  314. };
  315. };
  316. IFrameMessageSender.prototype.isIOS_ = function() {
  317. return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
  318. };
  319. module.exports = IFrameMessageSender;
  320. },{"../message":5}],3:[function(_dereq_,module,exports){
  321. /*
  322. * Copyright 2016 Google Inc. All Rights Reserved.
  323. * Licensed under the Apache License, Version 2.0 (the "License");
  324. * you may not use this file except in compliance with the License.
  325. * You may obtain a copy of the License at
  326. *
  327. * http://www.apache.org/licenses/LICENSE-2.0
  328. *
  329. * Unless required by applicable law or agreed to in writing, software
  330. * distributed under the License is distributed on an "AS IS" BASIS,
  331. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  332. * See the License for the specific language governing permissions and
  333. * limitations under the License.
  334. */
  335. var Player = _dereq_('./player');
  336. var VRView = {
  337. Player: Player
  338. };
  339. module.exports = VRView;
  340. },{"./player":4}],4:[function(_dereq_,module,exports){
  341. /*
  342. * Copyright 2016 Google Inc. All Rights Reserved.
  343. * Licensed under the Apache License, Version 2.0 (the "License");
  344. * you may not use this file except in compliance with the License.
  345. * You may obtain a copy of the License at
  346. *
  347. * http://www.apache.org/licenses/LICENSE-2.0
  348. *
  349. * Unless required by applicable law or agreed to in writing, software
  350. * distributed under the License is distributed on an "AS IS" BASIS,
  351. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  352. * See the License for the specific language governing permissions and
  353. * limitations under the License.
  354. */
  355. var EventEmitter = _dereq_('eventemitter3');
  356. var IFrameMessageSender = _dereq_('./iframe-message-sender');
  357. var Message = _dereq_('../message');
  358. var Util = _dereq_('../util');
  359. // Save the executing script. This will be used to calculate the embed URL.
  360. var CURRENT_SCRIPT_SRC = Util.getCurrentScript().src;
  361. var FAKE_FULLSCREEN_CLASS = 'vrview-fake-fullscreen';
  362. /**
  363. * Entry point for the VR View JS API.
  364. *
  365. * Emits the following events:
  366. * ready: When the player is loaded.
  367. * modechange: When the viewing mode changes (normal, fullscreen, VR).
  368. * click (id): When a hotspot is clicked.
  369. */
  370. function Player(selector, contentInfo) {
  371. // Create a VR View iframe depending on the parameters.
  372. var iframe = this.createIframe_(contentInfo);
  373. this.iframe = iframe;
  374. var parentEl = document.querySelector(selector);
  375. parentEl.appendChild(iframe);
  376. // Make a sender as well, for relying commands to the child IFrame.
  377. this.sender = new IFrameMessageSender(iframe);
  378. // Listen to messages from the IFrame.
  379. window.addEventListener('message', this.onMessage_.bind(this), false);
  380. // Expose a public .isPaused attribute.
  381. this.isPaused = false;
  382. // Expose a public .isMuted attribute.
  383. this.isMuted = false;
  384. if (typeof contentInfo.muted !== 'undefined') {
  385. this.isMuted = contentInfo.muted;
  386. }
  387. // Other public attributes
  388. this.currentTime = 0;
  389. this.duration = 0;
  390. this.volume = contentInfo.volume != undefined ? contentInfo.volume : 1;
  391. if (Util.isIOS()) {
  392. this.injectFullscreenStylesheet_();
  393. }
  394. }
  395. Player.prototype = new EventEmitter();
  396. /**
  397. * @param pitch {Number} The latitude of center, specified in degrees, between
  398. * -90 and 90, with 0 at the horizon.
  399. * @param yaw {Number} The longitude of center, specified in degrees, between
  400. * -180 and 180, with 0 at the image center.
  401. * @param radius {Number} The radius of the hotspot, specified in meters.
  402. * @param distance {Number} The distance of the hotspot from camera, specified
  403. * in meters.
  404. * @param hotspotId {String} The ID of the hotspot.
  405. */
  406. Player.prototype.addHotspot = function(hotspotId, params) {
  407. // TODO: Add validation to params.
  408. var data = {
  409. pitch: params.pitch,
  410. yaw: params.yaw,
  411. radius: params.radius,
  412. distance: params.distance,
  413. id: hotspotId
  414. };
  415. this.sender.send({type: Message.ADD_HOTSPOT, data: data});
  416. };
  417. Player.prototype.play = function() {
  418. this.sender.send({type: Message.PLAY});
  419. };
  420. Player.prototype.pause = function() {
  421. this.sender.send({type: Message.PAUSE});
  422. };
  423. /**
  424. * Equivalent of HTML5 setSrc().
  425. * @param {String} contentInfo
  426. */
  427. Player.prototype.setContent = function(contentInfo) {
  428. this.absolutifyPaths_(contentInfo);
  429. var data = {
  430. contentInfo: contentInfo
  431. };
  432. this.sender.send({type: Message.SET_CONTENT, data: data});
  433. };
  434. /**
  435. * Sets the software volume of the video. 0 is mute, 1 is max.
  436. */
  437. Player.prototype.setVolume = function(volumeLevel) {
  438. var data = {
  439. volumeLevel: volumeLevel
  440. };
  441. this.sender.send({type: Message.SET_VOLUME, data: data});
  442. };
  443. Player.prototype.getVolume = function() {
  444. return this.volume;
  445. };
  446. /**
  447. * Sets the mute state of the video element. true is muted, false is unmuted.
  448. */
  449. Player.prototype.mute = function(muteState) {
  450. var data = {
  451. muteState: muteState
  452. };
  453. this.sender.send({type: Message.MUTED, data: data});
  454. };
  455. /**
  456. * Set the current time of the media being played
  457. * @param {Number} time
  458. */
  459. Player.prototype.setCurrentTime = function(time) {
  460. var data = {
  461. currentTime: time
  462. };
  463. this.sender.send({type: Message.SET_CURRENT_TIME, data: data});
  464. };
  465. Player.prototype.getCurrentTime = function() {
  466. return this.currentTime;
  467. };
  468. Player.prototype.getDuration = function() {
  469. return this.duration;
  470. };
  471. Player.prototype.setFullscreen = function() {
  472. this.sender.send({type: Message.SET_FULLSCREEN});
  473. };
  474. /**
  475. * Helper for creating an iframe.
  476. *
  477. * @return {IFrameElement} The iframe.
  478. */
  479. Player.prototype.createIframe_ = function(contentInfo) {
  480. this.absolutifyPaths_(contentInfo);
  481. var iframe = document.createElement('iframe');
  482. iframe.setAttribute('allowfullscreen', true);
  483. iframe.setAttribute('scrolling', 'no');
  484. iframe.style.border = 0;
  485. // Handle iframe size if width and height are specified.
  486. if (contentInfo.hasOwnProperty('width')) {
  487. iframe.setAttribute('width', contentInfo.width);
  488. delete contentInfo.width;
  489. }
  490. if (contentInfo.hasOwnProperty('height')) {
  491. iframe.setAttribute('height', contentInfo.height);
  492. delete contentInfo.height;
  493. }
  494. var url = this.getEmbedUrl_() + Util.createGetParams(contentInfo);
  495. iframe.src = url;
  496. return iframe;
  497. };
  498. Player.prototype.onMessage_ = function(event) {
  499. var message = event.data;
  500. if (!message || !message.type) {
  501. console.warn('Received message with no type.');
  502. return;
  503. }
  504. var type = message.type.toLowerCase();
  505. var data = message.data;
  506. switch (type) {
  507. case 'ready':
  508. if (data !== undefined && data.duration !== undefined) {
  509. this.duration = data.duration;
  510. }
  511. case 'modechange':
  512. case 'error':
  513. case 'click':
  514. case 'ended':
  515. case 'getposition':
  516. this.emit(type, data);
  517. break;
  518. case 'volumechange':
  519. this.volume = data;
  520. this.emit('volumechange', data);
  521. break;
  522. case 'muted':
  523. this.isMuted = data;
  524. this.emit('mute', data);
  525. break;
  526. case 'timeupdate':
  527. this.currentTime = data;
  528. this.emit('timeupdate', {
  529. currentTime: this.currentTime,
  530. duration: this.duration
  531. });
  532. break;
  533. case 'play':
  534. case 'paused':
  535. this.isPaused = data;
  536. if (this.isPaused) {
  537. this.emit('pause', data);
  538. } else {
  539. this.emit('play', data);
  540. }
  541. break;
  542. case 'enter-fullscreen':
  543. case 'enter-vr':
  544. this.setFakeFullscreen_(true);
  545. break;
  546. case 'exit-fullscreen':
  547. this.setFakeFullscreen_(false);
  548. break;
  549. default:
  550. console.warn('Got unknown message of type %s from %s', message.type, message.origin);
  551. }
  552. };
  553. /**
  554. * Note: iOS doesn't support the fullscreen API.
  555. * In standalone <iframe> mode, VR View emulates fullscreen by redirecting to
  556. * another page.
  557. * In JS API mode, we stretch the iframe to cover the extent of the page using
  558. * CSS. To do this cleanly, we also inject a stylesheet.
  559. */
  560. Player.prototype.setFakeFullscreen_ = function(isFullscreen) {
  561. if (isFullscreen) {
  562. this.iframe.classList.add(FAKE_FULLSCREEN_CLASS);
  563. } else {
  564. this.iframe.classList.remove(FAKE_FULLSCREEN_CLASS);
  565. }
  566. };
  567. Player.prototype.injectFullscreenStylesheet_ = function() {
  568. var styleString = [
  569. 'iframe.' + FAKE_FULLSCREEN_CLASS,
  570. '{',
  571. 'position: fixed !important;',
  572. 'display: block !important;',
  573. 'z-index: 9999999999 !important;',
  574. 'top: 0 !important;',
  575. 'left: 0 !important;',
  576. 'width: 100% !important;',
  577. 'height: 100% !important;',
  578. 'margin: 0 !important;',
  579. '}',
  580. ].join('\n');
  581. var style = document.createElement('style');
  582. style.innerHTML = styleString;
  583. document.body.appendChild(style);
  584. };
  585. Player.prototype.getEmbedUrl_ = function() {
  586. // Assume that the script is in $ROOT/build/something.js, and that the iframe
  587. // HTML is in $ROOT/index.html.
  588. //
  589. // E.g: /vrview/2.0/build/vrview.min.js => /vrview/2.0/index.html.
  590. var path = CURRENT_SCRIPT_SRC;
  591. var split = path.split('/');
  592. var rootSplit = split.slice(0, split.length - 2);
  593. var rootPath = rootSplit.join('/');
  594. return rootPath + '/index.html';
  595. };
  596. Player.prototype.getDirName_ = function() {
  597. var path = window.location.pathname;
  598. path = path.substring(0, path.lastIndexOf('/'));
  599. return location.protocol + '//' + location.host + path;
  600. };
  601. /**
  602. * Make all of the URLs inside contentInfo absolute instead of relative.
  603. */
  604. Player.prototype.absolutifyPaths_ = function(contentInfo) {
  605. var dirName = this.getDirName_();
  606. var urlParams = ['image', 'preview', 'video'];
  607. for (var i = 0; i < urlParams.length; i++) {
  608. var name = urlParams[i];
  609. var path = contentInfo[name];
  610. if (path && Util.isPathAbsolute(path)) {
  611. var absolute = Util.relativeToAbsolutePath(dirName, path);
  612. contentInfo[name] = absolute;
  613. //console.log('Converted to absolute: %s', absolute);
  614. }
  615. }
  616. };
  617. /**
  618. * Get position YAW, PITCH
  619. */
  620. Player.prototype.getPosition = function() {
  621. this.sender.send({type: Message.GET_POSITION, data: {}});
  622. };
  623. module.exports = Player;
  624. },{"../message":5,"../util":6,"./iframe-message-sender":2,"eventemitter3":1}],5:[function(_dereq_,module,exports){
  625. /*
  626. * Copyright 2016 Google Inc. All Rights Reserved.
  627. * Licensed under the Apache License, Version 2.0 (the "License");
  628. * you may not use this file except in compliance with the License.
  629. * You may obtain a copy of the License at
  630. *
  631. * http://www.apache.org/licenses/LICENSE-2.0
  632. *
  633. * Unless required by applicable law or agreed to in writing, software
  634. * distributed under the License is distributed on an "AS IS" BASIS,
  635. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  636. * See the License for the specific language governing permissions and
  637. * limitations under the License.
  638. */
  639. /**
  640. * Messages from the API to the embed.
  641. */
  642. var Message = {
  643. PLAY: 'play',
  644. PAUSE: 'pause',
  645. TIMEUPDATE: 'timeupdate',
  646. ADD_HOTSPOT: 'addhotspot',
  647. SET_CONTENT: 'setimage',
  648. SET_VOLUME: 'setvolume',
  649. MUTED: 'muted',
  650. SET_CURRENT_TIME: 'setcurrenttime',
  651. DEVICE_MOTION: 'devicemotion',
  652. GET_POSITION: 'getposition',
  653. SET_FULLSCREEN: 'setfullscreen',
  654. };
  655. module.exports = Message;
  656. },{}],6:[function(_dereq_,module,exports){
  657. /*
  658. * Copyright 2016 Google Inc. All Rights Reserved.
  659. * Licensed under the Apache License, Version 2.0 (the "License");
  660. * you may not use this file except in compliance with the License.
  661. * You may obtain a copy of the License at
  662. *
  663. * http://www.apache.org/licenses/LICENSE-2.0
  664. *
  665. * Unless required by applicable law or agreed to in writing, software
  666. * distributed under the License is distributed on an "AS IS" BASIS,
  667. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  668. * See the License for the specific language governing permissions and
  669. * limitations under the License.
  670. */
  671. var Util = window.Util || {};
  672. Util.isDataURI = function(src) {
  673. return src && src.indexOf('data:') == 0;
  674. };
  675. Util.generateUUID = function() {
  676. function s4() {
  677. return Math.floor((1 + Math.random()) * 0x10000)
  678. .toString(16)
  679. .substring(1);
  680. }
  681. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  682. s4() + '-' + s4() + s4() + s4();
  683. };
  684. Util.isMobile = function() {
  685. var check = false;
  686. (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4)))check = true})(navigator.userAgent||navigator.vendor||window.opera);
  687. return check;
  688. };
  689. Util.isIOS = function() {
  690. return /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
  691. };
  692. Util.isSafari = function() {
  693. return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  694. };
  695. Util.cloneObject = function(obj) {
  696. var out = {};
  697. for (key in obj) {
  698. out[key] = obj[key];
  699. }
  700. return out;
  701. };
  702. Util.hashCode = function(s) {
  703. return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
  704. };
  705. Util.loadTrackSrc = function(context, src, callback, opt_progressCallback) {
  706. var request = new XMLHttpRequest();
  707. request.open('GET', src, true);
  708. request.responseType = 'arraybuffer';
  709. // Decode asynchronously.
  710. request.onload = function() {
  711. context.decodeAudioData(request.response, function(buffer) {
  712. callback(buffer);
  713. }, function(e) {
  714. console.error(e);
  715. });
  716. };
  717. if (opt_progressCallback) {
  718. request.onprogress = function(e) {
  719. var percent = e.loaded / e.total;
  720. opt_progressCallback(percent);
  721. };
  722. }
  723. request.send();
  724. };
  725. Util.isPow2 = function(n) {
  726. return (n & (n - 1)) == 0;
  727. };
  728. Util.capitalize = function(s) {
  729. return s.charAt(0).toUpperCase() + s.slice(1);
  730. };
  731. Util.isIFrame = function() {
  732. try {
  733. return window.self !== window.top;
  734. } catch (e) {
  735. return true;
  736. }
  737. };
  738. // From http://goo.gl/4WX3tg
  739. Util.getQueryParameter = function(name) {
  740. name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  741. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  742. results = regex.exec(location.search);
  743. return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  744. };
  745. // From http://stackoverflow.com/questions/11871077/proper-way-to-detect-webgl-support.
  746. Util.isWebGLEnabled = function() {
  747. var canvas = document.createElement('canvas');
  748. try { gl = canvas.getContext("webgl"); }
  749. catch (x) { gl = null; }
  750. if (gl == null) {
  751. try { gl = canvas.getContext("experimental-webgl"); experimental = true; }
  752. catch (x) { gl = null; }
  753. }
  754. return !!gl;
  755. };
  756. Util.clone = function(obj) {
  757. return JSON.parse(JSON.stringify(obj));
  758. };
  759. // From http://stackoverflow.com/questions/10140604/fastest-hypotenuse-in-javascript
  760. Util.hypot = Math.hypot || function(x, y) {
  761. return Math.sqrt(x*x + y*y);
  762. };
  763. // From http://stackoverflow.com/a/17447718/693934
  764. Util.isIE11 = function() {
  765. return navigator.userAgent.match(/Trident/);
  766. };
  767. Util.getRectCenter = function(rect) {
  768. return new THREE.Vector2(rect.x + rect.width/2, rect.y + rect.height/2);
  769. };
  770. Util.getScreenWidth = function() {
  771. return Math.max(window.screen.width, window.screen.height) *
  772. window.devicePixelRatio;
  773. };
  774. Util.getScreenHeight = function() {
  775. return Math.min(window.screen.width, window.screen.height) *
  776. window.devicePixelRatio;
  777. };
  778. Util.isIOS9OrLess = function() {
  779. if (!Util.isIOS()) {
  780. return false;
  781. }
  782. var re = /(iPhone|iPad|iPod) OS ([\d_]+)/;
  783. var iOSVersion = navigator.userAgent.match(re);
  784. if (!iOSVersion) {
  785. return false;
  786. }
  787. // Get the last group.
  788. var versionString = iOSVersion[iOSVersion.length - 1];
  789. var majorVersion = parseFloat(versionString);
  790. return majorVersion <= 9;
  791. };
  792. Util.getExtension = function(url) {
  793. return url.split('.').pop().split('?')[0];
  794. };
  795. Util.createGetParams = function(params) {
  796. var out = '?';
  797. for (var k in params) {
  798. var paramString = k + '=' + params[k] + '&';
  799. out += paramString;
  800. }
  801. // Remove the trailing ampersand.
  802. out.substring(0, params.length - 2);
  803. return out;
  804. };
  805. Util.sendParentMessage = function(message) {
  806. if (window.parent) {
  807. parent.postMessage(message, '*');
  808. }
  809. };
  810. Util.parseBoolean = function(value) {
  811. if (value == 'false' || value == 0) {
  812. return false;
  813. } else if (value == 'true' || value == 1) {
  814. return true;
  815. } else {
  816. return !!value;
  817. }
  818. };
  819. /**
  820. * @param base {String} An absolute directory root.
  821. * @param relative {String} A relative path.
  822. *
  823. * @returns {String} An absolute path corresponding to the rootPath.
  824. *
  825. * From http://stackoverflow.com/a/14780463/693934.
  826. */
  827. Util.relativeToAbsolutePath = function(base, relative) {
  828. var stack = base.split('/');
  829. var parts = relative.split('/');
  830. for (var i = 0; i < parts.length; i++) {
  831. if (parts[i] == '.') {
  832. continue;
  833. }
  834. if (parts[i] == '..') {
  835. stack.pop();
  836. } else {
  837. stack.push(parts[i]);
  838. }
  839. }
  840. return stack.join('/');
  841. };
  842. /**
  843. * @return {Boolean} True iff the specified path is an absolute path.
  844. */
  845. Util.isPathAbsolute = function(path) {
  846. return ! /^(?:\/|[a-z]+:\/\/)/.test(path);
  847. }
  848. Util.isEmptyObject = function(obj) {
  849. return Object.getOwnPropertyNames(obj).length == 0;
  850. };
  851. Util.isDebug = function() {
  852. return Util.parseBoolean(Util.getQueryParameter('debug'));
  853. };
  854. Util.getCurrentScript = function() {
  855. // Note: in IE11, document.currentScript doesn't work, so we fall back to this
  856. // hack, taken from https://goo.gl/TpExuH.
  857. if (!document.currentScript) {
  858. console.warn('This browser does not support document.currentScript. Trying fallback.');
  859. }
  860. return document.currentScript || document.scripts[document.scripts.length - 1];
  861. }
  862. module.exports = Util;
  863. },{}]},{},[3])(3)
  864. });