Spaces:
Running
Running
Download fallback-ring.js from lensmith-ai/composer: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/spaces/lensmith-ai/composer/resolve/main/fallback-ring.js
- Command line
-
hf download hf://spaces/lensmith-ai/composer/fallback-ring.js
-
curl -L -o fallback-ring.js https://huggingface.co/spaces/lensmith-ai/composer/resolve/main/fallback-ring.js
3.87 kB
| /* A still fallback can retain the artifact's ring as a static representation. | |
| The source image is captured from the artifact's own settled ring canvas. */ | |
| (function () { | |
| 'use strict'; | |
| var host = document.querySelector('[data-composer-window]'); | |
| if (!host) return; | |
| var imageUrl = new URL('assets/fallback-ring.png', document.baseURI).href; | |
| var watched = new WeakSet(); | |
| function watch(frame) { | |
| if (watched.has(frame)) return; | |
| watched.add(frame); | |
| var generation = 0; | |
| function attach() { | |
| var win, doc, current = ++generation, request = 0; | |
| try { win = frame.contentWindow; doc = frame.contentDocument; if (!win || !doc) return; } catch (_) { return; } | |
| function valid(ticket) { | |
| try { return frame.isConnected && generation === current && request === ticket && frame.contentWindow === win && frame.contentDocument === doc; } | |
| catch (_) { return false; } | |
| } | |
| win.addEventListener('message', function (event) { | |
| if (!valid(request) || event.source !== window || event.origin !== location.origin || !event.data) return; | |
| if (event.data.lensmithHost === 'play') { | |
| request++; | |
| if (frame.isConnected && frame.contentDocument === doc) { | |
| var old = doc.querySelector('img.space-fallback-ring'); | |
| if (old) old.remove(); | |
| } | |
| return; | |
| } | |
| if (event.data.lensmithHost !== 'still' || !doc.location || doc.location.hash !== '#replay') return; | |
| var ticket = ++request, deadline = Date.now() + 10000; | |
| function settle() { | |
| if (!valid(ticket)) return; | |
| var stage = doc.getElementById('cmpStage'), parent = stage && stage.offsetParent; | |
| if (!stage || !parent) return; | |
| if (doc.body.getAttribute('data-replay') !== 'done') { | |
| if (Date.now() < deadline) window.setTimeout(settle, 100); | |
| return; | |
| } | |
| if (doc.querySelector('canvas.cmp-entry-sand-rest') || doc.querySelector('img.space-fallback-ring')) return; | |
| var img = doc.createElement('img'); | |
| img.className = 'space-fallback-ring'; | |
| img.alt = ''; | |
| img.setAttribute('aria-hidden', 'true'); | |
| img.style.cssText = 'position:absolute;pointer-events:none;z-index:3;'; | |
| img.onload = function () { | |
| var decoded = img.decode ? img.decode() : Promise.resolve(); | |
| decoded.then(function () { | |
| if (!valid(ticket) || !img.naturalWidth || !img.naturalHeight || !stage.isConnected || !parent.isConnected || doc.querySelector('canvas.cmp-entry-sand-rest') || doc.querySelector('img.space-fallback-ring')) return; | |
| parent.appendChild(img); | |
| function position() { | |
| if (!valid(ticket) || !img.isConnected) return; | |
| var r = stage.getBoundingClientRect(), p = parent.getBoundingClientRect(); | |
| img.style.left = (r.left - p.left - parent.clientLeft - 6) + 'px'; | |
| img.style.top = (r.top - p.top - parent.clientTop - 6) + 'px'; | |
| img.style.width = (r.width + 12) + 'px'; | |
| img.style.height = (r.height + 12) + 'px'; | |
| } | |
| position(); | |
| win.addEventListener('resize', position, { passive: true }); | |
| }).catch(function () { /* Plain finished card remains visible. */ }); | |
| }; | |
| img.onerror = function () { /* Plain finished card remains visible. */ }; | |
| img.src = imageUrl; | |
| } | |
| window.setTimeout(settle, 0); | |
| }); | |
| } | |
| frame.addEventListener('load', attach); | |
| try { if (frame.contentDocument && frame.contentDocument.readyState === 'complete') attach(); } catch (_) {} | |
| } | |
| function scan() { host.querySelectorAll('iframe').forEach(watch); } | |
| new MutationObserver(scan).observe(host, { childList: true }); | |
| scan(); | |
| })(); | |