MikaFil commited on
Commit
7bbba4d
·
verified ·
1 Parent(s): 75c385c

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +48 -22
viewer.js CHANGED
@@ -85,16 +85,17 @@ export async function initializeViewer(config, instanceId) {
85
  const canvas = document.createElement('canvas');
86
  canvas.id = canvasId;
87
  canvas.className = 'ply-canvas';
88
- canvas.style.width = "100%";
89
- canvas.style.height = "100%";
90
  canvas.setAttribute('tabindex', '0');
91
  canvas.style.background = canvasBg;
92
  canvas.style.touchAction = "none";
93
  canvas.style.webkitTouchCallout = "none";
94
- canvas.addEventListener('gesturestart', e => e.preventDefault());
95
- canvas.addEventListener('gesturechange', e => e.preventDefault());
96
- canvas.addEventListener('gestureend', e => e.preventDefault());
97
- canvas.addEventListener('dblclick', e => e.preventDefault());
 
98
  canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
99
  // Mouse wheel suppression
100
  canvas.addEventListener('wheel', (e) => { e.preventDefault(); }, { passive: false });
@@ -124,10 +125,11 @@ export async function initializeViewer(config, instanceId) {
124
  throw e;
125
  }
126
 
 
127
  const opts = new pc.AppOptions();
128
  opts.graphicsDevice = device;
129
- opts.mouse = new pc.Mouse(canvas);
130
- opts.touch = new pc.TouchDevice(canvas);
131
  opts.componentSystems = [
132
  pc.RenderComponentSystem,
133
  pc.CameraComponentSystem,
@@ -144,26 +146,50 @@ export async function initializeViewer(config, instanceId) {
144
  pc.GSplatHandler
145
  ];
146
 
 
147
  app = new pc.Application(canvas, opts);
148
- app.setCanvasFillMode(pc.FILLMODE_NONE);
 
 
 
 
 
 
149
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
150
 
151
- // ---- Resize Observer ----
152
- resizeObserver = new ResizeObserver(entries => {
153
- entries.forEach(entry => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  try {
155
- app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
156
  } catch (e) { console.error("resizeCanvas error: " + e); }
157
  });
158
- });
159
- resizeObserver.observe(viewerContainer);
160
-
161
- window.addEventListener('resize', () => {
162
- try {
163
- app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
164
- } catch (e) { console.error("resizeCanvas error: " + e); }
165
- });
166
- app.on('destroy', () => resizeObserver.disconnect());
167
 
168
  // ---- Assets Setup ----
169
  const assets = {
 
85
  const canvas = document.createElement('canvas');
86
  canvas.id = canvasId;
87
  canvas.className = 'ply-canvas';
88
+ // REMOVE width/height style: PlayCanvas handles with fillWindow mode
89
+ canvas.removeAttribute('style'); // very important on iOS
90
  canvas.setAttribute('tabindex', '0');
91
  canvas.style.background = canvasBg;
92
  canvas.style.touchAction = "none";
93
  canvas.style.webkitTouchCallout = "none";
94
+ // Passive listeners for iOS: always passive false if calling preventDefault
95
+ canvas.addEventListener('gesturestart', e => e.preventDefault(), { passive: false });
96
+ canvas.addEventListener('gesturechange', e => e.preventDefault(), { passive: false });
97
+ canvas.addEventListener('gestureend', e => e.preventDefault(), { passive: false });
98
+ canvas.addEventListener('dblclick', e => e.preventDefault(), { passive: false });
99
  canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
100
  // Mouse wheel suppression
101
  canvas.addEventListener('wheel', (e) => { e.preventDefault(); }, { passive: false });
 
125
  throw e;
126
  }
127
 
128
+ // --- APP OPTIONS: input target fix ---
129
  const opts = new pc.AppOptions();
130
  opts.graphicsDevice = device;
131
+ opts.mouse = new pc.Mouse(document.body); // <--- NOTE: document.body, not canvas!
132
+ opts.touch = new pc.TouchDevice(document.body); // <--- document.body, matches PlayCanvas example
133
  opts.componentSystems = [
134
  pc.RenderComponentSystem,
135
  pc.CameraComponentSystem,
 
146
  pc.GSplatHandler
147
  ];
148
 
149
+ // The PlayCanvas Vercel demo uses AppBase, but we can keep using Application
150
  app = new pc.Application(canvas, opts);
151
+
152
+ // ---- iOS/Fill Mode handling ----
153
+ if (isIOS) {
154
+ app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); // always fill window on iOS
155
+ } else {
156
+ app.setCanvasFillMode(pc.FILLMODE_NONE); // your original behavior for desktop/Android
157
+ }
158
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
159
 
160
+ // ---- Resize Handling ----
161
+ // iOS: Always fill window. Desktop: use div resizeObserver.
162
+ if (isIOS) {
163
+ // Remove manual style sizes on the canvas
164
+ canvas.style.width = "";
165
+ canvas.style.height = "";
166
+ // Fill window
167
+ const resizeCanvasToWindow = () => {
168
+ app.resizeCanvas(window.innerWidth, window.innerHeight);
169
+ };
170
+ window.addEventListener('resize', resizeCanvasToWindow);
171
+ app.on('destroy', () => {
172
+ window.removeEventListener('resize', resizeCanvasToWindow);
173
+ });
174
+ resizeCanvasToWindow();
175
+ } else {
176
+ // Desktop: use ResizeObserver on the container div
177
+ resizeObserver = new ResizeObserver(entries => {
178
+ entries.forEach(entry => {
179
+ try {
180
+ app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
181
+ } catch (e) { console.error("resizeCanvas error: " + e); }
182
+ });
183
+ });
184
+ resizeObserver.observe(viewerContainer);
185
+
186
+ window.addEventListener('resize', () => {
187
  try {
188
+ app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
189
  } catch (e) { console.error("resizeCanvas error: " + e); }
190
  });
191
+ app.on('destroy', () => resizeObserver.disconnect());
192
+ }
 
 
 
 
 
 
 
193
 
194
  // ---- Assets Setup ----
195
  const assets = {