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

262 строки
7.1 KiB

  1. let selected = null;
  2. let selectedEntity = null;
  3. let entityIndex = 0;
  4. let clicked = null;
  5. let dragging = false;
  6. let clickTimeout = null;
  7. let dragOffsetX = null;
  8. let dragOffsetY = null;
  9. let altHeld = false;
  10. const config = {
  11. height: math.unit(10, "meters")
  12. }
  13. const entities = {
  14. }
  15. function constrainRel(coords) {
  16. return {
  17. x: Math.min(Math.max(coords.x, 0), 1),
  18. y: Math.min(Math.max(coords.y, 0), 1)
  19. }
  20. }
  21. function snapRel(coords) {
  22. return constrainRel({
  23. x: coords.x,
  24. y: altHeld ? coords.y : (Math.abs(coords.y - 1) < 0.05 ? 1 : coords.y)
  25. });
  26. }
  27. function adjustAbs(coords, oldHeight, newHeight) {
  28. return {x: coords.x, y: coords.y * math.divide(oldHeight, newHeight)};
  29. }
  30. function rel2abs(coords) {
  31. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  32. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  33. return {x: coords.x * canvasWidth, y: coords.y * canvasHeight};
  34. }
  35. function abs2rel(coords) {
  36. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  37. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  38. return {x: coords.x / canvasWidth, y: coords.y / canvasHeight};
  39. }
  40. function updateEntityElement(entity, element) {
  41. const position = rel2abs({x: element.dataset.x, y: element.dataset.y});
  42. element.style.left = position.x + "px";
  43. element.style.top = position.y + "px";
  44. const canvasHeight = document.querySelector("#display").clientHeight;
  45. const pixels = math.divide(entity.height, config.height) * canvasHeight;
  46. element.style.setProperty("--height", pixels + "px");
  47. }
  48. function updateSizes() {
  49. drawScale();
  50. Object.entries(entities).forEach(([key, entity]) => {
  51. const element = document.querySelector("#entity-" + key);
  52. updateEntityElement(entity, element);
  53. });
  54. }
  55. function drawScale() {
  56. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer) {
  57. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  58. drawTick(ctx, 50, y);
  59. }
  60. }
  61. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y) {
  62. const oldStyle = ctx.strokeStyle;
  63. ctx.beginPath();
  64. ctx.moveTo(x, y);
  65. ctx.lineTo(x + 20, y);
  66. ctx.strokeStyle = "#000000";
  67. ctx.stroke();
  68. ctx.beginPath();
  69. ctx.moveTo(x + 20, y);
  70. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  71. ctx.strokeStyle = "#aaaaaa";
  72. ctx.stroke();
  73. ctx.beginPath();
  74. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  75. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  76. ctx.strokeStyle = "#000000";
  77. ctx.stroke();
  78. ctx.strokeStyle = oldStyle;
  79. }
  80. const canvas = document.querySelector("#display");
  81. /** @type {CanvasRenderingContext2D} */
  82. const ctx = canvas.getContext("2d");
  83. const pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  84. ctx.clearRect(0, 0, canvas.width, canvas.height);
  85. ctx.scale(1, 1);
  86. ctx.canvas.width = canvas.clientWidth;
  87. ctx.canvas.height = canvas.clientHeight;
  88. ctx.beginPath();
  89. ctx.moveTo(50, 50);
  90. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  91. ctx.stroke();
  92. ctx.beginPath();
  93. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  94. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  95. ctx.stroke();
  96. drawTicks(ctx, pixelsPer);
  97. }
  98. function makeEntity() {
  99. const entityTemplate = {
  100. name: "",
  101. author: "",
  102. height: math.unit(Math.random() * 2 + 1, "meters")
  103. }
  104. return entityTemplate;
  105. }
  106. function clickDown(e) {
  107. clicked = e.target;
  108. const rect = e.target.getBoundingClientRect();
  109. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  110. let entY = document.querySelector("#entities").getBoundingClientRect().y;
  111. dragOffsetX = e.clientX - rect.left + entX - rect.width / 2;
  112. dragOffsetY = e.clientY - rect.top + entY - rect.height;
  113. clickTimeout = setTimeout(() => {dragging = true}, 100)
  114. }
  115. function clickUp() {
  116. clearTimeout(clickTimeout);
  117. if (clicked) {
  118. if (dragging) {
  119. dragging = false;
  120. } else {
  121. select(clicked);
  122. }
  123. clicked = null;
  124. }
  125. }
  126. function deselect() {
  127. if (selected) {
  128. selected.classList.remove("selected");
  129. }
  130. selected = null;
  131. }
  132. function select(target) {
  133. deselect();
  134. selected = target;
  135. selectedEntity = entities[target.dataset.key];
  136. selected.classList.add("selected");
  137. entityInfo(selectedEntity);
  138. }
  139. function entityInfo(entity) {
  140. document.querySelector("#entity-name").innerText = "Name: " + entity.name;
  141. document.querySelector("#entity-author").innerText = "Author: " + entity.author;
  142. document.querySelector("#entity-height").innerText = "Height: " + entity.height.format({ precision: 3 });
  143. }
  144. function displayEntity(entity, x, y) {
  145. const location = entity.location;
  146. const img = document.createElement("img");
  147. img.src = "./pepper.png"
  148. img.classList.add("entity");
  149. img.dataset.x = x;
  150. img.dataset.y = y;
  151. img.addEventListener("mousedown", e => {clickDown(e); e.stopPropagation()});
  152. img.id = "entity-" + entityIndex;
  153. img.dataset.key = entityIndex;
  154. entities[entityIndex] = entity;
  155. entityIndex += 1;
  156. const world = document.querySelector("#entities");
  157. world.appendChild(img);
  158. updateEntityElement(entity, img);
  159. }
  160. document.addEventListener("DOMContentLoaded", () => {
  161. for (let x = 0; x < 5; x++) {
  162. const entity = makeEntity();
  163. entity.name = "Green is my pepper";
  164. entity.author = "Fen"
  165. const x = 0.25 + Math.random() * 0.5;
  166. const y = 0.25 + Math.random() * 0.5;
  167. displayEntity(entity, x, y);
  168. }
  169. updateSizes();
  170. document.querySelector("#options-height-value").addEventListener("input", e => {
  171. updateWorldHeight();
  172. })
  173. document.querySelector("#options-height-unit").addEventListener("input", e => {
  174. updateWorldHeight();
  175. })
  176. world.addEventListener("mousedown", e => deselect());
  177. document.addEventListener("mouseup", e => clickUp());
  178. });
  179. window.addEventListener("resize", () => {
  180. updateSizes();
  181. })
  182. document.addEventListener("mousemove", (e) => {
  183. if (clicked) {
  184. const position = snapRel(abs2rel({x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY}));
  185. clicked.dataset.x = position.x;
  186. clicked.dataset.y = position.y;
  187. updateEntityElement(entities[clicked.dataset.key], clicked);
  188. }
  189. });
  190. function updateWorldHeight() {
  191. const value = document.querySelector("#options-height-value").value;
  192. const unit = document.querySelector("#options-height-unit").value;
  193. const oldHeight = config.height;
  194. config.height = math.unit(value + " " + unit)
  195. Object.entries(entities).forEach(([key, entity]) => {
  196. const element = document.querySelector("#entity-" + key);
  197. const newPosition = adjustAbs({x: element.dataset.x, y: element.dataset.y}, oldHeight, config.height);
  198. element.dataset.x = newPosition.x;
  199. element.dataset.y = newPosition.y;
  200. });
  201. updateSizes();
  202. }