← Commit history

viewers: cp-cam postMessage hook for camera control

Caleb ·f35096311e ·2mo ago ·parent 1923fe1
1 file changed +32
README.html+32
@@ -99,6 +99,17 @@ else {       g.onclick = () =&gt; { groundOn = !groundOn; viewer.setGroundVisible(groundOn); paint(); };       l.onclick = () =&gt; { viewer.setBottomLight(!viewer.getBottomLightState()); paint(); };       paint(); ctl.style.display = &#x27;flex&#x27;;+      addEventListener(&#x27;message&#x27;, (e) =&gt; {+        const d = e.data || {};+        if (d.type !== &#x27;cp-cam&#x27; || (d.board &amp;&amp; d.board !== &#x27;workcell&#x27;)) return;+        try {+          const c = viewer.getCamera();+          if (Array.isArray(d.target)) c.setTarget(new window.BABYLON.Vector3(...d.target));+          if (typeof d.alpha === &#x27;number&#x27;) c.alpha = d.alpha;+          if (typeof d.beta === &#x27;number&#x27;) c.beta = d.beta;+          if (typeof d.radius === &#x27;number&#x27;) c.radius = d.radius;+        } catch (err) {}+      });     }).catch(() =&gt; wrap.remove());   } catch (e) { wrap.remove(); } }@@ -142,6 +153,17 @@ else {       g.onclick = () =&gt; { groundOn = !groundOn; viewer.setGroundVisible(groundOn); paint(); };       l.onclick = () =&gt; { viewer.setBottomLight(!viewer.getBottomLightState()); paint(); };       paint(); ctl.style.display = &#x27;flex&#x27;;+      addEventListener(&#x27;message&#x27;, (e) =&gt; {+        const d = e.data || {};+        if (d.type !== &#x27;cp-cam&#x27; || (d.board &amp;&amp; d.board !== &#x27;scaffold&#x27;)) return;+        try {+          const c = viewer.getCamera();+          if (Array.isArray(d.target)) c.setTarget(new window.BABYLON.Vector3(...d.target));+          if (typeof d.alpha === &#x27;number&#x27;) c.alpha = d.alpha;+          if (typeof d.beta === &#x27;number&#x27;) c.beta = d.beta;+          if (typeof d.radius === &#x27;number&#x27;) c.radius = d.radius;+        } catch (err) {}+      });     }).catch(() =&gt; wrap.remove());   } catch (e) { wrap.remove(); } }@@ -267,5 +289,15 @@ else {   post(); })(); </script>+<script>+// cp-cam relay: forward camera messages from the parent page into the viewer iframes+addEventListener('message', function (e) {+  var d = e.data || {};+  if (d.type !== 'cp-cam') return;+  document.querySelectorAll('iframe.vframe').forEach(function (f) {+    try { f.contentWindow.postMessage(d, '*'); } catch (err) {}+  });+});+</script> </body> </html>