mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-04 22:34:53 +08:00
* [ts] poc wegl overlay * [ts] poc wegl overlay * canvas4 * overlay 4 * overlay * overlay fix scroll cut * Zoom fix - WIP horizontal scroll bug see comment at line 495 * Scroll should be resolved * web component * Loading * mostly work - when overflow top/bottom...are > 0, widgets slightly jump on up and down fast scroll. * Renamed * Add OffScreenUpdateBehaviour * Fixed loading spinner example * Translate is done each frame through requestAnimationFrame. * Added clip. * Removed SpineCanvasApp dependency. * Removed code duplication for clipping into div * Refactor drag logic. Drag works with clip too. * WIP - Doc before refactor * WIP - Refactor + animation and skin attribute change will reinit the widget * Modified export script to copy chibi * Fix zoom bug due to screen size remaining the same at different zoom level. * Removed useless changes on other classes. * Removed custom LoadingSpinnerWindget class. Added a new specific method in LoadingSpinner. * Fix 1 pixel misalignment in non clip mode. * Cleaned webcomponent example. * Made span fps display configurable. * Made some changes to make it work on old browsers. * Manage lifecycle for Input and SpineWebComponentOverlay. Missing SpineWebComponentWidget. * Managed lifecycle for SpineWebComponentWidget. * Fixed input remove listener. Run tsfmt. * Fixed infinite requestAnimationFrame calls. * Fix x-axis and y-axis not working in clip mode. * Fix widget using overlay before overlay webcomponent is fully upgraded. * Restore resize observer since the window resize event does not fire on body resize, but on window resize. It's supported by 93% of the browsers. * Add padding attributes/properties. * Exposed parameters to set bounds. Deeply changed how bounds work, especially for the fact that they are not auto recalculated anymore if the animation is changed (unless autoRecalculateBounds is set to true). * Changes to make the widget more dynamic while changing attributes. See now webcomponent-gui.html. * Initial support of spine-widget into scrollable containers, and overlay-id for multiple spine-overlay. working * WIP - Added/Changed: - overlay loading: now overlay is moved as the last element from where it is inserted to avoid widgets covered by backgrounds of html elements after it. - default overlayId - widget position in overlay coordinates (worldX, worldY) (experimental) - cursor position on widget world (cursorWorldX, cursorWorldY) (experimental) - jsonSkeletonKey: allow to load a specific skeleton in json containing multiple skeletons - onViewportManualStart: start the widget when in manual-start and enters the viewport the first time - overlayAssignedPromise: a promise that resolves when the overlay is assigned to the widget. Reads the comment on it - appendTo: to append the widget created using js and wait for the overlayAssignedPromise to resolve - changed how loadingPromise works - added cursorCanvasX, cursorCanvasY, cursorWorldX, cursorWorldY to overlay (experimental) * Docs and minor fixes. * Fixed example. * Allow multiple widgets for the same HTMLElement * Simplified clip to div by using scissor rather than changing viewport+camera. In this way we can treat coordinates equally for cliped and not clipped widgets! * Simplified drag calculation and drag debug removing an additional div. * WIP - Add interactivity events. isdraggable is currently broken. * Add interactivity events. * Fixed a bug where an infinite loop occurred in Firefox when compareDocumentPosition results in DOCUMENT_POSITION_DISCONNECTED. In both Chrome and Firefox, when an element is inside a webcomponent the comparison results in DOCUMENT_POSITION_DISCONNECTED. But in Firefox the element result in DOCUMENT_POSITION_FOLLOWING too, leading to an infinite loop. * Fix physicsTranslate y opposite direction while dragging. * Add followSlot method * Add animations, animations-bound and default-mix attributes. * Original event is passed to bounds and slot callbacks. * Made overlay canvas size consistent across different browsers. Base size is not anymore the screen size due to browser limitations, but the window size. This will trigger additional canvas resize on window resize. * Reduce DPI if canvas is too big to avoid page crash - this happen on webpage on mobile with high dpi and missing meta viewport tag with width=device-width. * Pma properties on atlas is used to detect pma textures. * Add rawData attribute to pass s stringified JSON object for inline base64 assets. * format * Prevent useless resize. * Prevent error on disconnected callback * Update tutorial. * Fixed multiple click events on mobile touches. Add team example. * Fixed slot interaction issue. * Resize overlay when follow slot element is added. * Add interactive widget example. * Temporarily add windmill only manually to webgl assets * Move getBounding in scrollable case. * formatter * Fixed overlay disconnectedCallback. * Overlay should load not for DOMContentLoaded event only if document has already complete loading. * Overlay parent bounding box determined only if necessary. * Overlay needs to consider border if it's scrollable. * Fixed issues with slot events on mobile. * food app and cleanup * Make overlayAssignedPromise private. * Change scrollable to appendedToBody, make it private and determine it at dom connect. * scrollable-tweak-off changed to no-auto-parent-transform. * Update bounds and slot interaction method names. * getHTMLElementReference to getHostElement. * recalculateBounds and autoRecalculateBounds to calculateBounds and autoCalculateBounds. * dpi to dpr and transform DPR to DevicePixelRatio when needed. Add @internal tag to internal fields. * Properties rename. * Better comments and variable names for canvas resize related stuff. * callbacks renamed consistently. * for each to for of. * Update other names. * onScreenManualStart to startWhenVisible. startWhenVisible set manualStart to true. * fix return to continue in for of. * Refcounter for asset manager and gl resources disposal for webcomponent. * Fixes to asset manager ref counter. * Add dispose example. * Fix overlay init not completed in some scenario. DOMContentLoaded is invoked only when document.readyState is interactive. * Cleaned up examples. * spinner to no-spinner. * Move webcomponent to spine-widget folder. * start won't reset bounds. * Remove test file. * Moved team example in its own page. * Improved tutorial explanations. * Moved some examples in their own pages. * Formatter. * Widgets dragged can be dragged even if host is offscreen. * General refactor. spine-widget to spine-skeleton. * spine-widget package renamed to spine-webcomponents. * Moved assets to a single assets folder. * Run formatter. * Add beta notice. * Changed widget occurrences to webcomponents.
252 lines
7.7 KiB
HTML
252 lines
7.7 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
</head>
|
|
<script src="../dist/iife/spine-webgl.js"></script>
|
|
<style>
|
|
html,
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
#container {
|
|
display: flex;
|
|
width: 100vw;
|
|
height: 100vh;
|
|
}
|
|
|
|
#skins {
|
|
width: 100px;
|
|
flex-shrink: 0;
|
|
overflow: scroll;
|
|
}
|
|
|
|
#canvas {
|
|
width: calc(100% - 100px);
|
|
}
|
|
</style>
|
|
|
|
<body>
|
|
<div id="container">
|
|
<div id="skins"></div>
|
|
<canvas id="canvas"></canvas>
|
|
</div>
|
|
|
|
<script>
|
|
// Define the class running in the Spine canvas
|
|
class App {
|
|
|
|
constructor() {
|
|
this.canvas = null;
|
|
this.atlas = null;
|
|
this.skeletonData = null;
|
|
this.skeleton = null;
|
|
this.state = null;
|
|
this.selectedSkins = [];
|
|
this.skinThumbnails = {};
|
|
this.lastBounds = {};
|
|
}
|
|
|
|
loadAssets(canvas) {
|
|
canvas.assetManager.AnimationState
|
|
canvas.assetManager.loadTextureAtlas("mix-and-match-pma.atlas");
|
|
canvas.assetManager.loadBinary("mix-and-match-pro.skel");
|
|
}
|
|
|
|
initialize(canvas) {
|
|
this.canvas = canvas;
|
|
let assetManager = canvas.assetManager;
|
|
|
|
// Create the atlas
|
|
this.atlas = canvas.assetManager.require("mix-and-match-pma.atlas");
|
|
let atlasLoader = new spine.AtlasAttachmentLoader(this.atlas);
|
|
|
|
// Create the skeleton
|
|
let skeletonBinary = new spine.SkeletonBinary(atlasLoader);
|
|
this.skeletonData = skeletonBinary.readSkeletonData(assetManager.require("mix-and-match-pro.skel"));
|
|
this.skeleton = new spine.Skeleton(this.skeletonData);
|
|
|
|
// Create the animation state
|
|
let stateData = new spine.AnimationStateData(this.skeletonData);
|
|
this.state = new spine.AnimationState(stateData);
|
|
this.state.setAnimation(0, "dance", true);
|
|
|
|
// Create the user interface to selecting skins
|
|
this.createUI(canvas);
|
|
|
|
// Create a default skin.
|
|
this.addSkin("skin-base");
|
|
this.addSkin("nose/short");
|
|
this.addSkin("eyelids/girly");
|
|
this.addSkin("eyes/violet");
|
|
this.addSkin("hair/brown");
|
|
this.addSkin("clothes/hoodie-orange");
|
|
this.addSkin("legs/pants-jeans");
|
|
this.addSkin("accessories/bag");
|
|
this.addSkin("accessories/hat-red-yellow");
|
|
}
|
|
|
|
addSkin(skinName) {
|
|
if (this.selectedSkins.indexOf(skinName) != -1) return;
|
|
this.selectedSkins.push(skinName);
|
|
let thumbnail = this.skinThumbnails[skinName];
|
|
thumbnail.isSet = true;
|
|
thumbnail.style.filter = "none";
|
|
this.updateSkin();
|
|
}
|
|
|
|
removeSkin(skinName) {
|
|
let index = this.selectedSkins.indexOf(skinName);
|
|
if (index == -1) return;
|
|
this.selectedSkins.splice(index, 1);
|
|
let thumbnail = this.skinThumbnails[skinName];
|
|
thumbnail.isSet = false;
|
|
thumbnail.style.filter = "grayscale(1)";
|
|
this.updateSkin();
|
|
}
|
|
|
|
updateSkin() {
|
|
// Create a new skin from all the selected skins.
|
|
let newSkin = new spine.Skin("custom-skin");
|
|
for (var skinName of this.selectedSkins) {
|
|
newSkin.addSkin(this.skeletonData.findSkin(skinName));
|
|
}
|
|
this.skeleton.setSkin(newSkin);
|
|
this.skeleton.setToSetupPose();
|
|
this.skeleton.updateWorldTransform(spine.Physics.update);
|
|
|
|
// Calculate the bounds so we can center and zoom
|
|
// the camera such that the skeleton is in full view.
|
|
let offset = new spine.Vector2(), size = new spine.Vector2();
|
|
this.skeleton.getBounds(offset, size);
|
|
this.lastBounds = { offset: offset, size: size };
|
|
}
|
|
|
|
createUI(canvas) {
|
|
const THUMBNAIL_SIZE = 100;
|
|
let renderer = canvas.renderer;
|
|
let camera = renderer.camera;
|
|
|
|
// We'll reuse the webgl context used to render the skeleton for
|
|
// thumbnail generation. We temporarily resize it to 300x300 pixels
|
|
// Note: we passed `preserveDrawingBuffer: true` to the SpineCanvas
|
|
// constructor. Without it, we could not fetch the pixel data from
|
|
// the canvas after rendering.
|
|
let oldWidth = canvas.htmlCanvas.width;
|
|
let oldHeight = canvas.htmlCanvas.height;
|
|
canvas.htmlCanvas.width = canvas.htmlCanvas.height = THUMBNAIL_SIZE;
|
|
canvas.gl.viewport(0, 0, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
|
|
|
|
// For each skin, generate at thumbnail as follows
|
|
// 1. Set it on the skeleton
|
|
// 2. Determine its bounds
|
|
// 3. Center and scale it to the offscreen canvas and render it
|
|
// 4. Fetch the rendered image from the canvas and store it.
|
|
let images = [];
|
|
for (var skin of this.skeletonData.skins) {
|
|
// Skip the empty default skin
|
|
if (skin.name === "default") continue;
|
|
|
|
// Set the skin, then update the skeleton
|
|
// to the setup pose and calculate the world transforms
|
|
this.skeleton.setSkin(skin);
|
|
this.skeleton.setToSetupPose();
|
|
this.skeleton.updateWorldTransform(spine.Physics.update);
|
|
|
|
// Calculate the bounding box enclosing the skeleton.
|
|
let offset = new spine.Vector2(), size = new spine.Vector2();
|
|
this.skeleton.getBounds(offset, size);
|
|
|
|
// Position the renderer camera on the center of the bounds, and
|
|
// set the zoom so the full skin is visible within the 300x300
|
|
// rendering area. We leave 10% of empty space around a skin in the
|
|
// thumbnail, hence the multiplication of 1.1 for the zoom factor.
|
|
camera.position.x = offset.x + size.x / 2;
|
|
camera.position.y = offset.y + size.y / 2;
|
|
camera.zoom = size.x > size.y ? size.x / THUMBNAIL_SIZE * 1.1 : size.y / THUMBNAIL_SIZE * 1.1;
|
|
camera.setViewport(THUMBNAIL_SIZE, THUMBNAIL_SIZE);
|
|
camera.update();
|
|
|
|
// Clear the canvas and render the skeleton
|
|
canvas.clear(0.5, 0.5, 0.5, 1);
|
|
renderer.begin();
|
|
renderer.drawSkeleton(this.skeleton, true);
|
|
renderer.end();
|
|
|
|
// Get the image data and convert it to an img element
|
|
let image = new Image();
|
|
image.src = canvas.htmlCanvas.toDataURL();
|
|
image.skinName = skin.name;
|
|
image.isSet = false;
|
|
image.style.filter = "grayscale(1)";
|
|
|
|
// Set up a click listener that will add/remove the skin
|
|
image.onclick = () => {
|
|
if (image.isSet) this.removeSkin(image.skinName);
|
|
else this.addSkin(image.skinName);
|
|
}
|
|
|
|
// Store the thumbail image in the list of all skin
|
|
// thumbnails.
|
|
images.push(image);
|
|
this.skinThumbnails[image.skinName] = image;
|
|
}
|
|
|
|
// Sort the list of skin thumbnails by name, so items
|
|
// from the same folder end up next to each other.
|
|
images.sort((a, b) => {
|
|
return a.skinName > b.skinName ? 1 : -1;
|
|
});
|
|
|
|
// Add the thumbnails to the skins <div>
|
|
let skinsDiv = document.getElementById("skins");
|
|
for (var thumbnail of images) {
|
|
skinsDiv.appendChild(thumbnail);
|
|
}
|
|
|
|
// Restore the canvas size and camera of the renderer
|
|
canvas.htmlCanvas.width = oldWidth;
|
|
canvas.htmlCanvas.height = oldHeight;
|
|
renderer.resize(spine.ResizeMode.Expand);
|
|
camera.position.x = 0;
|
|
camera.position.y = 0;
|
|
camera.zoom = 1;
|
|
}
|
|
|
|
update(canvas, delta) {
|
|
this.state.update(delta);
|
|
this.state.apply(this.skeleton);
|
|
this.skeleton.updateWorldTransform(spine.Physics.update);
|
|
}
|
|
|
|
render(canvas) {
|
|
// Center and zoom the camera so the skeleton is
|
|
// in full view irrespective of the page size.
|
|
let renderer = canvas.renderer;
|
|
let camera = renderer.camera;
|
|
renderer.resize(spine.ResizeMode.Expand);
|
|
let offset = this.lastBounds.offset, size = this.lastBounds.size;
|
|
camera.position.x = offset.x + size.x / 2;
|
|
camera.position.y = offset.y + size.y / 2;
|
|
camera.zoom = size.x > size.y ? size.x / this.canvas.htmlCanvas.width * 1.1 : size.y / this.canvas.htmlCanvas.height * 1.1;
|
|
camera.update();
|
|
|
|
canvas.clear(0.2, 0.2, 0.2, 1);
|
|
renderer.begin();
|
|
renderer.drawSkeleton(this.skeleton, true);
|
|
renderer.end();
|
|
}
|
|
}
|
|
|
|
// Create the Spine canvas which runs the app
|
|
new spine.SpineCanvas(document.getElementById("canvas"), {
|
|
pathPrefix: "/assets/",
|
|
app: new App()
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html> |