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

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