@markdy/renderer-dom 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -24,6 +24,8 @@ interface PlayerOptions {
24
24
  code: string;
25
25
  assets?: Record<string, string>;
26
26
  autoplay?: boolean;
27
+ /** Loop the animation when it reaches the end. Defaults to true. */
28
+ loop?: boolean;
27
29
  }
28
30
  interface Player {
29
31
  play(): void;
package/dist/index.js CHANGED
@@ -637,18 +637,36 @@ function buildAction(ev, el, s, baseOpts, delayMs, durMs, ast, states, scene, as
637
637
 
638
638
  // src/player.ts
639
639
  function createPlayer(opts) {
640
- const { container, code, assets: assetOverrides = {}, autoplay = false } = opts;
640
+ const { container, code, assets: assetOverrides = {}, autoplay = true, loop = true } = opts;
641
641
  const ast = parse(code);
642
+ const viewport = document.createElement("div");
643
+ Object.assign(viewport.style, {
644
+ position: "relative",
645
+ width: "100%",
646
+ aspectRatio: `${ast.meta.width} / ${ast.meta.height}`,
647
+ overflow: "hidden"
648
+ });
649
+ container.appendChild(viewport);
642
650
  const scene = document.createElement("div");
643
651
  Object.assign(scene.style, {
644
- position: "relative",
652
+ position: "absolute",
653
+ top: "0",
654
+ left: "0",
645
655
  width: `${ast.meta.width}px`,
646
656
  height: `${ast.meta.height}px`,
647
657
  background: ast.meta.bg,
648
658
  overflow: "hidden",
649
- userSelect: "none"
659
+ userSelect: "none",
660
+ transformOrigin: "0 0"
650
661
  });
651
- container.appendChild(scene);
662
+ viewport.appendChild(scene);
663
+ function scaleScene() {
664
+ const s = viewport.clientWidth / ast.meta.width;
665
+ scene.style.transform = `scale(${s})`;
666
+ }
667
+ scaleScene();
668
+ const resizeObserver = new ResizeObserver(scaleScene);
669
+ resizeObserver.observe(viewport);
652
670
  const actorEls = /* @__PURE__ */ new Map();
653
671
  for (const [name, def] of Object.entries(ast.actors)) {
654
672
  const el = createActorEl(name, def, ast.assets, assetOverrides);
@@ -704,12 +722,16 @@ function createPlayer(opts) {
704
722
  lastRafTs = timestamp;
705
723
  const totalMs = (ast.meta.duration ?? 0) * 1e3;
706
724
  if (totalMs > 0 && sceneMs >= totalMs) {
707
- sceneMs = totalMs;
708
- applyCurrentTime();
709
- isPlaying = false;
710
- lastRafTs = null;
711
- rafId = null;
712
- return;
725
+ if (loop) {
726
+ sceneMs = sceneMs % totalMs;
727
+ } else {
728
+ sceneMs = totalMs;
729
+ applyCurrentTime();
730
+ isPlaying = false;
731
+ lastRafTs = null;
732
+ rafId = null;
733
+ return;
734
+ }
713
735
  }
714
736
  applyCurrentTime();
715
737
  rafId = requestAnimationFrame(rafTick);
@@ -737,7 +759,8 @@ function createPlayer(opts) {
737
759
  destroy() {
738
760
  player.pause();
739
761
  for (const anim of allAnims) anim.cancel();
740
- if (scene.parentNode === container) container.removeChild(scene);
762
+ resizeObserver.disconnect();
763
+ if (viewport.parentNode === container) container.removeChild(viewport);
741
764
  }
742
765
  };
743
766
  if (autoplay) player.play();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markdy/renderer-dom",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "description": "Web Animations API renderer for MarkdyScript scenes.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -43,7 +43,7 @@
43
43
  "access": "public"
44
44
  },
45
45
  "dependencies": {
46
- "@markdy/core": "0.2.0"
46
+ "@markdy/core": "0.5.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "tsup": "^8.5.1",