@openbone/player 0.4.0 → 0.4.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/README.md +6 -4
- package/dist/bundle/webview.d.ts +1 -1
- package/dist/bundle/webview.js +2 -1
- package/dist/element.d.ts +6 -1
- package/dist/element.js +22 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/player.d.ts +51 -2
- package/dist/player.js +114 -7
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -69,14 +69,16 @@ Images and sounds load relative to the document's URL. The element is 400 × 300
|
|
|
69
69
|
| `state-machine` | first | State machine to run, by name or id. |
|
|
70
70
|
| `animation` | | Play this animation instead of a state machine. |
|
|
71
71
|
| `skin` | default skin | Skin (outfit) by name or id. Changing it switches the outfit without reloading. |
|
|
72
|
-
| `fit` | `contain` | `contain`, `cover` or `none` (actual size, centred). |
|
|
72
|
+
| `fit` | `contain` | `contain`, `cover`, `auto` (cover when it crops at most 15% of a side, contain otherwise) or `none` (actual size, centred). |
|
|
73
|
+
| `clip` | on | Only the artboard is drawn: art past its edges is cut off, the bars around it stay transparent and clicks there are ignored. `clip="false"` draws the whole canvas. |
|
|
74
|
+
| `fullscreen-fit` | `auto` | Fit while the player (or a parent) is full screen. `auto` fills the screen when that crops only a little of the artboard and shows all of it otherwise; `cover`, `contain`, `none`, or `same` to keep `fit`. |
|
|
73
75
|
| `renderer` | `webgl` | `webgl` or `canvas`. WebGL falls back to Canvas 2D when WebGL2 is not available. |
|
|
74
76
|
| `background` | the file's | Any CSS colour (`#fff`, `rebeccapurple`, `rgb(0 0 0 / 50%)`, …), or `transparent` / `none`. |
|
|
75
77
|
| `autoplay` | on | `autoplay="false"` loads and draws the first frame, then waits for `play()`. |
|
|
76
78
|
| `scripts` | on | `scripts="none"` never runs the document's scripts. |
|
|
77
79
|
| `audio` / `muted` | on | `audio="off"` disables sound; `muted` loads sounds but starts muted. |
|
|
78
80
|
|
|
79
|
-
Changing `skin`
|
|
81
|
+
Changing `skin`, `clip` or `fullscreen-fit` applies in place. Changing any other attribute, including setting `src` after the element is
|
|
80
82
|
on the page, reloads the player with the new settings.
|
|
81
83
|
|
|
82
84
|
### Element methods and events
|
|
@@ -128,14 +130,14 @@ player.speed = 0.5;
|
|
|
128
130
|
| `src` | A URL, or an already parsed `OpenBoneDocument`. |
|
|
129
131
|
| `baseUrl` | Folder for relative image and audio paths (default: the folder of `src`). |
|
|
130
132
|
| `resolveImageUrl(src)`, `resolveAudioUrl(asset)` | Map asset paths to URLs yourself (overrides `baseUrl`). |
|
|
131
|
-
| `stateMachine`, `animation`, `skin`, `autoplay`, `fit`, `renderer`, `background` | As the attributes above. `background: null` is transparent
|
|
133
|
+
| `stateMachine`, `animation`, `skin`, `autoplay`, `fit`, `clip`, `fullscreenFit`, `renderer`, `background` | As the attributes above. `background: null` is transparent; `fullscreenFit: null` keeps `fit`. |
|
|
132
134
|
| `scripts` | `"quickjs"` (default when the document has scripts), `"none"`, or your own `ScriptHost`. |
|
|
133
135
|
| `audio` | `true` (default), `false` or `"muted"`. |
|
|
134
136
|
| `audioContext` | Share an existing `AudioContext` instead of creating one. |
|
|
135
137
|
| `onEvent`, `onStateChange`, `onLoad` | Callbacks. |
|
|
136
138
|
|
|
137
139
|
Members: `ready` (promise), `play()`, `pause()`, `playing`, `speed` (sounds follow it), `setInput`, `getInput`,
|
|
138
|
-
`fire`, `setText`, `setSkin`, `skins`, `playAnimation(name, loop?)`, `setFit(fit)`, `setMuted(muted)`, `muted`,
|
|
140
|
+
`fire`, `setText`, `setSkin`, `skins`, `playAnimation(name, loop?)`, `setFit(fit)`, `setFullscreenFit(fit)`, `setClip(clip)`, `currentFit`, `setMuted(muted)`, `muted`,
|
|
139
141
|
`audio`, `instance` (the runtime `OpenBoneInstance`), `stateMachine`, `doc`, `clientToSkeleton(clientX, clientY)`,
|
|
140
142
|
`boundingBoxes()` and `dispose()`.
|
|
141
143
|
|
package/dist/bundle/webview.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - `?src=url` in the page URL (fetched; needs http(s), not file://)
|
|
7
7
|
* - `window.OPENBONE_SRC` (a URL or a parsed document)
|
|
8
8
|
* Options come from `data-*` attributes on `<body>`: data-state-machine, data-animation, data-skin, data-fit,
|
|
9
|
-
* data-background ("none" = transparent), data-renderer.
|
|
9
|
+
* data-background ("none" = transparent), data-renderer, data-clip ("false" draws past the artboard's edges).
|
|
10
10
|
*
|
|
11
11
|
* It exposes `window.OpenBone` (see bridge.ts for the message format).
|
|
12
12
|
*/
|
package/dist/bundle/webview.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - `?src=url` in the page URL (fetched; needs http(s), not file://)
|
|
7
7
|
* - `window.OPENBONE_SRC` (a URL or a parsed document)
|
|
8
8
|
* Options come from `data-*` attributes on `<body>`: data-state-machine, data-animation, data-skin, data-fit,
|
|
9
|
-
* data-background ("none" = transparent), data-renderer.
|
|
9
|
+
* data-background ("none" = transparent), data-renderer, data-clip ("false" draws past the artboard's edges).
|
|
10
10
|
*
|
|
11
11
|
* It exposes `window.OpenBone` (see bridge.ts for the message format).
|
|
12
12
|
*/
|
|
@@ -132,6 +132,7 @@ function start() {
|
|
|
132
132
|
animation: opt("animation"),
|
|
133
133
|
skin: opt("skin"),
|
|
134
134
|
fit: opt("fit") ?? "contain",
|
|
135
|
+
clip: opt("clip") !== "false",
|
|
135
136
|
renderer: opt("renderer") ?? "webgl",
|
|
136
137
|
...(bg !== undefined ? { background: bg === "none" ? null : bg } : {}),
|
|
137
138
|
scripts: host ?? "none",
|
package/dist/element.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { Player } from "./player.js";
|
|
|
6
6
|
* setMuted(muted), setBusMuted(bus, muted | "toggle"), toggleBus(bus), isBusMuted(bus), buses, player (the underlying Player).
|
|
7
7
|
* Audio plays after the first click/key press; `muted` attribute starts muted, `audio="off"` disables it.
|
|
8
8
|
* Changing the `skin` attribute switches the outfit without reloading.
|
|
9
|
+
* Only the artboard is drawn (anything past its edges is cut off); `clip="false"` draws the whole canvas. Full screen
|
|
10
|
+
* fills the screen when that crops only a little (`fullscreen-fit`, default "auto"; "same" keeps `fit`). Both change without reloading.
|
|
9
11
|
*/
|
|
10
12
|
export declare class OpenBonePlayerElement extends HTMLElement {
|
|
11
13
|
/** `skin` switches skins in place; any other of these reloads the player with the new settings. */
|
|
@@ -21,9 +23,12 @@ export declare class OpenBonePlayerElement extends HTMLElement {
|
|
|
21
23
|
connectedCallback(): void;
|
|
22
24
|
disconnectedCallback(): void;
|
|
23
25
|
attributeChangedCallback(n: string, old: string | null, val: string | null): void;
|
|
24
|
-
/** The attributes the current player was built with (everything observed except
|
|
26
|
+
/** The attributes the current player was built with (everything observed except those that change in place). */
|
|
25
27
|
private loadedWith;
|
|
26
28
|
private settings;
|
|
29
|
+
private clip;
|
|
30
|
+
/** `fullscreen-fit`: contain / cover / auto / none; "same" (or "") = the `fit` attribute; absent = auto. */
|
|
31
|
+
private fullscreenFit;
|
|
27
32
|
private init;
|
|
28
33
|
/** `background` attribute: a colour, "transparent"/"none" for no background, absent for the file's own. */
|
|
29
34
|
private background;
|
package/dist/element.js
CHANGED
|
@@ -6,10 +6,12 @@ import { Player } from "./player.js";
|
|
|
6
6
|
* setMuted(muted), setBusMuted(bus, muted | "toggle"), toggleBus(bus), isBusMuted(bus), buses, player (the underlying Player).
|
|
7
7
|
* Audio plays after the first click/key press; `muted` attribute starts muted, `audio="off"` disables it.
|
|
8
8
|
* Changing the `skin` attribute switches the outfit without reloading.
|
|
9
|
+
* Only the artboard is drawn (anything past its edges is cut off); `clip="false"` draws the whole canvas. Full screen
|
|
10
|
+
* fills the screen when that crops only a little (`fullscreen-fit`, default "auto"; "same" keeps `fit`). Both change without reloading.
|
|
9
11
|
*/
|
|
10
12
|
export class OpenBonePlayerElement extends HTMLElement {
|
|
11
13
|
/** `skin` switches skins in place; any other of these reloads the player with the new settings. */
|
|
12
|
-
static observedAttributes = ["src", "skin", "animation", "state-machine", "fit", "renderer", "autoplay", "background", "scripts", "audio", "muted"];
|
|
14
|
+
static observedAttributes = ["src", "skin", "clip", "fullscreen-fit", "animation", "state-machine", "fit", "renderer", "autoplay", "background", "scripts", "audio", "muted"];
|
|
13
15
|
player = null;
|
|
14
16
|
/**
|
|
15
17
|
* An AudioContext to play through instead of creating one, set before `src`. Create and resume it in a click
|
|
@@ -38,15 +40,30 @@ export class OpenBonePlayerElement extends HTMLElement {
|
|
|
38
40
|
return;
|
|
39
41
|
if (n === "skin")
|
|
40
42
|
this.player?.setSkin(val);
|
|
43
|
+
else if (n === "clip")
|
|
44
|
+
this.player?.setClip(this.clip());
|
|
45
|
+
else if (n === "fullscreen-fit")
|
|
46
|
+
this.player?.setFullscreenFit(this.fullscreenFit() ?? (val === null ? "auto" : null));
|
|
41
47
|
// Includes `src` set for the first time after the element was added to the page. Upgrading an element reports each
|
|
42
48
|
// attribute it already has; those match what the player was built with, so they don't reload it.
|
|
43
49
|
else if (this.settings() !== this.loadedWith)
|
|
44
50
|
this.init();
|
|
45
51
|
}
|
|
46
|
-
/** The attributes the current player was built with (everything observed except
|
|
52
|
+
/** The attributes the current player was built with (everything observed except those that change in place). */
|
|
47
53
|
loadedWith;
|
|
48
54
|
settings() {
|
|
49
|
-
|
|
55
|
+
const live = ["skin", "clip", "fullscreen-fit"];
|
|
56
|
+
return JSON.stringify(OpenBonePlayerElement.observedAttributes.filter((a) => !live.includes(a)).map((a) => this.getAttribute(a)));
|
|
57
|
+
}
|
|
58
|
+
clip() {
|
|
59
|
+
return this.getAttribute("clip") !== "false";
|
|
60
|
+
}
|
|
61
|
+
/** `fullscreen-fit`: contain / cover / auto / none; "same" (or "") = the `fit` attribute; absent = auto. */
|
|
62
|
+
fullscreenFit() {
|
|
63
|
+
const v = this.getAttribute("fullscreen-fit");
|
|
64
|
+
if (v === null)
|
|
65
|
+
return undefined;
|
|
66
|
+
return v === "contain" || v === "cover" || v === "auto" || v === "none" ? v : null;
|
|
50
67
|
}
|
|
51
68
|
init() {
|
|
52
69
|
const src = this.getAttribute("src");
|
|
@@ -62,6 +79,8 @@ export class OpenBonePlayerElement extends HTMLElement {
|
|
|
62
79
|
animation: this.getAttribute("animation") ?? undefined,
|
|
63
80
|
skin: this.getAttribute("skin") ?? undefined,
|
|
64
81
|
fit: this.getAttribute("fit") ?? "contain",
|
|
82
|
+
fullscreenFit: this.fullscreenFit(),
|
|
83
|
+
clip: this.clip(),
|
|
65
84
|
renderer: this.getAttribute("renderer") ?? "webgl",
|
|
66
85
|
autoplay: this.getAttribute("autoplay") !== "false",
|
|
67
86
|
background: this.background(),
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { Player, type PlayerOptions, setScriptHostFactory } from "./player.js";
|
|
1
|
+
export { type Fit, Player, type PlayerOptions, artboardRect, fitView, setScriptHostFactory } from "./player.js";
|
|
2
2
|
export { OpenBonePlayerElement, definePlayerElement } from "./element.js";
|
|
3
3
|
export { WebAudioBackend, type WebAudioOptions } from "./audio.js";
|
package/dist/index.js
CHANGED
package/dist/player.d.ts
CHANGED
|
@@ -15,7 +15,18 @@ export interface PlayerOptions {
|
|
|
15
15
|
/** "quickjs" (sandboxed, default when the doc has scripts), "none", or a custom host. */
|
|
16
16
|
scripts?: "quickjs" | "none" | ScriptHost;
|
|
17
17
|
renderer?: "webgl" | "canvas";
|
|
18
|
-
|
|
18
|
+
/** How the artboard fits the canvas (default "contain": all of it, with empty bars on the longer sides). */
|
|
19
|
+
fit?: Fit;
|
|
20
|
+
/**
|
|
21
|
+
* Fit while the canvas is shown full screen (default "auto": fill the screen when that crops only a little of the
|
|
22
|
+
* artboard, otherwise show all of it). null keeps `fit`.
|
|
23
|
+
*/
|
|
24
|
+
fullscreenFit?: Fit | null;
|
|
25
|
+
/**
|
|
26
|
+
* Draw only inside the artboard (default true), like a camera: anything placed past its edges is cut off, the bars
|
|
27
|
+
* around it stay clear (the page shows through) and clicks there are ignored. false draws everything on the canvas.
|
|
28
|
+
*/
|
|
29
|
+
clip?: boolean;
|
|
19
30
|
background?: string | null;
|
|
20
31
|
/** Play the document's sounds (default true). "muted" loads them but starts muted. Audio unlocks on the first click or key press. */
|
|
21
32
|
audio?: boolean | "muted";
|
|
@@ -33,6 +44,33 @@ export interface PlayerOptions {
|
|
|
33
44
|
}) => void;
|
|
34
45
|
onLoad?: (p: Player) => void;
|
|
35
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* contain: all of the artboard, with bars on the longer sides. cover: fill the canvas, cropping the artboard's edges.
|
|
49
|
+
* auto: cover when that crops at most AUTO_CROP of the artboard's width or height, contain otherwise (so buttons near
|
|
50
|
+
* the edges of the art never get cut off on a very different screen shape). none: actual size, centred.
|
|
51
|
+
*/
|
|
52
|
+
export type Fit = "contain" | "cover" | "auto" | "none";
|
|
53
|
+
/** The most of the artboard's width or height `auto` will crop to fill the canvas. */
|
|
54
|
+
export declare const AUTO_CROP = 0.15;
|
|
55
|
+
/** Where an artboard of aw × ah lands on a canvas of cw × ch (CSS pixels) for a fit: zoom and top-left offset. */
|
|
56
|
+
export declare function fitView(cw: number, ch: number, aw: number, ah: number, fit: Fit): {
|
|
57
|
+
zoom: number;
|
|
58
|
+
x: number;
|
|
59
|
+
y: number;
|
|
60
|
+
};
|
|
61
|
+
/** The canvas rectangle (device pixels, y down) the artboard covers, cut to the canvas; null when none of it shows. */
|
|
62
|
+
export declare function artboardRect(view: {
|
|
63
|
+
x: number;
|
|
64
|
+
y: number;
|
|
65
|
+
zoom: number;
|
|
66
|
+
}, aw: number, ah: number, cw: number, ch: number): {
|
|
67
|
+
x: number;
|
|
68
|
+
y: number;
|
|
69
|
+
w: number;
|
|
70
|
+
h: number;
|
|
71
|
+
} | null;
|
|
72
|
+
/** Whether `node` is the full-screen element or inside it, across shadow roots (a player element's canvas is in one). */
|
|
73
|
+
export declare function isInFullscreen(node: Node): boolean;
|
|
36
74
|
/**
|
|
37
75
|
* Replace how players create the sandbox for documents with scripts (default: load `@openbone/scripting`).
|
|
38
76
|
* Bundles use it to supply an inlined QuickJS build, or `() => Promise.resolve(null)` to run without scripts.
|
|
@@ -50,6 +88,7 @@ export declare class Player {
|
|
|
50
88
|
private c2d;
|
|
51
89
|
private view;
|
|
52
90
|
private ro;
|
|
91
|
+
private onFullscreen;
|
|
53
92
|
playing: boolean;
|
|
54
93
|
/** Playback rate (1 = real time). */
|
|
55
94
|
private _speed;
|
|
@@ -64,6 +103,11 @@ export declare class Player {
|
|
|
64
103
|
private load;
|
|
65
104
|
private setupRenderer;
|
|
66
105
|
private resize;
|
|
106
|
+
/** The fit in use now: `fullscreenFit` while the canvas is full screen, otherwise `fit`. */
|
|
107
|
+
get currentFit(): Fit;
|
|
108
|
+
private get clipping();
|
|
109
|
+
/** Whether page coordinates fall on the artboard (always true with clip off, where the whole canvas is live). */
|
|
110
|
+
private onArtboard;
|
|
67
111
|
private toSkeleton;
|
|
68
112
|
/** Page (client) coordinates, e.g. a pointer event's clientX/clientY, to skeleton space (y up), through the current fit. */
|
|
69
113
|
clientToSkeleton(clientX: number, clientY: number): [number, number];
|
|
@@ -74,9 +118,14 @@ export declare class Player {
|
|
|
74
118
|
boundingBoxes(): SkeletonBounds;
|
|
75
119
|
private bindPointer;
|
|
76
120
|
private draw;
|
|
121
|
+
private clearAll;
|
|
77
122
|
play(): void;
|
|
78
123
|
/** Change how the artboard fits the canvas ("none" = actual size, centred). */
|
|
79
|
-
setFit(fit:
|
|
124
|
+
setFit(fit: Fit): void;
|
|
125
|
+
/** Change the fit used while full screen (null = the same as `fit`). */
|
|
126
|
+
setFullscreenFit(fit: Fit | null): void;
|
|
127
|
+
/** Draw only inside the artboard (true, the default) or everything on the canvas (false). */
|
|
128
|
+
setClip(clip: boolean): void;
|
|
80
129
|
/** Pause the animation and its sounds (play() continues both from where they were). */
|
|
81
130
|
pause(): void;
|
|
82
131
|
/** Mute or unmute this player's sounds. */
|
package/dist/player.js
CHANGED
|
@@ -3,6 +3,34 @@ import { CanvasRenderer } from "@openbone/render-canvas";
|
|
|
3
3
|
import { WebGLRenderer } from "@openbone/render-webgl";
|
|
4
4
|
import { OpenBoneInstance } from "@openbone/runtime";
|
|
5
5
|
import { WebAudioBackend } from "./audio.js";
|
|
6
|
+
/** The most of the artboard's width or height `auto` will crop to fill the canvas. */
|
|
7
|
+
export const AUTO_CROP = 0.15;
|
|
8
|
+
/** Where an artboard of aw × ah lands on a canvas of cw × ch (CSS pixels) for a fit: zoom and top-left offset. */
|
|
9
|
+
export function fitView(cw, ch, aw, ah, fit) {
|
|
10
|
+
const contain = Math.min(cw / aw, ch / ah);
|
|
11
|
+
const cover = Math.max(cw / aw, ch / ah);
|
|
12
|
+
const zoom = fit === "none" ? 1 : fit === "contain" ? contain : fit === "cover" || 1 - contain / cover <= AUTO_CROP + 1e-9 ? cover : contain;
|
|
13
|
+
return { zoom, x: (cw - aw * zoom) / 2, y: (ch - ah * zoom) / 2 };
|
|
14
|
+
}
|
|
15
|
+
/** The canvas rectangle (device pixels, y down) the artboard covers, cut to the canvas; null when none of it shows. */
|
|
16
|
+
export function artboardRect(view, aw, ah, cw, ch) {
|
|
17
|
+
const x0 = Math.max(0, Math.round(view.x));
|
|
18
|
+
const y0 = Math.max(0, Math.round(view.y));
|
|
19
|
+
const x1 = Math.min(cw, Math.round(view.x + aw * view.zoom));
|
|
20
|
+
const y1 = Math.min(ch, Math.round(view.y + ah * view.zoom));
|
|
21
|
+
return x1 > x0 && y1 > y0 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : null;
|
|
22
|
+
}
|
|
23
|
+
const fullscreenElement = (d) => d.fullscreenElement ?? d.webkitFullscreenElement ?? null;
|
|
24
|
+
/** Whether `node` is the full-screen element or inside it, across shadow roots (a player element's canvas is in one). */
|
|
25
|
+
export function isInFullscreen(node) {
|
|
26
|
+
const fs = node.ownerDocument ? fullscreenElement(node.ownerDocument) : null;
|
|
27
|
+
if (!fs)
|
|
28
|
+
return false;
|
|
29
|
+
for (let n = node; n; n = n.parentNode ?? n.host ?? null)
|
|
30
|
+
if (n === fs)
|
|
31
|
+
return true;
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
6
34
|
let scriptHostFactory = null;
|
|
7
35
|
/**
|
|
8
36
|
* Replace how players create the sandbox for documents with scripts (default: load `@openbone/scripting`).
|
|
@@ -23,6 +51,7 @@ export class Player {
|
|
|
23
51
|
c2d = null;
|
|
24
52
|
view = { x: 0, y: 0, zoom: 1 };
|
|
25
53
|
ro = null;
|
|
54
|
+
onFullscreen = () => this.doc && this.resize();
|
|
26
55
|
playing = false;
|
|
27
56
|
/** Playback rate (1 = real time). */
|
|
28
57
|
_speed = 1;
|
|
@@ -115,6 +144,9 @@ export class Player {
|
|
|
115
144
|
this.bindPointer();
|
|
116
145
|
this.ro = new ResizeObserver(() => this.resize());
|
|
117
146
|
this.ro.observe(o.canvas);
|
|
147
|
+
const d = o.canvas.ownerDocument ?? document;
|
|
148
|
+
d.addEventListener("fullscreenchange", this.onFullscreen);
|
|
149
|
+
d.addEventListener("webkitfullscreenchange", this.onFullscreen);
|
|
118
150
|
this.resize();
|
|
119
151
|
this.instance.advance(0);
|
|
120
152
|
this.draw();
|
|
@@ -142,12 +174,27 @@ export class Player {
|
|
|
142
174
|
const h = c.clientHeight || this.doc.height;
|
|
143
175
|
c.width = Math.round(w * dpr);
|
|
144
176
|
c.height = Math.round(h * dpr);
|
|
145
|
-
|
|
146
|
-
const z = fit === "none" ? 1 : (fit === "cover" ? Math.max : Math.min)(w / this.doc.width, h / this.doc.height);
|
|
147
|
-
this.view = { zoom: z, x: (w - this.doc.width * z) / 2, y: (h - this.doc.height * z) / 2 };
|
|
177
|
+
this.view = fitView(w, h, this.doc.width, this.doc.height, this.currentFit);
|
|
148
178
|
if (!this.playing)
|
|
149
179
|
this.draw();
|
|
150
180
|
}
|
|
181
|
+
/** The fit in use now: `fullscreenFit` while the canvas is full screen, otherwise `fit`. */
|
|
182
|
+
get currentFit() {
|
|
183
|
+
const fs = this.opts.fullscreenFit === undefined ? "auto" : this.opts.fullscreenFit;
|
|
184
|
+
return (fs && isInFullscreen(this.opts.canvas) ? fs : this.opts.fit) ?? "contain";
|
|
185
|
+
}
|
|
186
|
+
get clipping() {
|
|
187
|
+
return this.opts.clip !== false;
|
|
188
|
+
}
|
|
189
|
+
/** Whether page coordinates fall on the artboard (always true with clip off, where the whole canvas is live). */
|
|
190
|
+
onArtboard(clientX, clientY) {
|
|
191
|
+
if (!this.clipping)
|
|
192
|
+
return true;
|
|
193
|
+
const r = this.opts.canvas.getBoundingClientRect();
|
|
194
|
+
const ax = (clientX - r.left - this.view.x) / this.view.zoom;
|
|
195
|
+
const ay = (clientY - r.top - this.view.y) / this.view.zoom;
|
|
196
|
+
return ax >= 0 && ay >= 0 && ax <= this.doc.width && ay <= this.doc.height;
|
|
197
|
+
}
|
|
151
198
|
toSkeleton(e) {
|
|
152
199
|
return this.clientToSkeleton(e.clientX, e.clientY);
|
|
153
200
|
}
|
|
@@ -167,7 +214,13 @@ export class Player {
|
|
|
167
214
|
}
|
|
168
215
|
bindPointer() {
|
|
169
216
|
const c = this.opts.canvas;
|
|
170
|
-
|
|
217
|
+
// Past the artboard's edges (with clip on) nothing is drawn, so nothing there can be hit: a move there leaves it.
|
|
218
|
+
const send = (type) => (e) => {
|
|
219
|
+
if (this.onArtboard(e.clientX, e.clientY))
|
|
220
|
+
this.instance.pointer(type, ...this.toSkeleton(e));
|
|
221
|
+
else if (type === "move")
|
|
222
|
+
this.instance.pointer("move", -1e9, -1e9);
|
|
223
|
+
};
|
|
171
224
|
c.addEventListener("pointerdown", send("down"));
|
|
172
225
|
c.addEventListener("pointerup", send("up"));
|
|
173
226
|
c.addEventListener("pointermove", send("move"));
|
|
@@ -177,19 +230,58 @@ export class Player {
|
|
|
177
230
|
const dpr = window.devicePixelRatio || 1;
|
|
178
231
|
const v = { x: this.view.x * dpr, y: this.view.y * dpr, zoom: this.view.zoom * dpr };
|
|
179
232
|
const bg = this.opts.background === undefined ? this.doc.background : this.opts.background;
|
|
233
|
+
const cw = this.opts.canvas.width;
|
|
234
|
+
const ch = this.opts.canvas.height;
|
|
235
|
+
// With clip on, the background and the art fill the artboard only; the bars around it stay transparent.
|
|
236
|
+
const clip = this.clipping ? artboardRect(v, this.doc.width, this.doc.height, cw, ch) : null;
|
|
237
|
+
if (this.clipping && !clip) {
|
|
238
|
+
this.clearAll();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
180
241
|
if (this.gl) {
|
|
181
242
|
const col = bg ? colorToGl(bg) : [0, 0, 0, 0];
|
|
182
|
-
this.gl.
|
|
243
|
+
const gl = this.gl.gl;
|
|
244
|
+
if (clip) {
|
|
245
|
+
this.clearAll();
|
|
246
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
247
|
+
gl.scissor(clip.x, ch - clip.y - clip.h, clip.w, clip.h); // GL counts y from the bottom
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
this.gl.render(this.instance.skeleton, v, col);
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
if (clip)
|
|
254
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
255
|
+
}
|
|
183
256
|
}
|
|
184
257
|
else if (this.c2d) {
|
|
185
258
|
const ctx = this.c2d.ctx;
|
|
186
259
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
187
|
-
ctx.clearRect(0, 0,
|
|
260
|
+
ctx.clearRect(0, 0, cw, ch);
|
|
261
|
+
ctx.save();
|
|
262
|
+
if (clip) {
|
|
263
|
+
ctx.beginPath();
|
|
264
|
+
ctx.rect(clip.x, clip.y, clip.w, clip.h);
|
|
265
|
+
ctx.clip();
|
|
266
|
+
}
|
|
188
267
|
if (bg) {
|
|
189
268
|
ctx.fillStyle = bg;
|
|
190
|
-
ctx.fillRect(0, 0,
|
|
269
|
+
ctx.fillRect(0, 0, cw, ch);
|
|
191
270
|
}
|
|
192
271
|
this.c2d.render(this.instance.skeleton, { view: v, clear: false });
|
|
272
|
+
ctx.restore();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
clearAll() {
|
|
276
|
+
if (this.gl) {
|
|
277
|
+
const gl = this.gl.gl;
|
|
278
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
279
|
+
gl.clearColor(0, 0, 0, 0);
|
|
280
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
281
|
+
}
|
|
282
|
+
else if (this.c2d) {
|
|
283
|
+
this.c2d.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
284
|
+
this.c2d.ctx.clearRect(0, 0, this.opts.canvas.width, this.opts.canvas.height);
|
|
193
285
|
}
|
|
194
286
|
}
|
|
195
287
|
play() {
|
|
@@ -215,6 +307,18 @@ export class Player {
|
|
|
215
307
|
if (this.doc)
|
|
216
308
|
this.resize();
|
|
217
309
|
}
|
|
310
|
+
/** Change the fit used while full screen (null = the same as `fit`). */
|
|
311
|
+
setFullscreenFit(fit) {
|
|
312
|
+
this.opts.fullscreenFit = fit;
|
|
313
|
+
if (this.doc)
|
|
314
|
+
this.resize();
|
|
315
|
+
}
|
|
316
|
+
/** Draw only inside the artboard (true, the default) or everything on the canvas (false). */
|
|
317
|
+
setClip(clip) {
|
|
318
|
+
this.opts.clip = clip;
|
|
319
|
+
if (this.doc && !this.playing)
|
|
320
|
+
this.draw();
|
|
321
|
+
}
|
|
218
322
|
/** Pause the animation and its sounds (play() continues both from where they were). */
|
|
219
323
|
pause() {
|
|
220
324
|
this.playing = false;
|
|
@@ -291,6 +395,9 @@ export class Player {
|
|
|
291
395
|
dispose() {
|
|
292
396
|
this.pause();
|
|
293
397
|
this.ro?.disconnect();
|
|
398
|
+
const d = this.opts.canvas.ownerDocument ?? document;
|
|
399
|
+
d.removeEventListener("fullscreenchange", this.onFullscreen);
|
|
400
|
+
d.removeEventListener("webkitfullscreenchange", this.onFullscreen);
|
|
294
401
|
this.instance?.dispose();
|
|
295
402
|
this.audio?.dispose();
|
|
296
403
|
this.gl?.dispose();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openbone/player",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Drop-in <openbone-player> web component and JS API for OpenBone animations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openbone",
|
|
@@ -48,11 +48,11 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"quickjs-emscripten": "^0.32.0",
|
|
51
|
-
"@openbone/
|
|
52
|
-
"@openbone/render-
|
|
53
|
-
"@openbone/
|
|
54
|
-
"@openbone/runtime": "0.4.
|
|
55
|
-
"@openbone/
|
|
51
|
+
"@openbone/render-canvas": "0.4.1",
|
|
52
|
+
"@openbone/render-webgl": "0.4.1",
|
|
53
|
+
"@openbone/core": "0.4.1",
|
|
54
|
+
"@openbone/runtime": "0.4.1",
|
|
55
|
+
"@openbone/scripting": "0.4.1"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"esbuild": ">=0.25.0"
|