mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[ts] Closes #2370, added SpineCanvas.dispose and callback.
This commit is contained in:
parent
57388824ca
commit
c198203695
@ -168,6 +168,7 @@
|
|||||||
### WebGL backend
|
### WebGL backend
|
||||||
* `PolygonBatcher` can now disable culling automatically if the static variable `PolygonBatcher.disableCulling` is set to true.
|
* `PolygonBatcher` can now disable culling automatically if the static variable `PolygonBatcher.disableCulling` is set to true.
|
||||||
* Added `SpineCanvas`, a simpler way to render a scene via spine-webgl. See `spine-ts/spine-webgl/examples/barebones.html` and `spine-ts/spine-webgl/examples/mix-and-match.html`.
|
* Added `SpineCanvas`, a simpler way to render a scene via spine-webgl. See `spine-ts/spine-webgl/examples/barebones.html` and `spine-ts/spine-webgl/examples/mix-and-match.html`.
|
||||||
|
* Added `SpineCanavs.dispose()` to halt the updating and rendering of the canvas.
|
||||||
|
|
||||||
### Canvas backend
|
### Canvas backend
|
||||||
* Improved example.
|
* Improved example.
|
||||||
|
|||||||
@ -32,12 +32,12 @@ import { TimeKeeper, AssetManager, ManagedWebGLRenderingContext, SceneRenderer,
|
|||||||
/** An app running inside a {@link SpineCanvas}. The app life-cycle
|
/** An app running inside a {@link SpineCanvas}. The app life-cycle
|
||||||
* is as follows:
|
* is as follows:
|
||||||
*
|
*
|
||||||
* 1. `loadAssets()` is called. The app can queue assets for loading via {@link SpineCanvas#assetManager}.
|
* 1. `loadAssets()` is called. The app can queue assets for loading via {@link SpineCanvas.assetManager}.
|
||||||
* 2. `initialize()` is called when all assets are loaded. The app can setup anything it needs to enter the main application logic.
|
* 2. `initialize()` is called when all assets are loaded. The app can setup anything it needs to enter the main application logic.
|
||||||
* 3. `update()` is called periodically at screen refresh rate. The app can update its state.
|
* 3. `update()` is called periodically at screen refresh rate. The app can update its state.
|
||||||
* 4. `render()` is called periodically at screen refresh rate. The app can render its state via {@link SpineCanvas#renderer} or directly via the WebGL context in {@link SpineCanvas.gl}`
|
* 4. `render()` is called periodically at screen refresh rate. The app can render its state via {@link SpineCanvas.renderer} or directly via the WebGL context in {@link SpineCanvas.gl}.
|
||||||
*
|
*
|
||||||
* The `error()` method is called in case the assets could not be loaded.
|
* The `error()` method is called in case the assets could not be loaded. The `dispose()` method is called in case the canvas has been disposed via {@link SpineCanvas.dispose}.
|
||||||
*/
|
*/
|
||||||
export interface SpineCanvasApp {
|
export interface SpineCanvasApp {
|
||||||
loadAssets?(canvas: SpineCanvas): void;
|
loadAssets?(canvas: SpineCanvas): void;
|
||||||
@ -45,6 +45,7 @@ export interface SpineCanvasApp {
|
|||||||
update?(canvas: SpineCanvas, delta: number): void;
|
update?(canvas: SpineCanvas, delta: number): void;
|
||||||
render?(canvas: SpineCanvas): void;
|
render?(canvas: SpineCanvas): void;
|
||||||
error?(canvas: SpineCanvas, errors: StringMap<string>): void;
|
error?(canvas: SpineCanvas, errors: StringMap<string>): void;
|
||||||
|
dispose?(canvas: SpineCanvas): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Configuration passed to the {@link SpineCanvas} constructor */
|
/** Configuration passed to the {@link SpineCanvas} constructor */
|
||||||
@ -75,8 +76,10 @@ export class SpineCanvas {
|
|||||||
/** The input processor used to listen to mouse, touch, and keyboard events. */
|
/** The input processor used to listen to mouse, touch, and keyboard events. */
|
||||||
readonly input: Input;
|
readonly input: Input;
|
||||||
|
|
||||||
|
private disposed = false;
|
||||||
|
|
||||||
/** Constructs a new spine canvas, rendering to the provided HTML canvas. */
|
/** Constructs a new spine canvas, rendering to the provided HTML canvas. */
|
||||||
constructor (canvas: HTMLCanvasElement, config: SpineCanvasConfig) {
|
constructor (canvas: HTMLCanvasElement, private config: SpineCanvasConfig) {
|
||||||
if (!config.pathPrefix) config.pathPrefix = "";
|
if (!config.pathPrefix) config.pathPrefix = "";
|
||||||
if (!config.app) config.app = {
|
if (!config.app) config.app = {
|
||||||
loadAssets: () => { },
|
loadAssets: () => { },
|
||||||
@ -84,6 +87,7 @@ export class SpineCanvas {
|
|||||||
update: () => { },
|
update: () => { },
|
||||||
render: () => { },
|
render: () => { },
|
||||||
error: () => { },
|
error: () => { },
|
||||||
|
dispose: () => { },
|
||||||
}
|
}
|
||||||
if (config.webglConfig) config.webglConfig = { alpha: true };
|
if (config.webglConfig) config.webglConfig = { alpha: true };
|
||||||
|
|
||||||
@ -97,6 +101,7 @@ export class SpineCanvas {
|
|||||||
if (config.app.loadAssets) config.app.loadAssets(this);
|
if (config.app.loadAssets) config.app.loadAssets(this);
|
||||||
|
|
||||||
let loop = () => {
|
let loop = () => {
|
||||||
|
if (this.disposed) return;
|
||||||
requestAnimationFrame(loop);
|
requestAnimationFrame(loop);
|
||||||
this.time.update();
|
this.time.update();
|
||||||
if (config.app.update) config.app.update(this, this.time.delta);
|
if (config.app.update) config.app.update(this, this.time.delta);
|
||||||
@ -104,6 +109,7 @@ export class SpineCanvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let waitForAssets = () => {
|
let waitForAssets = () => {
|
||||||
|
if (this.disposed) return;
|
||||||
if (this.assetManager.isLoadingComplete()) {
|
if (this.assetManager.isLoadingComplete()) {
|
||||||
if (this.assetManager.hasErrors()) {
|
if (this.assetManager.hasErrors()) {
|
||||||
if (config.app.error) config.app.error(this, this.assetManager.getErrors());
|
if (config.app.error) config.app.error(this, this.assetManager.getErrors());
|
||||||
@ -123,4 +129,10 @@ export class SpineCanvas {
|
|||||||
this.gl.clearColor(r, g, b, a);
|
this.gl.clearColor(r, g, b, a);
|
||||||
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Disposes the app, so the update() and render() functions are no longer called. Calls the dispose() callback.*/
|
||||||
|
dispose() {
|
||||||
|
if (this.config.app.dispose) this.config.app.dispose(this);
|
||||||
|
this.disposed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user