81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
/*
|
|
* Copyright 2016 Google Inc. All Rights Reserved.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
var Message = require('../message');
|
|
|
|
/**
|
|
* Sends events to the embedded VR view IFrame via postMessage. Also handles
|
|
* messages sent back from the IFrame:
|
|
*
|
|
* click: When a hotspot was clicked.
|
|
* modechange: When the user changes viewing mode (VR|Fullscreen|etc).
|
|
*/
|
|
function IFrameMessageSender(iframe) {
|
|
if (!iframe) {
|
|
console.error('No iframe specified');
|
|
return;
|
|
}
|
|
this.iframe = iframe;
|
|
|
|
// On iOS, if the iframe is across domains, also send DeviceMotion data.
|
|
if (this.isIOS_()) {
|
|
window.addEventListener('devicemotion', this.onDeviceMotion_.bind(this), false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sends a message to the associated VR View IFrame.
|
|
*/
|
|
IFrameMessageSender.prototype.send = function(message) {
|
|
var iframeWindow = this.iframe.contentWindow;
|
|
iframeWindow.postMessage(message, '*');
|
|
};
|
|
|
|
IFrameMessageSender.prototype.onDeviceMotion_ = function(e) {
|
|
var message = {
|
|
type: Message.DEVICE_MOTION,
|
|
deviceMotionEvent: this.cloneDeviceMotionEvent_(e)
|
|
};
|
|
|
|
this.send(message);
|
|
};
|
|
|
|
IFrameMessageSender.prototype.cloneDeviceMotionEvent_ = function(e) {
|
|
return {
|
|
acceleration: {
|
|
x: e.acceleration.x,
|
|
y: e.acceleration.y,
|
|
z: e.acceleration.z,
|
|
},
|
|
accelerationIncludingGravity: {
|
|
x: e.accelerationIncludingGravity.x,
|
|
y: e.accelerationIncludingGravity.y,
|
|
z: e.accelerationIncludingGravity.z,
|
|
},
|
|
rotationRate: {
|
|
alpha: e.rotationRate.alpha,
|
|
beta: e.rotationRate.beta,
|
|
gamma: e.rotationRate.gamma,
|
|
},
|
|
interval: e.interval,
|
|
timeStamp: e.timeStamp
|
|
};
|
|
};
|
|
|
|
IFrameMessageSender.prototype.isIOS_ = function() {
|
|
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
};
|
|
|
|
module.exports = IFrameMessageSender;
|