motlab.js 0.1.0 → 0.1.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/package.json CHANGED
@@ -1,107 +1,107 @@
1
- {
2
- "name": "motlab.js",
3
- "version": "0.1.0",
4
- "description": "JavaScript deterministic motion engine.",
5
- "keywords": [
6
- "animation",
7
- "motion",
8
- "deterministic",
9
- "timeline",
10
- "canvas",
11
- "webgl2",
12
- "svg",
13
- "video",
14
- "physics",
15
- "rig",
16
- "game"
17
- ],
18
- "homepage": "https://github.com/staryone/motlab.js#readme",
19
- "bugs": {
20
- "url": "https://github.com/staryone/motlab.js/issues"
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/staryone/motlab.js.git"
25
- },
26
- "license": "MIT",
27
- "type": "module",
28
- "engines": {
29
- "node": ">=24.0.0"
30
- },
31
- "exports": {
32
- ".": {
33
- "types": "./types/index.d.ts",
34
- "default": "./src/index.js"
35
- },
36
- "./renderer/canvas2d": {
37
- "types": "./types/renderer/canvas2d.d.ts",
38
- "default": "./src/renderer/canvas2d.js"
39
- },
40
- "./renderer/svg": {
41
- "types": "./types/renderer/svg.d.ts",
42
- "default": "./src/renderer/svg.js"
43
- },
44
- "./renderer/webgl2": {
45
- "types": "./types/renderer/webgl2.d.ts",
46
- "default": "./src/renderer/webgl2.js"
47
- },
48
- "./addons/*": {
49
- "types": "./types/addons/*.d.ts",
50
- "default": "./src/addons/*.js"
51
- },
52
- "./adapters/*": {
53
- "types": "./types/adapters/*.d.ts",
54
- "default": "./src/adapters/*.js"
55
- },
56
- "./package.json": "./package.json"
57
- },
58
- "sideEffects": false,
59
- "files": [
60
- "src",
61
- "types",
62
- "docs",
63
- "llms.txt",
64
- "CHANGELOG.md"
65
- ],
66
- "scripts": {
67
- "test": "node --test \"test/*.test.js\"",
68
- "test:browser": "node scripts/browser-test.js",
69
- "typecheck": "tsc -p tsconfig.core.json && tsc -p tsconfig.json",
70
- "types": "tsc -p tsconfig.types.json",
71
- "test:types": "npm run types && tsc -p test/types/tsconfig.json",
72
- "prepack": "npm run types"
73
- },
74
- "peerDependencies": {
75
- "@dimforge/rapier2d-deterministic-compat": ">=0.19.0",
76
- "@rive-app/canvas-advanced": "^2.0.0",
77
- "lottie-web": "^5.10.0",
78
- "pixi.js": "^8.0.0",
79
- "three": ">=0.170.0"
80
- },
81
- "peerDependenciesMeta": {
82
- "@dimforge/rapier2d-deterministic-compat": {
83
- "optional": true
84
- },
85
- "@rive-app/canvas-advanced": {
86
- "optional": true
87
- },
88
- "lottie-web": {
89
- "optional": true
90
- },
91
- "pixi.js": {
92
- "optional": true
93
- },
94
- "three": {
95
- "optional": true
96
- }
97
- },
98
- "devDependencies": {
99
- "@dimforge/rapier2d-deterministic-compat": "0.21.0",
100
- "@rive-app/canvas-advanced": "2.51.0",
101
- "@types/three": "0.187.0",
102
- "lottie-web": "5.13.0",
103
- "pixi.js": "8.23.0",
104
- "three": "0.187.0",
105
- "typescript": "7.0.2"
106
- }
107
- }
1
+ {
2
+ "name": "motlab.js",
3
+ "version": "0.1.1",
4
+ "description": "JavaScript deterministic motion engine.",
5
+ "keywords": [
6
+ "animation",
7
+ "motion",
8
+ "deterministic",
9
+ "timeline",
10
+ "canvas",
11
+ "webgl2",
12
+ "svg",
13
+ "video",
14
+ "physics",
15
+ "rig",
16
+ "game"
17
+ ],
18
+ "homepage": "https://github.com/staryone/motlab.js#readme",
19
+ "bugs": {
20
+ "url": "https://github.com/staryone/motlab.js/issues"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/staryone/motlab.js.git"
25
+ },
26
+ "license": "MIT",
27
+ "type": "module",
28
+ "engines": {
29
+ "node": ">=24.0.0"
30
+ },
31
+ "exports": {
32
+ ".": {
33
+ "types": "./types/index.d.ts",
34
+ "default": "./src/index.js"
35
+ },
36
+ "./renderer/canvas2d": {
37
+ "types": "./types/renderer/canvas2d.d.ts",
38
+ "default": "./src/renderer/canvas2d.js"
39
+ },
40
+ "./renderer/svg": {
41
+ "types": "./types/renderer/svg.d.ts",
42
+ "default": "./src/renderer/svg.js"
43
+ },
44
+ "./renderer/webgl2": {
45
+ "types": "./types/renderer/webgl2.d.ts",
46
+ "default": "./src/renderer/webgl2.js"
47
+ },
48
+ "./addons/*": {
49
+ "types": "./types/addons/*.d.ts",
50
+ "default": "./src/addons/*.js"
51
+ },
52
+ "./adapters/*": {
53
+ "types": "./types/adapters/*.d.ts",
54
+ "default": "./src/adapters/*.js"
55
+ },
56
+ "./package.json": "./package.json"
57
+ },
58
+ "sideEffects": false,
59
+ "files": [
60
+ "src",
61
+ "types",
62
+ "docs",
63
+ "llms.txt",
64
+ "CHANGELOG.md"
65
+ ],
66
+ "scripts": {
67
+ "test": "node --test \"test/*.test.js\"",
68
+ "test:browser": "node scripts/browser-test.js",
69
+ "typecheck": "tsc -p tsconfig.core.json && tsc -p tsconfig.json",
70
+ "types": "tsc -p tsconfig.types.json",
71
+ "test:types": "npm run types && tsc -p test/types/tsconfig.json",
72
+ "prepack": "npm run types"
73
+ },
74
+ "peerDependencies": {
75
+ "@dimforge/rapier2d-deterministic-compat": ">=0.19.0",
76
+ "@rive-app/canvas-advanced": "^2.0.0",
77
+ "lottie-web": "^5.10.0",
78
+ "pixi.js": "^8.0.0",
79
+ "three": ">=0.170.0"
80
+ },
81
+ "peerDependenciesMeta": {
82
+ "@dimforge/rapier2d-deterministic-compat": {
83
+ "optional": true
84
+ },
85
+ "@rive-app/canvas-advanced": {
86
+ "optional": true
87
+ },
88
+ "lottie-web": {
89
+ "optional": true
90
+ },
91
+ "pixi.js": {
92
+ "optional": true
93
+ },
94
+ "three": {
95
+ "optional": true
96
+ }
97
+ },
98
+ "devDependencies": {
99
+ "@dimforge/rapier2d-deterministic-compat": "0.21.0",
100
+ "@rive-app/canvas-advanced": "2.51.0",
101
+ "@types/three": "0.187.0",
102
+ "lottie-web": "5.13.0",
103
+ "pixi.js": "8.23.0",
104
+ "three": "0.187.0",
105
+ "typescript": "7.0.2"
106
+ }
107
+ }
@@ -1,102 +1,102 @@
1
- // Lottie animations inside a motlab scene, drawn by lottie-web's canvas renderer. Lottie keyframes are a function of
2
- // the frame number, so every moment is a seek: t becomes a frame and goToAndStop draws it.
3
- import { canvasLayer } from './canvas.js'
4
-
5
- /** @typedef {import('lottie-web').LottiePlayer} LottiePlayer */
6
- /** @typedef {import('../core/nodes.js').CommonProps} CommonProps */
7
- /** @typedef {import('../core/nodes.js').Node} Node */
8
- /**
9
- * @template T
10
- * @typedef {import('../core/math.js').Value<T>} Value
11
- */
12
-
13
- /**
14
- * The frame to show `seconds` into an animation of `total` frames at `fps`, counted from its first frame. Frames
15
- * between whole numbers tween, as lottie-web's subframes do.
16
- * @param {number} seconds @param {number} fps @param {number} total @param {boolean} loop
17
- */
18
- export function frameAt(seconds, fps, total, loop) {
19
- const frame = Math.max(0, seconds) * fps
20
- if (loop) return frame % total
21
- return Math.min(frame, total - 1)
22
- }
23
-
24
- /**
25
- * lottie-web's canvas renderer caches the context state it last set, such as the fill colour, and skips setting it
26
- * again. Resizing a canvas resets that state, so the cache has to be cleared with it.
27
- * @param {import('lottie-web').AnimationItem} anim
28
- */
29
- function forgetContextState(anim) {
30
- const data = /** @type {any} */ (anim).renderer?.contextData
31
- if (!data) return
32
- for (const key of Object.keys(data)) if (key.startsWith('applied')) data[key] = ''
33
- if ('currentOpacity' in data) data.currentOpacity = 1
34
- }
35
-
36
- /**
37
- * Loads a Lottie animation to place in scenes. Its nodes share one player, which seeks and draws for each node in
38
- * turn, so the same animation can show different moments at once.
39
- * @param {LottiePlayer | { default: LottiePlayer }} lottieWeb lottie-web, from 'lottie-web' or its canvas build: the
40
- * default export, or the whole module.
41
- * @param {{ data: Record<string, any> }} options data is the Lottie JSON, parsed.
42
- * @returns {Promise<{ duration: number, frameRate: number, size: [number, number],
43
- * node: (props?: CommonProps & { w?: number, h?: number, time?: Value<number>, loop?: boolean }) => Node,
44
- * dispose: () => void }>}
45
- * duration is in seconds. A node is w by h px (default: the animation's size) and plays from its local time, or
46
- * from `time`; loop repeats it, otherwise the last frame holds.
47
- */
48
- export async function lottie(lottieWeb, { data }) {
49
- const player = lottieWeb && 'loadAnimation' in lottieWeb ? lottieWeb : lottieWeb?.default
50
- if (typeof player?.loadAnimation !== 'function') throw new TypeError('lottie: pass lottie-web, as in import lottieWeb from \'lottie-web\'')
51
- if (!data || !(data.fr > 0) || !(data.op > data.ip) || !(data.w > 0 && data.h > 0)) throw new TypeError('lottie: data must be a parsed Lottie JSON with fr, ip, op, w and h')
52
- const fps = data.fr
53
- const total = data.op - data.ip
54
- /** @type {[number, number]} */
55
- const size = [data.w, data.h]
56
- // lottie-web expects a page, so a canvas element rather than an OffscreenCanvas.
57
- const canvas = document.createElement('canvas')
58
- canvas.width = data.w
59
- canvas.height = data.h
60
- const context = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'))
61
- // The typings ask for a container element, which the canvas renderer does without when it gets a context.
62
- const config = /** @type {import('lottie-web').AnimationConfigWithData<'canvas'>} */ (/** @type {unknown} */ ({
63
- renderer: 'canvas', loop: false, autoplay: false, animationData: data,
64
- rendererSettings: { context, clearCanvas: true, preserveAspectRatio: 'xMidYMid meet', dpr: 1 },
65
- }))
66
- const anim = player.loadAnimation(config)
67
- // Fonts and images load in the background, and drawing is synchronous, so wait for them here.
68
- if (!anim.isLoaded) {
69
- await new Promise((done, reject) => {
70
- anim.addEventListener('DOMLoaded', () => done(undefined))
71
- anim.addEventListener('data_failed', () => reject(new Error('lottie: animation assets failed to load')))
72
- })
73
- }
74
-
75
- let disposed = false
76
- return {
77
- duration: total / fps,
78
- frameRate: fps,
79
- size,
80
- dispose() {
81
- if (disposed) return
82
- disposed = true
83
- anim.destroy?.()
84
- },
85
- node({ w = size[0], h = size[1], loop = false, ...props } = {}) {
86
- return canvasLayer({
87
- ...props,
88
- w,
89
- h,
90
- render(t, width, height) {
91
- if (canvas.width !== width || canvas.height !== height) {
92
- // Before resizing, since the resize ends by redrawing the last frame.
93
- forgetContextState(anim)
94
- anim.resize(width, height)
95
- }
96
- anim.goToAndStop(frameAt(t, fps, total, loop), true)
97
- return canvas
98
- },
99
- })
100
- },
101
- }
102
- }
1
+ // Lottie animations inside a motlab scene, drawn by lottie-web's canvas renderer. Lottie keyframes are a function of
2
+ // the frame number, so every moment is a seek: t becomes a frame and goToAndStop draws it.
3
+ import { canvasLayer } from './canvas.js'
4
+
5
+ /** @typedef {import('lottie-web').LottiePlayer} LottiePlayer */
6
+ /** @typedef {import('../core/nodes.js').CommonProps} CommonProps */
7
+ /** @typedef {import('../core/nodes.js').Node} Node */
8
+ /**
9
+ * @template T
10
+ * @typedef {import('../core/math.js').Value<T>} Value
11
+ */
12
+
13
+ /**
14
+ * The frame to show `seconds` into an animation of `total` frames at `fps`, counted from its first frame. Frames
15
+ * between whole numbers tween, as lottie-web's subframes do.
16
+ * @param {number} seconds @param {number} fps @param {number} total @param {boolean} loop
17
+ */
18
+ export function frameAt(seconds, fps, total, loop) {
19
+ const frame = Math.max(0, seconds) * fps
20
+ if (loop) return frame % total
21
+ return Math.min(frame, total - 1)
22
+ }
23
+
24
+ /**
25
+ * lottie-web's canvas renderer caches the context state it last set, such as the fill colour, and skips setting it
26
+ * again. Resizing a canvas resets that state, so the cache has to be cleared with it.
27
+ * @param {import('lottie-web').AnimationItem} anim
28
+ */
29
+ function forgetContextState(anim) {
30
+ const data = /** @type {any} */ (anim).renderer?.contextData
31
+ if (!data) return
32
+ for (const key of Object.keys(data)) if (key.startsWith('applied')) data[key] = ''
33
+ if ('currentOpacity' in data) data.currentOpacity = 1
34
+ }
35
+
36
+ /**
37
+ * Loads a Lottie animation to place in scenes. Its nodes share one player, which seeks and draws for each node in
38
+ * turn, so the same animation can show different moments at once.
39
+ * @param {LottiePlayer | { default: LottiePlayer }} lottieWeb lottie-web, from 'lottie-web' or its canvas build: the
40
+ * default export, or the whole module.
41
+ * @param {{ data: Record<string, any> }} options data is the Lottie JSON, parsed.
42
+ * @returns {Promise<{ duration: number, frameRate: number, size: [number, number],
43
+ * node: (props?: CommonProps & { w?: number, h?: number, time?: Value<number>, loop?: boolean }) => Node,
44
+ * dispose: () => void }>}
45
+ * duration is in seconds. A node is w by h px (default: the animation's size) and plays from its local time, or
46
+ * from `time`; loop repeats it, otherwise the last frame holds.
47
+ */
48
+ export async function lottie(lottieWeb, { data }) {
49
+ const player = lottieWeb && 'loadAnimation' in lottieWeb ? lottieWeb : lottieWeb?.default
50
+ if (typeof player?.loadAnimation !== 'function') throw new TypeError('lottie: pass lottie-web, as in import lottieWeb from \'lottie-web\'')
51
+ if (!data || !(data.fr > 0) || !(data.op > data.ip) || !(data.w > 0 && data.h > 0)) throw new TypeError('lottie: data must be a parsed Lottie JSON with fr, ip, op, w and h')
52
+ const fps = data.fr
53
+ const total = data.op - data.ip
54
+ /** @type {[number, number]} */
55
+ const size = [data.w, data.h]
56
+ // lottie-web expects a page, so a canvas element rather than an OffscreenCanvas.
57
+ const canvas = document.createElement('canvas')
58
+ canvas.width = data.w
59
+ canvas.height = data.h
60
+ const context = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'))
61
+ // The typings ask for a container element, which the canvas renderer does without when it gets a context.
62
+ const config = /** @type {import('lottie-web').AnimationConfigWithData<'canvas'>} */ (/** @type {unknown} */ ({
63
+ renderer: 'canvas', loop: false, autoplay: false, animationData: data,
64
+ rendererSettings: { context, clearCanvas: true, preserveAspectRatio: 'xMidYMid meet', dpr: 1 },
65
+ }))
66
+ const anim = player.loadAnimation(config)
67
+ // Fonts and images load in the background, and drawing is synchronous, so wait for them here.
68
+ if (!anim.isLoaded) {
69
+ await new Promise((done, reject) => {
70
+ anim.addEventListener('DOMLoaded', () => done(undefined))
71
+ anim.addEventListener('data_failed', () => reject(new Error('lottie: animation assets failed to load')))
72
+ })
73
+ }
74
+
75
+ let disposed = false
76
+ return {
77
+ duration: total / fps,
78
+ frameRate: fps,
79
+ size,
80
+ dispose() {
81
+ if (disposed) return
82
+ disposed = true
83
+ anim.destroy?.()
84
+ },
85
+ node({ w = size[0], h = size[1], loop = false, ...props } = {}) {
86
+ return canvasLayer({
87
+ ...props,
88
+ w,
89
+ h,
90
+ render(t, width, height) {
91
+ if (canvas.width !== width || canvas.height !== height) {
92
+ // Before resizing, since the resize ends by redrawing the last frame.
93
+ forgetContextState(anim)
94
+ anim.resize(width, height)
95
+ }
96
+ anim.goToAndStop(frameAt(t, fps, total, loop), true)
97
+ return canvas
98
+ },
99
+ })
100
+ },
101
+ }
102
+ }