less copy protection, more size visualization
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

155 行
4.0 KiB

  1. let selected = null;
  2. let selectedEntity = null;
  3. let entityIndex = 0;
  4. const config = {
  5. height: math.unit(10, "meters")
  6. }
  7. const entities = {
  8. }
  9. function updateSizes() {
  10. drawScale();
  11. Object.entries(entities).forEach(([key, entity]) => {
  12. const element = document.querySelector("#entity-" + key);
  13. const canvasHeight = document.querySelector("#display").clientHeight;
  14. const pixels = math.divide(entity.height, config.height).value * canvasHeight;
  15. element.style.setProperty("--height", pixels + "px");
  16. });
  17. }
  18. function drawScale() {
  19. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer) {
  20. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  21. drawTick(ctx, 50, y);
  22. }
  23. }
  24. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y) {
  25. const oldStyle = ctx.strokeStyle;
  26. console.log(ctx.strokeStyle);
  27. ctx.beginPath();
  28. ctx.moveTo(x, y);
  29. ctx.lineTo(x+20, y);
  30. ctx.strokeStyle = "#000000";
  31. ctx.stroke();
  32. ctx.beginPath();
  33. ctx.moveTo(x+20, y);
  34. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  35. ctx.strokeStyle = "#aaaaaa";
  36. ctx.stroke();
  37. ctx.beginPath();
  38. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  39. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  40. ctx.strokeStyle = "#000000";
  41. ctx.stroke();
  42. ctx.strokeStyle = oldStyle;
  43. console.log(ctx.strokeStyle);
  44. }
  45. const canvas = document.querySelector("#display");
  46. /** @type {CanvasRenderingContext2D} */
  47. const ctx = canvas.getContext("2d");
  48. const pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  49. ctx.clearRect(0, 0, canvas.width, canvas.height);
  50. ctx.scale(1,1);
  51. console.log(canvas.clientWidth);
  52. ctx.canvas.width = canvas.clientWidth;
  53. ctx.canvas.height = canvas.clientHeight;
  54. ctx.beginPath();
  55. ctx.moveTo(50, 50);
  56. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  57. ctx.stroke();
  58. ctx.beginPath();
  59. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  60. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  61. ctx.stroke();
  62. drawTicks(ctx, pixelsPer);
  63. }
  64. function makeEntity() {
  65. const entityTemplate = {
  66. name: "",
  67. author: "",
  68. location: {
  69. x: 0,
  70. y: 0
  71. },
  72. height: math.unit(Math.random() * 2 + 1, "meters")
  73. }
  74. return entityTemplate;
  75. }
  76. function select(target) {
  77. if (selected) {
  78. selected.classList.remove("selected");
  79. }
  80. selected = target;
  81. selectedEntity = entities[target.dataset.key];
  82. console.log(selectedEntity)
  83. selected.classList.add("selected");
  84. entityInfo(selectedEntity);
  85. }
  86. function entityInfo(entity) {
  87. document.querySelector("#entity-name").innerText = "Name: " + entity.name;
  88. document.querySelector("#entity-author").innerText = "Author: " + entity.author;
  89. document.querySelector("#entity-height").innerText = "Height: " + entity.height.format({precision: 3});
  90. }
  91. function displayEntity(entity) {
  92. const location = entity.location;
  93. const img = document.createElement("img");
  94. img.src = "./pepper.png"
  95. img.classList.add("entity");
  96. img.style.left = location.x + "px";
  97. img.style.top = location.y + "px";
  98. img.addEventListener("click", e => select(e.target));
  99. img.id = "entity-" + entityIndex;
  100. img.dataset.key = entityIndex;
  101. entities[entityIndex] = entity;
  102. entityIndex += 1;
  103. const world = document.querySelector("#entities");
  104. world.appendChild(img);
  105. }
  106. document.addEventListener("DOMContentLoaded", () => {
  107. for (let x = 0; x < 5; x++) {
  108. const entity = makeEntity();
  109. entity.name = "Green is my pepper";
  110. entity.author = "Fen"
  111. entity.location.x = 300 + Math.random() * 600;
  112. entity.location.y = 300 + Math.random() * 400;
  113. displayEntity(entity);
  114. }
  115. updateSizes();
  116. });
  117. window.addEventListener("resize", () => {
  118. updateSizes();
  119. })