Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/iframe/context.mjs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { win } from './runtime.mjs';
export { data, deviceType } from './state.mjs';
6 changes: 3 additions & 3 deletions examples/iframe/loader.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import files from './files.mjs';
import MiniStats from './ministats.mjs';
import { fetchFile, importModule, clearImports, parseConfig, fire } from './runtime.mjs';
import { fetchFile, importModule, clearImports, parseConfig, fire, win } from './runtime.mjs';
import { data, deviceType as selectedDeviceType, refreshContext, updateDeviceType } from './state.mjs';
import { blockZoom } from './zoom.mjs';

Expand Down Expand Up @@ -91,7 +91,7 @@ class ExampleLoader {
const reportedType = (isWebGPU(selectedDeviceType) && engineType === 'webgpu') ?
selectedDeviceType :
engineType;
window.top.activeGraphicsDevice = reportedType;
win.activeGraphicsDevice = reportedType;
fire('updateActiveDevice', { deviceType: reportedType });
}

Expand Down Expand Up @@ -166,7 +166,7 @@ class ExampleLoader {
window.pc = await import(engineUrl);

// @ts-ignore
window.top.pc = window.pc;
win.pc = window.pc;

await this._fetchFiles();

Expand Down
18 changes: 17 additions & 1 deletion examples/iframe/runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,26 @@ export function parseConfig(script) {
return config;
}

/**
* @type {Window | null} - The same-origin top window, if available.
*/
const host = (() => {
try {
return window.top && window.top.location.origin === window.location.origin ? window.top : null;
} catch {
return null;
}
})();

/**
* @type {Window} - The example-facing window.
*/
export const win = host ?? window;

/**
* @param {string} eventName - The name of the fired event.
* @param {object} detail - The detail object.
*/
export function fire(eventName, detail = {}) {
window.top?.dispatchEvent(new CustomEvent(eventName, { detail }));
host?.dispatchEvent(new CustomEvent(eventName, { detail }));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import * as pc from 'playcanvas';
import { CameraControls } from 'playcanvas/scripts/esm/camera-controls.mjs';
import { GsplatRevealRadial } from 'playcanvas/scripts/esm/gsplat/reveal-radial.mjs';

import { data, deviceType } from 'examples/context';
import { data, deviceType, win } from 'examples/context';

// allow overriding scene url and orientation via hash query params, e.g.
// #/gaussian-splatting/lod-streaming?url=https://example.com/scene/lod-meta.json&orientation=90
const hashQuery = (window.top?.location.hash || window.location.hash || '').split('?')[1] || '';
const hashQuery = (win.location.hash || window.location.hash || '').split('?')[1] || '';
const hashParams = new URLSearchParams(hashQuery);
const paramUrl = hashParams.get('url');
const paramOrientation = hashParams.get('orientation');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as pc from 'playcanvas';

import { data, deviceType } from 'examples/context';
import { data, deviceType, win } from 'examples/context';

const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();
Expand Down Expand Up @@ -295,7 +295,7 @@ assetListLoader.load(() => {

// resize control panel to fit the content better
if (resizeControlPanel) {
const panel = window.top.document.getElementById('controlPanel');
const panel = win.document.getElementById('controlPanel');
if (panel) {
panel.style.width = '360px';
resizeControlPanel = false;
Expand Down
10 changes: 5 additions & 5 deletions examples/src/examples/test/contact-hardening-shadows.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import * as pc from 'playcanvas';

import { data, deviceType } from 'examples/context';
import { data, deviceType, win } from 'examples/context';

const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();
Expand Down Expand Up @@ -297,9 +297,9 @@ assetListLoader.load(() => {
}
});

const areaLightElement = window.top.document.getElementById('area-light');
const pointLightElement = window.top.document.getElementById('point-light');
const directionalLightElement = window.top.document.getElementById('directional-light');
const areaLightElement = win.document.getElementById('area-light');
const pointLightElement = win.document.getElementById('point-light');
const directionalLightElement = win.document.getElementById('directional-light');

let resizeControlPanel = true;
let time = 0;
Expand Down Expand Up @@ -350,7 +350,7 @@ assetListLoader.load(() => {

// resize control panel to fit the content better
if (resizeControlPanel) {
const panel = window.top.document.getElementById('controlPanel');
const panel = win.document.getElementById('controlPanel');
if (panel) {
panel.style.width = '360px';
resizeControlPanel = false;
Expand Down
Loading