You need to set the viewport once in the beginning. Start by getting the example to run:
https://github.com/EsotericSoftware/spine-runtimes/tree/4.1/spine-ts/spine-player/example
Eg, install Python 3 if you don't have it, change to the spine-ts/spine-player
folder, then:
python -m http.server
Then open a browser to:
http://localhost:8000/example/example.html
Look at the callbacks available:
https://github.com/EsotericSoftware/spine-runtimes/blob/4.1/spine-ts/spine-player/src/Player.ts#L145-L162
Use the success
callback so your code runs after the skeleton is loaded. See docs here:
https://esotericsoftware.com/spine-player#Advanced-playback
Figure out how to get an attachment by name:
http://esotericsoftware.com/spine-api-reference#Skeleton-getAttachment2
The API is generic and applies to all languages. Check the spine-ts code to be sure of the method you want to call:
https://github.com/EsotericSoftware/spine-runtimes/blob/4.1/spine-ts/spine-core/src/Skeleton.ts#L515
Now you can modify the example:
new spine.SpinePlayer("container", {
skelUrl: "assets/spineboy-pro.skel",
atlasUrl: "assets/spineboy-pma.atlas",
premultipliedAlpha: true,
backgroundColor: "#cccccc",
viewport: {
debugRender: true,
},
showControls: false,
success: function (player) {
var vertices = player.skeleton.getAttachmentByName("head-bb", "head").vertices;
var minX = 99999, minY = 99999, maxX = -99999, maxY = -99999;
for (var i = 0, n = vertices.length; i < n; i += 2) {
var x = vertices[i], y = vertices[i + 1];
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
player.config.viewport.x = minX;
player.config.viewport.y = minY;
player.config.viewport.width = maxX - minX;
player.config.viewport.height = maxX - minX;
// Needed to show skeleton when no animation is specified (bug fixed in 4.2):
player.skeleton.updateWorldTransform();
}
});
For that to work you'll have to move the spineboy head-bb
slot to the root (here spineboy-pro.skel), then you get: