@wangyaoshen/locus-player 0.1.9

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/main.js ADDED
@@ -0,0 +1,184 @@
1
+ var g = Object.defineProperty;
2
+ var p = (o, s, t) => s in o ? g(o, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[s] = t;
3
+ var e = (o, s, t) => (p(o, typeof s != "symbol" ? s + "" : s, t), t);
4
+ import { Stage as v, Player as b, Vector2 as m } from "@wangyaoshen/locus-core";
5
+ const f = `.initial{display:none}.state-initial .initial{display:block}.loading{display:none}.state-loading .loading{display:block}.ready{display:none}.state-ready .ready{display:block}.error{display:none}.state-error .error{display:block}:host{position:relative;display:block}.overlay{position:absolute;left:0;right:0;top:0;bottom:0;display:flex;align-items:center;justify-content:center;opacity:0;background-color:#0000008a;transition:opacity .1s}.overlay.state-ready:not(.auto){cursor:pointer}.overlay.playing:not(.hover):hover{cursor:none}.overlay.hover,.overlay.state-ready:focus-within,.overlay.state-ready:not(.playing){opacity:1}.overlay.hover .button,.overlay.state-ready:focus-within .button,.overlay.state-ready:not(.playing) .button{scale:1;transition:scale .1s ease-out}.overlay.state-loading,.overlay.state-error{opacity:1;transition:opacity 1s}.overlay.state-ready.auto{opacity:0}.button{width:50%;max-width:96px;aspect-ratio:1;scale:.5;transition:scale .1s ease-in,opacity .1s;background-color:transparent;border:none;background-size:100% 100%;background-repeat:no-repeat;opacity:.54;cursor:inherit;background-image:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiB3aWR0aD0iMjRweCIgZmlsbD0iI2ZmZmZmZiI+PHBhdGggZD0iTTAgMGgyNHYyNEgwVjB6IiBmaWxsPSJub25lIi8+PHBhdGggZD0iTTEyIDJDNi40OCAyIDIgNi40OCAyIDEyczQuNDggMTAgMTAgMTAgMTAtNC40OCAxMC0xMFMxNy41MiAyIDEyIDJ6bS0yIDE0LjV2LTlsNiA0LjUtNiA0LjV6Ii8+PC9zdmc+)}.playing .button{background-image:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDI0IDI0IiBoZWlnaHQ9IjI0cHgiIHZpZXdCb3g9IjAgMCAyNCAyNCIgd2lkdGg9IjI0cHgiIGZpbGw9IiNmZmZmZmYiPjxnPjxyZWN0IGZpbGw9Im5vbmUiIGhlaWdodD0iMjQiIHdpZHRoPSIyNCIvPjxyZWN0IGZpbGw9Im5vbmUiIGhlaWdodD0iMjQiIHdpZHRoPSIyNCIvPjxyZWN0IGZpbGw9Im5vbmUiIGhlaWdodD0iMjQiIHdpZHRoPSIyNCIvPjwvZz48Zz48Zy8+PHBhdGggZD0iTTEyLDJDNi40OCwyLDIsNi40OCwyLDEyczQuNDgsMTAsMTAsMTBzMTAtNC40OCwxMC0xMFMxNy41MiwyLDEyLDJ6IE0xMSwxNkg5VjhoMlYxNnogTTE1LDE2aC0yVjhoMlYxNnoiLz48L2c+PC9zdmc+)}.button:focus,.overlay:hover .button{opacity:.87}.auto .button{display:none}.canvas{width:100%;display:block;opacity:0;transition:opacity .1s}.canvas.state-ready{opacity:1}.message{font-family:JetBrains Mono,sans-serif;text-align:center;font-size:20px;padding:8px 16px;margin:16px;border-radius:4px;color:#fff9;background-color:#000000de}.loader{width:50%;max-width:96px;display:none;rotate:-90deg;animation:stroke 2s cubic-bezier(.5,0,.5,1) infinite,rotate 2s linear infinite}@keyframes stroke{0%{stroke-dasharray:5.6548667765px 50.8938009883px;stroke-dashoffset:2.8274333882px}50%{stroke-dasharray:50.8938009883px 5.6548667765px;stroke-dashoffset:-2.8274333882px}to{stroke-dasharray:5.6548667765px 50.8938009883px;stroke-dashoffset:-53.7212343766px}}@keyframes rotate{0%{rotate:-110deg}to{rotate:250deg}}
6
+ `, I = `<div class="overlay" part="overlay">
7
+ <button
8
+ part="play-button"
9
+ title="Play / Pause"
10
+ class="button ready"
11
+ tabindex="0"
12
+ ></button>
13
+ <div part="message" class="message error">
14
+ An error occurred while loading the animation.
15
+ </div>
16
+ <svg
17
+ part="loader"
18
+ class="loader loading"
19
+ viewBox="0 0 24 24"
20
+ stroke="#ffffff"
21
+ stroke-width="2"
22
+ fill="transparent"
23
+ >
24
+ <circle cx="12" cy="12" r="9" />
25
+ </svg>
26
+ </div>
27
+ `, M = `<style>${f}</style>${I}`, c = "motion-canvas-player";
28
+ class w extends HTMLElement {
29
+ constructor() {
30
+ super();
31
+ e(this, "root");
32
+ e(this, "canvas");
33
+ e(this, "overlay");
34
+ e(this, "button");
35
+ e(this, "state", "initial");
36
+ e(this, "project", null);
37
+ e(this, "player", null);
38
+ e(this, "defaultSettings");
39
+ e(this, "abortController", null);
40
+ e(this, "mouseMoveId", null);
41
+ e(this, "finished", !1);
42
+ e(this, "playing", !1);
43
+ e(this, "connected", !1);
44
+ e(this, "stage", new v());
45
+ e(this, "handleMouseMove", () => {
46
+ this.mouseMoveId && clearTimeout(this.mouseMoveId), this.hover && !this.playing && this.setPlaying(!0), this.mouseMoveId = window.setTimeout(() => {
47
+ this.mouseMoveId = null, this.updateClass();
48
+ }, 2e3), this.updateClass();
49
+ });
50
+ e(this, "handleMouseLeave", () => {
51
+ this.hover && this.setPlaying(!1), this.mouseMoveId && (clearTimeout(this.mouseMoveId), this.mouseMoveId = null, this.updateClass());
52
+ });
53
+ e(this, "handleMouseDown", (t) => {
54
+ t.preventDefault();
55
+ });
56
+ e(this, "handleClick", () => {
57
+ this.auto || (this.handleMouseMove(), this.setPlaying(!this.playing), this.button.animate(
58
+ [
59
+ { scale: "0.9" },
60
+ {
61
+ scale: "1",
62
+ easing: "ease-out"
63
+ }
64
+ ],
65
+ { duration: 200 }
66
+ ));
67
+ });
68
+ e(this, "render", async () => {
69
+ this.player && await this.stage.render(
70
+ this.player.playback.currentScene,
71
+ this.player.playback.previousScene
72
+ );
73
+ });
74
+ this.root = this.attachShadow({ mode: "open" }), this.root.innerHTML = M, this.overlay = this.root.querySelector(".overlay"), this.button = this.root.querySelector(".button"), this.canvas = this.stage.finalBuffer, this.canvas.classList.add("canvas"), this.root.prepend(this.canvas), this.overlay.addEventListener("click", this.handleClick), this.overlay.addEventListener("mousemove", this.handleMouseMove), this.overlay.addEventListener("mouseleave", this.handleMouseLeave), this.button.addEventListener("mousedown", this.handleMouseDown), this.setState(
75
+ "initial"
76
+ /* Initial */
77
+ );
78
+ }
79
+ static get observedAttributes() {
80
+ return ["src", "quality", "width", "height", "auto", "variables"];
81
+ }
82
+ get auto() {
83
+ return !!this.getAttribute("auto");
84
+ }
85
+ get hover() {
86
+ return this.getAttribute("auto") === "hover";
87
+ }
88
+ get quality() {
89
+ const t = this.getAttribute("quality");
90
+ return t ? parseFloat(t) : this.defaultSettings.resolutionScale;
91
+ }
92
+ get width() {
93
+ const t = this.getAttribute("width");
94
+ return t ? parseFloat(t) : this.defaultSettings.size.width;
95
+ }
96
+ get height() {
97
+ const t = this.getAttribute("height");
98
+ return t ? parseFloat(t) : this.defaultSettings.size.height;
99
+ }
100
+ get variables() {
101
+ try {
102
+ const t = this.getAttribute("variables");
103
+ return t ? JSON.parse(t) : {};
104
+ } catch {
105
+ return this.project.logger.warn("Project variables could not be parsed."), {};
106
+ }
107
+ }
108
+ setState(t) {
109
+ this.state = t, this.setPlaying(this.playing);
110
+ }
111
+ setPlaying(t) {
112
+ var a, i;
113
+ this.state === "ready" && (t || this.auto && !this.hover) ? ((a = this.player) == null || a.togglePlayback(!0), this.playing = !0) : ((i = this.player) == null || i.togglePlayback(!1), this.playing = !1), this.updateClass();
114
+ }
115
+ updateClass() {
116
+ this.overlay.className = `overlay state-${this.state}`, this.canvas.className = `canvas state-${this.state}`, this.overlay.classList.toggle("playing", this.playing), this.overlay.classList.toggle("auto", this.auto), this.overlay.classList.toggle("hover", this.mouseMoveId !== null), this.connected && (this.mouseMoveId !== null || !this.playing ? this.dataset.overlay = "" : delete this.dataset.overlay);
117
+ }
118
+ async updateSource(t) {
119
+ var r, l, h, d;
120
+ this.setState(
121
+ "initial"
122
+ /* Initial */
123
+ ), (r = this.abortController) == null || r.abort(), this.abortController = new AbortController();
124
+ let a;
125
+ try {
126
+ const n = import(
127
+ /* webpackIgnore: true */
128
+ /* @vite-ignore */
129
+ t
130
+ ), y = new Promise((u) => setTimeout(u, 200));
131
+ await Promise.any([y, n]), this.setState(
132
+ "loading"
133
+ /* Loading */
134
+ ), a = (await n).default;
135
+ } catch (n) {
136
+ console.error(n), this.setState(
137
+ "error"
138
+ /* Error */
139
+ );
140
+ return;
141
+ }
142
+ this.defaultSettings = a.meta.getFullRenderingSettings();
143
+ const i = new b(a);
144
+ i.setVariables(this.variables), this.finished = !1, (l = this.player) == null || l.onRender.unsubscribe(this.render), (h = this.player) == null || h.togglePlayback(!1), (d = this.player) == null || d.deactivate(), this.project = a, this.player = i, this.updateSettings(), this.player.onRender.subscribe(this.render), this.player.togglePlayback(this.playing), this.setState(
145
+ "ready"
146
+ /* Ready */
147
+ );
148
+ }
149
+ attributeChangedCallback(t, a, i) {
150
+ var r;
151
+ switch (t) {
152
+ case "auto":
153
+ this.setPlaying(this.playing);
154
+ break;
155
+ case "src":
156
+ this.updateSource(i);
157
+ break;
158
+ case "quality":
159
+ case "width":
160
+ case "height":
161
+ this.updateSettings();
162
+ break;
163
+ case "variables":
164
+ (r = this.player) == null || r.setVariables(this.variables);
165
+ }
166
+ }
167
+ disconnectedCallback() {
168
+ var t, a;
169
+ this.connected = !1, (t = this.player) == null || t.deactivate(), (a = this.player) == null || a.onRender.unsubscribe(this.render);
170
+ }
171
+ connectedCallback() {
172
+ var t, a;
173
+ this.connected = !0, (t = this.player) == null || t.activate(), (a = this.player) == null || a.onRender.subscribe(this.render);
174
+ }
175
+ updateSettings() {
176
+ const t = {
177
+ ...this.defaultSettings,
178
+ size: new m(this.width, this.height),
179
+ resolutionScale: this.quality
180
+ };
181
+ this.stage.configure(t), this.player.configure(t);
182
+ }
183
+ }
184
+ customElements.get(c) || customElements.define(c, w);
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@wangyaoshen/locus-player",
3
+ "version": "0.1.9",
4
+ "description": "A custom element for displaying animations made with Motion Canvas",
5
+ "main": "dist/main.js",
6
+ "types": "types/main.d.ts",
7
+ "author": "motion-canvas",
8
+ "homepage": "https://motioncanvas.io/",
9
+ "bugs": "https://github.com/motion-canvas/motion-canvas/issues",
10
+ "license": "MIT",
11
+ "type": "module",
12
+ "scripts": {
13
+ "dev": "vite",
14
+ "build": "tsc && vite build"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/motion-canvas/motion-canvas.git"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "types"
23
+ ],
24
+ "devDependencies": {
25
+ "@wangyaoshen/locus-core": "0.1.9",
26
+ "sass": "^1.58.0",
27
+ "terser": "^5.16.1"
28
+ }
29
+ }
@@ -0,0 +1,8 @@
1
+ export interface MotionCanvasPlayerProps {
2
+ src: string;
3
+ width?: number;
4
+ height?: number;
5
+ auto?: boolean;
6
+ quality?: number;
7
+ variables?: string;
8
+ }