less copy protection, more size visualization
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

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