index.html 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!--<script src="https://unpkg.com/@esotericsoftware/[email protected].*/dist/iife/spine-canvas.js"></script>-->
  5. <script src="../dist/iife/spine-canvas.js"></script>
  6. </head>
  7. <body style="margin: 0; padding: 0; background: #333">
  8. <canvas id="canvas" style="width: 100%; height: 100vh;"></canvas>
  9. </body>
  10. <script>
  11. let lastFrameTime = Date.now() / 1000;
  12. let canvas, context;
  13. let assetManager;
  14. let skeleton, animationState, bounds;
  15. let skeletonRenderer;
  16. async function load() {
  17. canvas = document.getElementById("canvas");
  18. context = canvas.getContext("2d");
  19. skeletonRenderer = new spine.SkeletonRenderer(context);
  20. // Load the assets.
  21. assetManager = new spine.AssetManager("https://esotericsoftware.com/files/examples/4.0/spineboy/export/");
  22. assetManager.loadText("spineboy-ess.json");
  23. assetManager.loadTextureAtlas("spineboy.atlas");
  24. await assetManager.loadAll();
  25. // Create the texture atlas and skeleton data.
  26. let atlas = assetManager.require("spineboy.atlas");
  27. let atlasLoader = new spine.AtlasAttachmentLoader(atlas);
  28. let skeletonJson = new spine.SkeletonJson(atlasLoader);
  29. let skeletonData = skeletonJson.readSkeletonData(assetManager.require("spineboy-ess.json"));
  30. // Instantiate a new skeleton based on the atlas and skeleton data.
  31. skeleton = new spine.Skeleton(skeletonData);
  32. skeleton.setToSetupPose();
  33. skeleton.updateWorldTransform();
  34. bounds = skeleton.getBoundsRect();
  35. // Setup an animation state with a default mix of 0.2 seconds.
  36. var animationStateData = new spine.AnimationStateData(skeleton.data);
  37. animationStateData.defaultMix = 0.2;
  38. animationState = new spine.AnimationState(animationStateData);
  39. // Set the run animation, looping.
  40. animationState.setAnimation(0, "run", true);
  41. // Start rendering.
  42. requestAnimationFrame(render);
  43. }
  44. function render() {
  45. // Calculate the delta time between this and the last frame in seconds.
  46. var now = Date.now() / 1000;
  47. var delta = now - lastFrameTime;
  48. lastFrameTime = now;
  49. // Resize the canvas drawing buffer if the canvas CSS width and height changed
  50. // and clear the canvas.
  51. if (canvas.width != canvas.clientWidth || canvas.height != canvas.clientHeight) {
  52. canvas.width = canvas.clientWidth;
  53. canvas.height = canvas.clientHeight;
  54. }
  55. context.clearRect(0, 0, canvas.width, canvas.height);
  56. // Center the skeleton and resize it so it fits inside the canvas.
  57. skeleton.x = canvas.width / 2;
  58. skeleton.y = canvas.height - canvas.height * 0.1;
  59. let scale = canvas.height / bounds.height * 0.8;
  60. skeleton.scaleX = scale;
  61. skeleton.scaleY = -scale;
  62. // Update and apply the animation state, update the skeleton's
  63. // world transforms and render the skeleton.
  64. animationState.update(delta);
  65. animationState.apply(skeleton);
  66. skeleton.updateWorldTransform();
  67. skeletonRenderer.draw(skeleton);
  68. requestAnimationFrame(render);
  69. }
  70. load();
  71. </script>
  72. </html>