control-bones-example.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <html>
  2. <head>
  3. <meta charset="UTF-8" />
  4. <title>spine-pixi</title>
  5. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pixi.min.js"></script>
  6. <script src="../dist/iife/spine-pixi-v7.js"></script>
  7. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/tweakpane.min.js"></script>
  8. <link rel="stylesheet" href="../../index.css">
  9. </head>
  10. <body>
  11. <script>
  12. (async function () {
  13. var app = new PIXI.Application({
  14. width: window.innerWidth,
  15. height: window.innerHeight,
  16. resolution: window.devicePixelRatio || 1,
  17. autoDensity: true,
  18. resizeTo: window,
  19. backgroundColor: 0x2c3e50,
  20. hello: true,
  21. });
  22. document.body.appendChild(app.view);
  23. app.stage.eventMode = 'static';
  24. app.stage.hitArea = app.screen;
  25. let dragObject = null;
  26. let lastX = -1, lastY = -1;
  27. const endDrag = () => (dragObject = null);
  28. app.stage
  29. .on('pointerup', endDrag)
  30. .on('pointerupoutside', endDrag)
  31. .on('pointermove', ({ x, y }) => {
  32. if (dragObject) {
  33. dragObject.x += x - lastX;
  34. dragObject.y += y - lastY;
  35. lastX = x;
  36. lastY = y;
  37. }
  38. });
  39. // Pre-load the skeleton data and atlas. You can also load .json skeleton data.
  40. PIXI.Assets.add("stretchymanData", "./assets/stretchyman-pro.skel");
  41. PIXI.Assets.add("stretchymanAtlas", "./assets/stretchyman-pma.atlas");
  42. await PIXI.Assets.load(["stretchymanData", "stretchymanAtlas"]);
  43. // Create the spine display object
  44. const stretchyman = spine.Spine.from({skeleton: "stretchymanData", atlas: "stretchymanAtlas",
  45. scale: 0.75,
  46. });
  47. // Set the default mix time to use when transitioning
  48. // from one animation to the next.
  49. stretchyman.state.data.defaultMix = 0.2;
  50. // Center the spine object on screen.
  51. stretchyman.x = window.innerWidth / 2;
  52. stretchyman.y = window.innerHeight / 2 + stretchyman.getBounds().height / 2;
  53. // Set animation "run" on track 0, looped.
  54. stretchyman.state.setAnimation(0, "idle", true);
  55. app.stage.addChild(stretchyman);
  56. stretchyman.updateTransform();
  57. const controlBoneNames = [
  58. "back-arm-ik-target",
  59. "back-leg-ik-target",
  60. "front-arm-ik-target",
  61. "front-leg-ik-target",
  62. ];
  63. const controlBones = [];
  64. for (var i = 0; i < controlBoneNames.length; i++) {
  65. const bone = stretchyman.skeleton.findBone(controlBoneNames[i]);
  66. const point = { x: bone.worldX, y: bone.worldY };
  67. stretchyman.skeletonToPixiWorldCoordinates(point);
  68. const control = new PIXI.Graphics()
  69. .beginFill('ff00ff')
  70. .drawCircle(0, 0, 6);
  71. control.x = point.x;
  72. control.y = point.y;
  73. controlBones.push({ bone, control });
  74. app.stage.addChild(control);
  75. control.interactive = "static";
  76. control.on('pointerdown', ({ x, y }) => {
  77. dragObject = control;
  78. lastX = x;
  79. lastY = y;
  80. })
  81. }
  82. const point = { x: 0, y: 0 };
  83. stretchyman.beforeUpdateWorldTransforms = () => {
  84. for (let { bone, control } of controlBones) {
  85. point.x = control.x;
  86. point.y = control.y;
  87. stretchyman.pixiWorldCoordinatesToBone(point, bone);
  88. bone.x = point.x;
  89. bone.y = point.y;
  90. }
  91. };
  92. })();
  93. </script>
  94. </body>
  95. </html>