slot-objects.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Pre-load the skeleton data and atlas. You can also load .json skeleton data.
  24. PIXI.Assets.add("spineboyData", "./assets/spineboy-pro.skel");
  25. PIXI.Assets.add("spineboyAtlas", "./assets/spineboy-pma.atlas");
  26. await PIXI.Assets.load(["spineboyData", "spineboyAtlas"]);
  27. // Create the spine display object
  28. const spineboy = spine.Spine.from({skeleton: "spineboyData", atlas: "spineboyAtlas",
  29. scale: 0.25,
  30. });
  31. // Set the default mix time to use when transitioning
  32. // from one animation to the next.
  33. spineboy.state.data.defaultMix = 0.2;
  34. // Center the spine object on screen.
  35. spineboy.x = window.innerWidth / 2;
  36. spineboy.y = window.innerHeight / 2 + spineboy.getBounds().height / 2;
  37. // Set animation "run" on track 0, looped.
  38. spineboy.state.setAnimation(0, "walk", true);
  39. // Add the display object to the stage.
  40. app.stage.addChild(spineboy);
  41. const tooth1 = PIXI.Sprite.from('assets/raptor-jaw-tooth.png');
  42. const tooth2 = PIXI.Sprite.from('assets/raptor-jaw-tooth.png');
  43. const text = new PIXI.Text('Text GUN');
  44. const toothContainer = new PIXI.Container();
  45. toothContainer.addChild(tooth1);
  46. toothContainer.name = "tooth";
  47. text.name = "text";
  48. // putting logo2 on top of the hand using slot directly and remove the attachment hand
  49. let frontFist;
  50. setTimeout(() => {
  51. frontFist = spineboy.skeleton.findSlot("front-foot");
  52. tooth1.x = -10;
  53. tooth1.y = -70;
  54. spineboy.addSlotObject(frontFist, toothContainer);
  55. frontFist.setAttachment(null);
  56. }, 1000);
  57. // scaling the bone, will scale the pixi object too
  58. setTimeout(() => {
  59. frontFist.bone.scaleX = .5;
  60. frontFist.bone.scaleY = .5;
  61. }, 2000);
  62. // adding a pixi text in a slot using slot index
  63. let mouth;
  64. setTimeout(() => {
  65. spineboy.addSlotObject("gun", text);
  66. }, 4000);
  67. // adding one pixi object to an already "occupied" slot will remove the old one,
  68. // and move the given one to the slot
  69. setTimeout(() => {
  70. spineboy.addSlotObject("gun", toothContainer);
  71. }, 5000);
  72. // adding multiple DisplayObjects to a slot using a Container to control their offset, size, ...
  73. setTimeout(() => {
  74. toothContainer.addChild(tooth2);
  75. tooth2.x = 30;
  76. tooth2.y = -70;
  77. tooth2.angle = 30;
  78. tooth2.tint = 0xFF5500;
  79. }, 6000);
  80. // removing the container won't automatically destroy the displayObject contained, so take care of them
  81. setTimeout(() => {
  82. spineboy.removeSlotObject("gun");
  83. console.log(toothContainer.destroyed)
  84. console.log(tooth1.destroyed)
  85. console.log(tooth2.destroyed)
  86. toothContainer.destroy();
  87. tooth1.destroy();
  88. console.log(toothContainer.destroyed)
  89. console.log(tooth1.destroyed)
  90. console.log(tooth2.destroyed)
  91. }, 7000);
  92. // removing a specific slot object, that is not in that slot do nothing
  93. setTimeout(() => {
  94. spineboy.addSlotObject("gun", tooth2);
  95. spineboy.removeSlotObject("gun", text);
  96. }, 8000);
  97. // removing a specific slot object
  98. setTimeout(() => {
  99. spineboy.removeSlotObject("gun", tooth2);
  100. tooth2.destroy();
  101. }, 9000);
  102. // resetting the slot with the original attachment
  103. setTimeout(() => {
  104. frontFist.setToSetupPose();
  105. frontFist.bone.setToSetupPose();
  106. }, 10000);
  107. // showing an animation with clipping -> Pixi masks will be created
  108. // for clipping attachments having slot objects
  109. setTimeout(() => {
  110. spineboy.state.setAnimation(0, "portal", true)
  111. const tooth3 = PIXI.Sprite.from('assets/raptor-jaw-tooth.png');
  112. tooth3.scale.set(2);
  113. tooth3.x = -60;
  114. tooth3.y = 120;
  115. tooth3.angle = 180;
  116. const foot1 = new PIXI.Container();
  117. foot1.addChild(tooth3);
  118. spineboy.addSlotObject("rear-foot", foot1);
  119. }, 11000);
  120. })();
  121. </script>
  122. </body>
  123. </html>