less copy protection, more size visualization
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

812 lines
24 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 unitChoices = {
  11. length: [
  12. "meters",
  13. "kilometers",
  14. "feet",
  15. "miles",
  16. ],
  17. area: [
  18. "cm^2",
  19. "meters^2"
  20. ],
  21. mass: [
  22. "kilograms"
  23. ]
  24. }
  25. const config = {
  26. height: math.unit(1500, "meters"),
  27. minLineSize: 50,
  28. maxLineSize: 250,
  29. autoFit: false
  30. }
  31. const availableEntities = {
  32. }
  33. const entities = {
  34. }
  35. function constrainRel(coords) {
  36. return {
  37. x: Math.min(Math.max(coords.x, 0), 1),
  38. y: Math.min(Math.max(coords.y, 0), 1)
  39. }
  40. }
  41. function snapRel(coords) {
  42. return constrainRel({
  43. x: coords.x,
  44. y: altHeld ? coords.y : (Math.abs(coords.y - 1) < 0.05 ? 1 : coords.y)
  45. });
  46. }
  47. function adjustAbs(coords, oldHeight, newHeight) {
  48. return { x: coords.x, y: 1 + (coords.y - 1) * math.divide(oldHeight, newHeight) };
  49. }
  50. function rel2abs(coords) {
  51. const canvasWidth = document.querySelector("#display").clientWidth - 100;
  52. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  53. return { x: coords.x * canvasWidth + 50, y: coords.y * canvasHeight };
  54. }
  55. function abs2rel(coords) {
  56. const canvasWidth = document.querySelector("#display").clientWidth - 100;
  57. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  58. return { x: (coords.x - 50) / canvasWidth, y: coords.y / canvasHeight };
  59. }
  60. function updateEntityElement(entity, element) {
  61. const position = rel2abs({ x: element.dataset.x, y: element.dataset.y });
  62. const view = element.dataset.view;
  63. element.style.left = position.x + "px";
  64. element.style.top = position.y + "px";
  65. const canvasHeight = document.querySelector("#display").clientHeight;
  66. const pixels = math.divide(entity.views[view].height, config.height) * (canvasHeight - 100);
  67. element.style.setProperty("--height", pixels + "px");
  68. element.querySelector(".entity-name").innerText = entity.name;
  69. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  70. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  71. bottomName.style.left = position.x + entX + "px";
  72. bottomName.style.top = "95vh";
  73. bottomName.innerText = entity.name;
  74. }
  75. function updateSizes() {
  76. drawScale();
  77. Object.entries(entities).forEach(([key, entity]) => {
  78. const element = document.querySelector("#entity-" + key);
  79. updateEntityElement(entity, element);
  80. });
  81. }
  82. function drawScale() {
  83. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer, heightPer) {
  84. let total = heightPer.clone();
  85. total.value = 0;
  86. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  87. drawTick(ctx, 50, y, total);
  88. total = math.add(total, heightPer);
  89. }
  90. }
  91. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y, value) {
  92. const oldStroke = ctx.strokeStyle;
  93. const oldFill = ctx.fillStyle;
  94. ctx.beginPath();
  95. ctx.moveTo(x, y);
  96. ctx.lineTo(x + 20, y);
  97. ctx.strokeStyle = "#000000";
  98. ctx.stroke();
  99. ctx.beginPath();
  100. ctx.moveTo(x + 20, y);
  101. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  102. ctx.strokeStyle = "#aaaaaa";
  103. ctx.stroke();
  104. ctx.beginPath();
  105. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  106. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  107. ctx.strokeStyle = "#000000";
  108. ctx.stroke();
  109. const oldFont = ctx.font;
  110. ctx.font = 'normal 24pt coda';
  111. ctx.fillStyle = "#dddddd";
  112. ctx.beginPath();
  113. ctx.fillText(value.format({ precision: 3 }), x + 20, y + 35);
  114. ctx.font = oldFont;
  115. ctx.strokeStyle = oldStroke;
  116. ctx.fillStyle = oldFill;
  117. }
  118. const canvas = document.querySelector("#display");
  119. /** @type {CanvasRenderingContext2D} */
  120. const ctx = canvas.getContext("2d");
  121. let pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  122. let heightPer = config.height.clone();
  123. heightPer.value = 1;
  124. if (pixelsPer < config.minLineSize) {
  125. heightPer.value /= pixelsPer / config.minLineSize;
  126. pixelsPer = config.minLineSize;
  127. }
  128. if (pixelsPer > config.maxLineSize) {
  129. heightPer.value /= pixelsPer / config.maxLineSize;
  130. pixelsPer = config.maxLineSize;
  131. }
  132. ctx.clearRect(0, 0, canvas.width, canvas.height);
  133. ctx.scale(1, 1);
  134. ctx.canvas.width = canvas.clientWidth;
  135. ctx.canvas.height = canvas.clientHeight;
  136. ctx.beginPath();
  137. ctx.moveTo(50, 50);
  138. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  139. ctx.stroke();
  140. ctx.beginPath();
  141. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  142. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  143. ctx.stroke();
  144. drawTicks(ctx, pixelsPer, heightPer);
  145. }
  146. function makeEntity(name, author, views) {
  147. const entityTemplate = {
  148. name: name,
  149. author: author,
  150. scale: 1,
  151. views: views,
  152. init: function () {
  153. Object.entries(this.views).forEach(([viewKey, view]) => {
  154. view.parent = this;
  155. if (this.defaultView === undefined) {
  156. this.defaultView = viewKey;
  157. }
  158. Object.entries(view.attributes).forEach(([key, val]) => {
  159. Object.defineProperty(
  160. view,
  161. key,
  162. {
  163. get: function () {
  164. return math.multiply(Math.pow(this.parent.scale, this.attributes[key].power), this.attributes[key].base);
  165. },
  166. set: function (value) {
  167. const newScale = Math.pow(math.divide(value, this.attributes[key].base), 1 / this.attributes[key].power);
  168. this.parent.scale = newScale;
  169. }
  170. }
  171. )
  172. });
  173. });
  174. delete this.init;
  175. return this;
  176. }
  177. }.init();
  178. return entityTemplate;
  179. }
  180. function clickDown(target, x, y) {
  181. clicked = target;
  182. const rect = target.getBoundingClientRect();
  183. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  184. let entY = document.querySelector("#entities").getBoundingClientRect().y;
  185. dragOffsetX = x - rect.left + entX;
  186. dragOffsetY = y - rect.top + entY;
  187. clickTimeout = setTimeout(() => { dragging = true }, 200)
  188. }
  189. // could we make this actually detect the menu area?
  190. function hoveringInDeleteArea(e) {
  191. return e.clientY < document.body.clientHeight / 10;
  192. }
  193. function clickUp(e) {
  194. clearTimeout(clickTimeout);
  195. if (clicked) {
  196. if (dragging) {
  197. dragging = false;
  198. if (hoveringInDeleteArea(e)) {
  199. removeEntity(clicked);
  200. document.querySelector("#menubar").classList.remove("hover-delete");
  201. }
  202. } else {
  203. select(clicked);
  204. }
  205. clicked = null;
  206. }
  207. }
  208. function deselect() {
  209. if (selected) {
  210. selected.classList.remove("selected");
  211. }
  212. selected = null;
  213. clearViewList();
  214. clearEntityOptions();
  215. clearViewOptions();
  216. }
  217. function select(target) {
  218. deselect();
  219. selected = target;
  220. selectedEntity = entities[target.dataset.key];
  221. selected.classList.add("selected");
  222. configViewList(selectedEntity, target.dataset.view);
  223. configEntityOptions(selectedEntity, target.dataset.view);
  224. configViewOptions(selectedEntity, target.dataset.view);
  225. }
  226. function configViewList(entity, selectedView) {
  227. const list = document.querySelector("#entity-view");
  228. list.innerHTML = "";
  229. list.style.display = "block";
  230. Object.keys(entity.views).forEach(view => {
  231. const option = document.createElement("option");
  232. option.innerText = entity.views[view].name;
  233. option.value = view;
  234. if (view === selectedView) {
  235. option.selected = true;
  236. }
  237. list.appendChild(option);
  238. });
  239. }
  240. function clearViewList() {
  241. const list = document.querySelector("#entity-view");
  242. list.innerHTML = "";
  243. list.style.display = "none";
  244. }
  245. function updateWorldOptions(entity, view) {
  246. const heightInput = document.querySelector("#options-height-value");
  247. const heightSelect = document.querySelector("#options-height-unit");
  248. const converted = config.height.to(heightSelect.value);
  249. heightInput.value = math.round(converted.value, 3);
  250. }
  251. function configEntityOptions(entity, view) {
  252. const holder = document.querySelector("#options-entity");
  253. holder.innerHTML = "";
  254. const scaleLabel = document.createElement("div");
  255. scaleLabel.classList.add("options-label");
  256. scaleLabel.innerText = "Scale";
  257. const scaleRow = document.createElement("div");
  258. scaleRow.classList.add("options-row");
  259. const scaleInput = document.createElement("input");
  260. scaleInput.classList.add("options-field-numeric");
  261. scaleInput.id = "options-entity-scale";
  262. scaleInput.addEventListener("input", e => {
  263. entity.scale = e.target.value;
  264. if (config.autoFit) {
  265. fitWorld();
  266. }
  267. updateSizes();
  268. updateEntityOptions(entity, view);
  269. updateViewOptions(entity, view);
  270. });
  271. scaleInput.setAttribute("min", 1);
  272. scaleInput.setAttribute("type", "number");
  273. scaleInput.value = entity.scale;
  274. scaleRow.appendChild(scaleInput);
  275. holder.appendChild(scaleLabel);
  276. holder.appendChild(scaleRow);
  277. const nameLabel = document.createElement("div");
  278. nameLabel.classList.add("options-label");
  279. nameLabel.innerText = "Name";
  280. const nameRow = document.createElement("div");
  281. nameRow.classList.add("options-row");
  282. const nameInput = document.createElement("input");
  283. nameInput.classList.add("options-field-text");
  284. nameInput.value = entity.name;
  285. nameInput.addEventListener("input", e => {
  286. entity.name = e.target.value;
  287. updateSizes();
  288. })
  289. nameRow.appendChild(nameInput);
  290. holder.appendChild(nameLabel);
  291. holder.appendChild(nameRow);
  292. }
  293. function updateEntityOptions(entity, view) {
  294. const scaleInput = document.querySelector("#options-entity-scale");
  295. scaleInput.value = entity.scale;
  296. }
  297. function clearEntityOptions() {
  298. const holder = document.querySelector("#options-entity");
  299. holder.innerHTML = "";
  300. }
  301. function configViewOptions(entity, view) {
  302. const holder = document.querySelector("#options-view");
  303. holder.innerHTML = "";
  304. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  305. const label = document.createElement("div");
  306. label.classList.add("options-label");
  307. label.innerText = val.name;
  308. holder.appendChild(label);
  309. const row = document.createElement("div");
  310. row.classList.add("options-row");
  311. holder.appendChild(row);
  312. const input = document.createElement("input");
  313. input.classList.add("options-field-numeric");
  314. input.id = "options-view-" + key + "-input";
  315. input.setAttribute("type", "number");
  316. input.setAttribute("min", 1);
  317. input.value = entity.views[view][key].value;
  318. const select = document.createElement("select");
  319. select.id = "options-view-" + key + "-select"
  320. unitChoices[val.type].forEach(name => {
  321. const option = document.createElement("option");
  322. option.innerText = name;
  323. select.appendChild(option);
  324. });
  325. input.addEventListener("input", e => {
  326. entity.views[view][key] = math.unit(input.value, select.value);
  327. if (config.autoFit) {
  328. fitWorld();
  329. }
  330. updateSizes();
  331. updateEntityOptions(entity, view);
  332. updateViewOptions(entity, view, key);
  333. });
  334. select.addEventListener("input", e => {
  335. entity.views[view][key] = math.unit(input.value, select.value);
  336. if (config.autoFit) {
  337. fitWorld();
  338. }
  339. updateSizes();
  340. updateEntityOptions(entity, view);
  341. updateViewOptions(entity, view, key);
  342. });
  343. row.appendChild(input);
  344. row.appendChild(select);
  345. });
  346. }
  347. function updateViewOptions(entity, view, changed) {
  348. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  349. if (key != changed) {
  350. const input = document.querySelector("#options-view-" + key + "-input");
  351. const select = document.querySelector("#options-view-" + key + "-select");
  352. const currentUnit = select.value;
  353. const convertedAmount = entity.views[view][key].to(currentUnit);
  354. input.value = math.round(convertedAmount.value, 5);
  355. }
  356. });
  357. }
  358. function clearViewOptions() {
  359. const holder = document.querySelector("#options-view");
  360. holder.innerHTML = "";
  361. }
  362. // this is a crime against humanity, and also stolen from
  363. // stack overflow
  364. // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent
  365. const testCanvas = document.createElement("canvas");
  366. testCanvas.id = "test-canvas";
  367. const testCtx = testCanvas.getContext("2d");
  368. function testClick(event) {
  369. // oh my god I can't believe I'm doing this
  370. const target = event.target;
  371. if (navigator.userAgent.indexOf("Firefox") != -1) {
  372. clickDown(target.parentElement, event.clientX, event.clientY);
  373. return;
  374. }
  375. // Get click coordinates
  376. let w = target.width;
  377. let h = target.height;
  378. let ratioW = 1, ratioH = 1;
  379. // Limit the size of the canvas so that very large images don't cause problems)
  380. if (w > 4000) {
  381. ratioW = w / 4000;
  382. w /= ratioW;
  383. h /= ratioW;
  384. }
  385. if (h > 4000) {
  386. ratioH = h / 4000;
  387. w /= ratioH;
  388. h /= ratioH;
  389. }
  390. const ratio = ratioW * ratioH;
  391. var x = event.clientX - target.getBoundingClientRect().x,
  392. y = event.clientY - target.getBoundingClientRect().y,
  393. alpha;
  394. testCtx.canvas.width = w;
  395. testCtx.canvas.height = h;
  396. // Draw image to canvas
  397. // and read Alpha channel value
  398. testCtx.drawImage(target, 0, 0, w, h);
  399. alpha = testCtx.getImageData(Math.floor(x / ratio), Math.floor(y / ratio), 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  400. // If pixel is transparent,
  401. // retrieve the element underneath and trigger it's click event
  402. if (alpha === 0) {
  403. const oldDisplay = target.style.display;
  404. target.style.display = "none";
  405. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  406. newTarget.dispatchEvent(new MouseEvent(event.type, {
  407. "clientX": event.clientX,
  408. "clientY": event.clientY
  409. }));
  410. target.style.display = oldDisplay;
  411. } else {
  412. clickDown(target.parentElement, event.clientX, event.clientY);
  413. }
  414. }
  415. function arrangeEntities(order) {
  416. let x = 0.1;
  417. order.forEach(key => {
  418. document.querySelector("#entity-" + key).dataset.x = x;
  419. x += 0.8 / order.length
  420. });
  421. updateSizes();
  422. }
  423. function removeAllEntities() {
  424. Object.keys(entities).forEach(key => {
  425. removeEntity(document.querySelector("#entity-" + key));
  426. });
  427. }
  428. function removeEntity(element) {
  429. delete entities[element.dataset.key];
  430. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  431. bottomName.parentElement.removeChild(bottomName);
  432. element.parentElement.removeChild(element);
  433. }
  434. function displayEntity(entity, view, x, y) {
  435. const box = document.createElement("div");
  436. box.classList.add("entity-box");
  437. const img = document.createElement("img");
  438. img.classList.add("entity-image");
  439. img.addEventListener("dragstart", e => {
  440. e.preventDefault();
  441. });
  442. const nameTag = document.createElement("div");
  443. nameTag.classList.add("entity-name");
  444. nameTag.innerText = entity.name;
  445. box.appendChild(img);
  446. box.appendChild(nameTag);
  447. const image = entity.views[view].image;
  448. img.src = image.source;
  449. if (image.bottom) {
  450. img.style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  451. }
  452. box.dataset.x = x;
  453. box.dataset.y = y;
  454. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  455. img.addEventListener("touchstart", e => {
  456. const fakeEvent = {
  457. target: e.target,
  458. clientX: e.touches[0].clientX,
  459. clientY: e.touches[0].clientY
  460. };
  461. testClick(fakeEvent);
  462. });
  463. box.id = "entity-" + entityIndex;
  464. box.dataset.key = entityIndex;
  465. box.dataset.view = view;
  466. entities[entityIndex] = entity;
  467. entity.index = entityIndex;
  468. const world = document.querySelector("#entities");
  469. world.appendChild(box);
  470. const bottomName = document.createElement("div");
  471. bottomName.classList.add("bottom-name");
  472. bottomName.id = "bottom-name-" + entityIndex;
  473. bottomName.innerText = entity.name;
  474. bottomName.addEventListener("click", () => select(box));
  475. world.appendChild(bottomName);
  476. entityIndex += 1;
  477. updateEntityElement(entity, box);
  478. if (config.autoFit) {
  479. fitWorld();
  480. }
  481. }
  482. document.addEventListener("DOMContentLoaded", () => {
  483. const stuff = [makeFen].concat(makeBuildings().map(x => x.constructor))
  484. let x = 0.2;
  485. stuff.forEach(entity => {
  486. displayEntity(entity(), entity().defaultView, x, 1);
  487. x += 0.7 / stuff.length;
  488. })
  489. window.addEventListener("wheel", e => {
  490. const dir = e.deltaY < 0 ? 0.9 : 1.1;
  491. config.height = math.multiply(config.height, dir);
  492. updateSizes();
  493. updateWorldOptions();
  494. })
  495. document.querySelector("body").appendChild(testCtx.canvas);
  496. updateSizes();
  497. document.querySelector("#options-height-value").addEventListener("input", e => {
  498. updateWorldHeight();
  499. })
  500. document.querySelector("#options-height-unit").addEventListener("input", e => {
  501. updateWorldHeight();
  502. })
  503. world.addEventListener("mousedown", e => deselect());
  504. document.querySelector("#display").addEventListener("mousedown", deselect);
  505. document.addEventListener("mouseup", e => clickUp(e));
  506. document.addEventListener("touchend", e => {
  507. const fakeEvent = {
  508. target: e.target,
  509. clientX: e.changedTouches[0].clientX,
  510. clientY: e.changedTouches[0].clientY
  511. };
  512. clickUp(fakeEvent);
  513. });
  514. document.querySelector("#entity-view").addEventListener("input", e => {
  515. selected.dataset.view = e.target.value
  516. const image = entities[selected.dataset.key].views[e.target.value].image
  517. selected.querySelector(".entity-image").src = image.source;
  518. if (image.bottom) {
  519. selected.querySelector(".entity-image").style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  520. }
  521. updateSizes();
  522. updateEntityOptions(entities[selected.dataset.key], e.target.value);
  523. updateViewOptions(entities[selected.dataset.key], e.target.value);
  524. });
  525. clearViewList();
  526. document.querySelector("#menu-clear").addEventListener("click", e => {
  527. removeAllEntities();
  528. });
  529. document.querySelector("#menu-order-height").addEventListener("click", e => {
  530. const order = Object.keys(entities).sort((a, b) => {
  531. const entA = entities[a];
  532. const entB = entities[b];
  533. const viewA = document.querySelector("#entity-" + a).dataset.view;
  534. const viewB = document.querySelector("#entity-" + b).dataset.view;
  535. const heightA = entA.views[viewA].height.to("meter").value;
  536. const heightB = entB.views[viewB].height.to("meter").value;
  537. return heightA - heightB;
  538. });
  539. arrangeEntities(order);
  540. });
  541. document.querySelector("#options-world-fit").addEventListener("click", fitWorld);
  542. document.querySelector("#options-world-autofit").addEventListener("input", e => {
  543. config.autoFit = e.target.value;
  544. if (config.autoFit) {
  545. fitWorld();
  546. }
  547. });
  548. document.addEventListener("keydown", e => {
  549. console.log(e)
  550. if (e.key == "Delete" || e.key == "Backspace") {
  551. if (selected) {
  552. removeEntity(selected);
  553. selected = null;
  554. }
  555. }
  556. })
  557. prepareEntities();
  558. });
  559. function prepareEntities() {
  560. availableEntities["buildings"] = makeBuildings();
  561. availableEntities["characters"] = makeCharacters();
  562. availableEntities["objects"] = makeObjects();
  563. availableEntities["vehicles"] = makeVehicles();
  564. const holder = document.querySelector("#spawners");
  565. Object.entries(availableEntities).forEach(([category, entityList]) => {
  566. const select = document.createElement("select");
  567. select.id = "create-entity-" + category;
  568. for (let i = 0; i < entityList.length; i++) {
  569. const entity = entityList[i];
  570. const option = document.createElement("option");
  571. option.value = i;
  572. option.innerText = entity.name;
  573. select.appendChild(option);
  574. };
  575. const button = document.createElement("button");
  576. button.innerText = "Create " + category;
  577. button.addEventListener("click", e => {
  578. const newEntity = entityList[select.value].constructor()
  579. displayEntity(newEntity, newEntity.defaultView, 0.5, 1);
  580. });
  581. holder.appendChild(select);
  582. holder.appendChild(button);
  583. });
  584. }
  585. window.addEventListener("resize", () => {
  586. updateSizes();
  587. })
  588. document.addEventListener("mousemove", (e) => {
  589. if (clicked) {
  590. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  591. clicked.dataset.x = position.x;
  592. clicked.dataset.y = position.y;
  593. updateEntityElement(entities[clicked.dataset.key], clicked);
  594. if (hoveringInDeleteArea(e)) {
  595. document.querySelector("#menubar").classList.add("hover-delete");
  596. } else {
  597. document.querySelector("#menubar").classList.remove("hover-delete");
  598. }
  599. }
  600. });
  601. document.addEventListener("touchmove", (e) => {
  602. if (clicked) {
  603. e.preventDefault();
  604. let x = e.touches[0].clientX;
  605. let y = e.touches[0].clientY;
  606. const position = snapRel(abs2rel({ x: x - dragOffsetX, y: y - dragOffsetY }));
  607. clicked.dataset.x = position.x;
  608. clicked.dataset.y = position.y;
  609. updateEntityElement(entities[clicked.dataset.key], clicked);
  610. // what a hack
  611. // I should centralize this 'fake event' creation...
  612. if (hoveringInDeleteArea({ clientY: y })) {
  613. document.querySelector("#menubar").classList.add("hover-delete");
  614. } else {
  615. document.querySelector("#menubar").classList.remove("hover-delete");
  616. }
  617. }
  618. }, { passive: false });
  619. function fitWorld() {
  620. let max = math.unit(0, "meter");
  621. Object.entries(entities).forEach(([key, entity]) => {
  622. const view = document.querySelector("#entity-" + key).dataset.view;
  623. max = math.max(max, entity.views[view].height);
  624. });
  625. setWorldHeight(config.height, math.multiply(max, 1.1));
  626. }
  627. function updateWorldHeight() {
  628. const value = Math.max(1, document.querySelector("#options-height-value").value);
  629. const unit = document.querySelector("#options-height-unit").value;
  630. const oldHeight = config.height;
  631. setWorldHeight(oldHeight, math.unit(value, unit));
  632. }
  633. function setWorldHeight(oldHeight, newHeight) {
  634. config.height = newHeight;
  635. Object.entries(entities).forEach(([key, entity]) => {
  636. const element = document.querySelector("#entity-" + key);
  637. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  638. element.dataset.x = newPosition.x;
  639. element.dataset.y = newPosition.y;
  640. });
  641. updateSizes();
  642. }