@markdy/renderer-dom 0.3.0 → 0.5.1
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 +4 -0
- package/dist/index.js +61 -11
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,10 @@ 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;
|
|
29
|
+
/** Show a small "Powered by Markdy" badge below the animation. Defaults to true. */
|
|
30
|
+
copyright?: boolean;
|
|
27
31
|
}
|
|
28
32
|
interface Player {
|
|
29
33
|
play(): void;
|
package/dist/index.js
CHANGED
|
@@ -637,18 +637,62 @@ 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 =
|
|
640
|
+
const { container, code, assets: assetOverrides = {}, autoplay = true, loop = true, copyright = 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);
|
|
650
|
+
let badge = null;
|
|
651
|
+
if (copyright) {
|
|
652
|
+
badge = document.createElement("a");
|
|
653
|
+
badge.href = "https://markdy.com";
|
|
654
|
+
badge.target = "_blank";
|
|
655
|
+
badge.rel = "noopener noreferrer";
|
|
656
|
+
badge.textContent = "Powered by Markdy";
|
|
657
|
+
Object.assign(badge.style, {
|
|
658
|
+
display: "block",
|
|
659
|
+
textAlign: "right",
|
|
660
|
+
fontSize: "10px",
|
|
661
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
662
|
+
color: "#999",
|
|
663
|
+
textDecoration: "none",
|
|
664
|
+
padding: "3px 6px 0",
|
|
665
|
+
opacity: "0.7",
|
|
666
|
+
transition: "opacity 0.2s"
|
|
667
|
+
});
|
|
668
|
+
badge.addEventListener("mouseenter", () => {
|
|
669
|
+
badge.style.opacity = "1";
|
|
670
|
+
});
|
|
671
|
+
badge.addEventListener("mouseleave", () => {
|
|
672
|
+
badge.style.opacity = "0.7";
|
|
673
|
+
});
|
|
674
|
+
container.appendChild(badge);
|
|
675
|
+
}
|
|
642
676
|
const scene = document.createElement("div");
|
|
643
677
|
Object.assign(scene.style, {
|
|
644
|
-
position: "
|
|
678
|
+
position: "absolute",
|
|
679
|
+
top: "0",
|
|
680
|
+
left: "0",
|
|
645
681
|
width: `${ast.meta.width}px`,
|
|
646
682
|
height: `${ast.meta.height}px`,
|
|
647
683
|
background: ast.meta.bg,
|
|
648
684
|
overflow: "hidden",
|
|
649
|
-
userSelect: "none"
|
|
685
|
+
userSelect: "none",
|
|
686
|
+
transformOrigin: "0 0"
|
|
650
687
|
});
|
|
651
|
-
|
|
688
|
+
viewport.appendChild(scene);
|
|
689
|
+
function scaleScene() {
|
|
690
|
+
const s = viewport.clientWidth / ast.meta.width;
|
|
691
|
+
scene.style.transform = `scale(${s})`;
|
|
692
|
+
}
|
|
693
|
+
scaleScene();
|
|
694
|
+
const resizeObserver = new ResizeObserver(scaleScene);
|
|
695
|
+
resizeObserver.observe(viewport);
|
|
652
696
|
const actorEls = /* @__PURE__ */ new Map();
|
|
653
697
|
for (const [name, def] of Object.entries(ast.actors)) {
|
|
654
698
|
const el = createActorEl(name, def, ast.assets, assetOverrides);
|
|
@@ -704,12 +748,16 @@ function createPlayer(opts) {
|
|
|
704
748
|
lastRafTs = timestamp;
|
|
705
749
|
const totalMs = (ast.meta.duration ?? 0) * 1e3;
|
|
706
750
|
if (totalMs > 0 && sceneMs >= totalMs) {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
751
|
+
if (loop) {
|
|
752
|
+
sceneMs = sceneMs % totalMs;
|
|
753
|
+
} else {
|
|
754
|
+
sceneMs = totalMs;
|
|
755
|
+
applyCurrentTime();
|
|
756
|
+
isPlaying = false;
|
|
757
|
+
lastRafTs = null;
|
|
758
|
+
rafId = null;
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
713
761
|
}
|
|
714
762
|
applyCurrentTime();
|
|
715
763
|
rafId = requestAnimationFrame(rafTick);
|
|
@@ -737,7 +785,9 @@ function createPlayer(opts) {
|
|
|
737
785
|
destroy() {
|
|
738
786
|
player.pause();
|
|
739
787
|
for (const anim of allAnims) anim.cancel();
|
|
740
|
-
|
|
788
|
+
resizeObserver.disconnect();
|
|
789
|
+
if (badge?.parentNode === container) container.removeChild(badge);
|
|
790
|
+
if (viewport.parentNode === container) container.removeChild(viewport);
|
|
741
791
|
}
|
|
742
792
|
};
|
|
743
793
|
if (autoplay) player.play();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markdy/renderer-dom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
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.
|
|
46
|
+
"@markdy/core": "0.5.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"tsup": "^8.5.1",
|