|
@@ -17,6 +17,9 @@ var assetManager;
|
|
|
var skeleton, state, bounds;
|
|
|
var skeletonRenderer;
|
|
|
|
|
|
+var skelName = "spineboy";
|
|
|
+var animName = "idle";
|
|
|
+
|
|
|
function init () {
|
|
|
canvas = document.getElementById("canvas");
|
|
|
canvas.width = window.innerWidth;
|
|
@@ -29,21 +32,21 @@ function init () {
|
|
|
// enable the triangle renderer, supports meshes, but may produce artifacts in some browsers
|
|
|
skeletonRenderer.triangleRendering = false;
|
|
|
|
|
|
- assetManager = new spine.canvas.AssetManager();
|
|
|
+ assetManager = new spine.canvas.AssetManager();
|
|
|
|
|
|
- assetManager.loadText("assets/spineboy.json");
|
|
|
- assetManager.loadText("assets/spineboy.atlas");
|
|
|
- assetManager.loadTexture("assets/spineboy.png");
|
|
|
+ assetManager.loadText("assets/" + skelName + ".json");
|
|
|
+ assetManager.loadText("assets/" + skelName + ".atlas");
|
|
|
+ assetManager.loadTexture("assets/" + skelName + ".png");
|
|
|
|
|
|
requestAnimationFrame(load);
|
|
|
}
|
|
|
|
|
|
function load () {
|
|
|
if (assetManager.isLoadingComplete()) {
|
|
|
- var data = loadSkeleton("spineboy", "walk", "default");
|
|
|
+ var data = loadSkeleton(skelName, animName, "default");
|
|
|
skeleton = data.skeleton;
|
|
|
state = data.state;
|
|
|
- bounds = data.bounds;
|
|
|
+ bounds = data.bounds;
|
|
|
requestAnimationFrame(render);
|
|
|
} else {
|
|
|
requestAnimationFrame(load);
|
|
@@ -56,7 +59,7 @@ function loadSkeleton (name, initialAnimation, skin) {
|
|
|
// Load the texture atlas using name.atlas and name.png from the AssetManager.
|
|
|
// The function passed to TextureAtlas is used to resolve relative paths.
|
|
|
atlas = new spine.TextureAtlas(assetManager.get("assets/" + name + ".atlas"), function(path) {
|
|
|
- return assetManager.get("assets/" + path);
|
|
|
+ return assetManager.get("assets/" + path);
|
|
|
});
|
|
|
|
|
|
// Create a AtlasAttachmentLoader, which is specific to the WebGL backend.
|
|
@@ -64,12 +67,12 @@ function loadSkeleton (name, initialAnimation, skin) {
|
|
|
|
|
|
// Create a SkeletonJson instance for parsing the .json file.
|
|
|
var skeletonJson = new spine.SkeletonJson(atlasLoader);
|
|
|
-
|
|
|
- // Set the scale to apply during parsing, parse the file, and create a new skeleton.
|
|
|
+
|
|
|
+ // Set the scale to apply during parsing, parse the file, and create a new skeleton.
|
|
|
var skeletonData = skeletonJson.readSkeletonData(assetManager.get("assets/" + name + ".json"));
|
|
|
var skeleton = new spine.Skeleton(skeletonData);
|
|
|
- skeleton.flipY = true;
|
|
|
- var bounds = calculateBounds(skeleton);
|
|
|
+ skeleton.flipY = true;
|
|
|
+ var bounds = calculateBounds(skeleton);
|
|
|
skeleton.setSkinByName(skin);
|
|
|
|
|
|
// Create an AnimationState, and set the initial animation in looping mode.
|
|
@@ -100,7 +103,7 @@ function calculateBounds(skeleton) {
|
|
|
skeleton.updateWorldTransform();
|
|
|
var offset = new spine.Vector2();
|
|
|
var size = new spine.Vector2();
|
|
|
- skeleton.getBounds(offset, size);
|
|
|
+ skeleton.getBounds(offset, size);
|
|
|
return { offset: offset, size: size };
|
|
|
}
|
|
|
|
|
@@ -108,7 +111,7 @@ function render () {
|
|
|
var now = Date.now() / 1000;
|
|
|
var delta = now - lastFrameTime;
|
|
|
lastFrameTime = now;
|
|
|
-
|
|
|
+
|
|
|
resize();
|
|
|
|
|
|
context.save();
|
|
@@ -119,15 +122,23 @@ function render () {
|
|
|
|
|
|
state.update(delta);
|
|
|
state.apply(skeleton);
|
|
|
- skeleton.updateWorldTransform();
|
|
|
- skeletonRenderer.draw(skeleton);
|
|
|
+ skeleton.updateWorldTransform();
|
|
|
+ skeletonRenderer.draw(skeleton);
|
|
|
+
|
|
|
+ context.strokeStyle = "green";
|
|
|
+ context.beginPath();
|
|
|
+ context.moveTo(-1000, 0);
|
|
|
+ context.lineTo(1000, 0);
|
|
|
+ context.moveTo(0, -1000);
|
|
|
+ context.lineTo(0, 1000);
|
|
|
+ context.stroke();
|
|
|
|
|
|
requestAnimationFrame(render);
|
|
|
}
|
|
|
|
|
|
function resize () {
|
|
|
var w = canvas.clientWidth;
|
|
|
- var h = canvas.clientHeight;
|
|
|
+ var h = canvas.clientHeight;
|
|
|
if (canvas.width != w || canvas.height != h) {
|
|
|
canvas.width = w;
|
|
|
canvas.height = h;
|
|
@@ -140,12 +151,12 @@ function resize () {
|
|
|
var scaleY = bounds.size.y / canvas.height;
|
|
|
var scale = Math.max(scaleX, scaleY) * 1.2;
|
|
|
if (scale < 1) scale = 1;
|
|
|
- var width = canvas.width * scale;
|
|
|
- var height = canvas.height * scale;
|
|
|
-
|
|
|
+ var width = canvas.width;
|
|
|
+ var height = canvas.height;
|
|
|
+
|
|
|
context.resetTransform();
|
|
|
context.scale(1 / scale, 1 / scale);
|
|
|
- context.translate(-centerX, -centerY);
|
|
|
+ context.translate(-centerX, -centerY);
|
|
|
context.translate(width / 2, height / 2);
|
|
|
}
|
|
|
|