spritesheet.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <style type="text/css">
  2. html,
  3. body {
  4. margin: 0;
  5. padding: 0;
  6. margin-top: 18px;
  7. }
  8. #sprite-name {
  9. position: fixed;
  10. top: 0;
  11. left: 0;
  12. font-family: Arial, Helvetica, sans-serif;
  13. font-size: 30px;
  14. text-align: center;
  15. width: 100%;
  16. background-color: rgba(255, 255, 255, 0.3);
  17. }
  18. </style>
  19. <canvas></canvas>
  20. <span id="sprite-name"></span>
  21. <script src="./js/SpriteAtlas.js"></script>
  22. <script>
  23. function parseArgs() {
  24. let queryParams = location.search.split("?")[1];
  25. let urlArgs = {};
  26. queryParams.split("&").forEach((keyValue) => {
  27. let dataSplit = keyValue.split("=");
  28. urlArgs[dataSplit[0]] = decodeURI(dataSplit[1]);
  29. });
  30. return urlArgs;
  31. }
  32. let canvas;
  33. let context;
  34. let hoveredSprite;
  35. let selectedSprite;
  36. document.addEventListener("DOMContentLoaded", () => {
  37. canvas = document.getElementsByTagName("canvas")[0];
  38. canvas.addEventListener("mousemove", (event) => {
  39. hoveredSprite = Object.values(spriteAtlas.atlas).find((sprite) => {
  40. return event.offsetX > sprite.x && event.offsetX < sprite.x + sprite.width &&
  41. event.offsetY > sprite.y && event.offsetY < sprite.y + sprite.height;
  42. });
  43. });
  44. canvas.addEventListener("mouseup", (event) => {
  45. let spriteToSelect = Object.values(spriteAtlas.atlas).find((sprite) => {
  46. return event.offsetX > sprite.x && event.offsetX < sprite.x + sprite.width &&
  47. event.offsetY > sprite.y && event.offsetY < sprite.y + sprite.height;
  48. });
  49. if(!selectedSprite || spriteToSelect.name != selectedSprite.name) {
  50. selectedSprite = spriteToSelect;
  51. localStorage['selectedSprite'] = JSON.stringify(selectedSprite);
  52. } else {
  53. selectedSprite = null;
  54. delete localStorage['selectedSprite'];
  55. }
  56. });
  57. let args = parseArgs();
  58. spriteAtlas = new SpriteAtlas(args.path, args.file);
  59. spriteAtlas.load().then(() => {
  60. animate();
  61. });
  62. });
  63. function animate() {
  64. canvas.width = spriteAtlas.spritesheetImage.width;
  65. canvas.height = spriteAtlas.spritesheetImage.height;
  66. context = canvas.getContext("2d");
  67. canvas.width = canvas.width;
  68. context.font = "30px Arial";
  69. context.fillStyle = "#000000";
  70. context.textAlign = "center";
  71. context.drawImage(spriteAtlas.spritesheetImage, 0, 0);
  72. context.strokeStyle = "#22FF22";
  73. context.lineWidth = 4;
  74. if (!!hoveredSprite) {
  75. context.beginPath();
  76. context.rect(hoveredSprite.x, hoveredSprite.y, hoveredSprite.width, hoveredSprite.height);
  77. context.stroke();
  78. document.getElementById("sprite-name").innerHTML = hoveredSprite.name;
  79. context.font = "10px Arial";
  80. context.textAlign = "right";
  81. context.fillText(`x:${hoveredSprite.x}, y:${hoveredSprite.y}, w:${hoveredSprite.width}, h:${hoveredSprite.height}`, hoveredSprite.x + hoveredSprite.width, hoveredSprite.y + hoveredSprite.height);
  82. }
  83. if (!!selectedSprite) {
  84. context.strokeStyle = "#2222FF";
  85. context.beginPath();
  86. context.rect(selectedSprite.x, selectedSprite.y, selectedSprite.width, selectedSprite.height);
  87. context.stroke();
  88. }
  89. requestAnimationFrame(animate);
  90. }
  91. </script>