Skip to content

Embed a SWF in a page

@swf2es/web defines <swf2es-player> when it loads. It takes the same main attributes as an <embed>. The page calls configure() once, before the first player loads:

<swf2es-player id="movie" src="movie.swf" width="800" height="600"
flashvars="lang=en" scale="showAll" allowscriptaccess="sameDomain">
</swf2es-player>
<script type="module">
import { configure } from "@swf2es/web";
configure({
// Optional: room for a large game's compiled modules (64 MiB by default).
cache: { maxBytes: 256 * 1024 * 1024 },
});
const movie = document.getElementById("movie");
movie.addEventListener("error", (e) => console.error(e.detail.error));
await movie.ready; // resolves once the SWF plays
</script>

The packages are ES modules, so serve them with a bundler or an import map for @swf2es/* (tests/player/serve.ts has one), the renderer’s @swf2es/player/scene, @swf2es/render/webgl, @swf2es/render/ramps and @swf2es/context3d/device among them.

The element keeps the modules it compiles from a SWF’s ActionScript 3, and from the libraries it links against, in the page’s IndexedDB (a database named swf2es-modules), so that the next visit reads them rather than compiling them again: in headless Chrome a SWF that only traces loaded in 156 ms rather than 184 on a repeat visit, as3pb in 185 rather than 254, and a large application’s module took 250 ms to compile against 10 to read. They are written once the page is idle, and take at most 64 MiB, the least recently used going first; cache: { maxBytes } sets another limit. A browser that keeps nothing for the page just means no cache, and the SWF compiles as on a first visit: a private window, Safari clearing the storage of a site not visited for days, third-party storage blocked for a page in another site’s frame, or a database that fails or is slow to open. cache: false keeps none, for a site whose storage or consent policy needs that. A page whose players are in its markup, as one that loads the browser build from a CDN, may start them before any of its scripts could configure them, so cache="false" on an element does the same for that player alone: it neither reads nor writes the cache, and opens no database on its account, while the page’s other players share theirs. The attribute is read when a movie loads. Only "false" means anything: cache="true" does not turn the cache back on where configure set it off.

configure({ libraries: { builtin, playerglobal } }) takes URLs of other ABCs to load in place of the shipped ones, each optional: a page that would rather play on Flash Player’s own playerglobal gives its URL.