less copy protection, more size visualization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

811 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. }
  374. // Get click coordinates
  375. let w = target.width;
  376. let h = target.height;
  377. let ratioW = 1, ratioH = 1;
  378. // Limit the size of the canvas so that very large images don't cause problems)
  379. if (w > 4000) {
  380. ratioW = w / 4000;
  381. w /= ratioW;
  382. h /= ratioW;
  383. }
  384. if (h > 4000) {
  385. ratioH = h / 4000;
  386. w /= ratioH;
  387. h /= ratioH;
  388. }
  389. const ratio = ratioW * ratioH;
  390. var x = event.clientX - target.getBoundingClientRect().x,
  391. y = event.clientY - target.getBoundingClientRect().y,
  392. alpha;
  393. testCtx.canvas.width = w;
  394. testCtx.canvas.height = h;
  395. // Draw image to canvas
  396. // and read Alpha channel value
  397. testCtx.drawImage(target, 0, 0, w, h);
  398. alpha = testCtx.getImageData(Math.floor(x / ratio), Math.floor(y / ratio), 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  399. // If pixel is transparent,
  400. // retrieve the element underneath and trigger it's click event
  401. if (alpha === 0) {
  402. const oldDisplay = target.style.display;
  403. target.style.display = "none";
  404. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  405. newTarget.dispatchEvent(new MouseEvent(event.type, {
  406. "clientX": event.clientX,
  407. "clientY": event.clientY
  408. }));
  409. target.style.display = oldDisplay;
  410. } else {
  411. clickDown(target.parentElement, event.clientX, event.clientY);
  412. }
  413. }
  414. function arrangeEntities(order) {
  415. let x = 0.1;
  416. order.forEach(key => {
  417. document.querySelector("#entity-" + key).dataset.x = x;
  418. x += 0.8 / order.length
  419. });
  420. updateSizes();
  421. }
  422. function removeAllEntities() {
  423. Object.keys(entities).forEach(key => {
  424. removeEntity(document.querySelector("#entity-" + key));
  425. });
  426. }
  427. function removeEntity(element) {
  428. delete entities[element.dataset.key];
  429. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  430. bottomName.parentElement.removeChild(bottomName);
  431. element.parentElement.removeChild(element);
  432. }
  433. function displayEntity(entity, view, x, y) {
  434. const box = document.createElement("div");
  435. box.classList.add("entity-box");
  436. const img = document.createElement("img");
  437. img.classList.add("entity-image");
  438. img.addEventListener("dragstart", e => {
  439. e.preventDefault();
  440. });
  441. const nameTag = document.createElement("div");
  442. nameTag.classList.add("entity-name");
  443. nameTag.innerText = entity.name;
  444. box.appendChild(img);
  445. box.appendChild(nameTag);
  446. const image = entity.views[view].image;
  447. img.src = image.source;
  448. if (image.bottom) {
  449. img.style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  450. }
  451. box.dataset.x = x;
  452. box.dataset.y = y;
  453. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  454. img.addEventListener("touchstart", e => {
  455. const fakeEvent = {
  456. target: e.target,
  457. clientX: e.touches[0].clientX,
  458. clientY: e.touches[0].clientY
  459. };
  460. testClick(fakeEvent);
  461. });
  462. box.id = "entity-" + entityIndex;
  463. box.dataset.key = entityIndex;
  464. box.dataset.view = view;
  465. entities[entityIndex] = entity;
  466. entity.index = entityIndex;
  467. const world = document.querySelector("#entities");
  468. world.appendChild(box);
  469. const bottomName = document.createElement("div");
  470. bottomName.classList.add("bottom-name");
  471. bottomName.id = "bottom-name-" + entityIndex;
  472. bottomName.innerText = entity.name;
  473. bottomName.addEventListener("click", () => select(box));
  474. world.appendChild(bottomName);
  475. entityIndex += 1;
  476. updateEntityElement(entity, box);
  477. if (config.autoFit) {
  478. fitWorld();
  479. }
  480. }
  481. document.addEventListener("DOMContentLoaded", () => {
  482. const stuff = [makeFen].concat(makeBuildings().map(x => x.constructor))
  483. let x = 0.2;
  484. stuff.forEach(entity => {
  485. displayEntity(entity(), entity().defaultView, x, 1);
  486. x += 0.7 / stuff.length;
  487. })
  488. window.addEventListener("wheel", e => {
  489. const dir = e.deltaY < 0 ? 0.9 : 1.1;
  490. config.height = math.multiply(config.height, dir);
  491. updateSizes();
  492. updateWorldOptions();
  493. })
  494. document.querySelector("body").appendChild(testCtx.canvas);
  495. updateSizes();
  496. document.querySelector("#options-height-value").addEventListener("input", e => {
  497. updateWorldHeight();
  498. })
  499. document.querySelector("#options-height-unit").addEventListener("input", e => {
  500. updateWorldHeight();
  501. })
  502. world.addEventListener("mousedown", e => deselect());
  503. document.querySelector("#display").addEventListener("mousedown", deselect);
  504. document.addEventListener("mouseup", e => clickUp(e));
  505. document.addEventListener("touchend", e => {
  506. const fakeEvent = {
  507. target: e.target,
  508. clientX: e.changedTouches[0].clientX,
  509. clientY: e.changedTouches[0].clientY
  510. };
  511. clickUp(fakeEvent);
  512. });
  513. document.querySelector("#entity-view").addEventListener("input", e => {
  514. selected.dataset.view = e.target.value
  515. const image = entities[selected.dataset.key].views[e.target.value].image
  516. selected.querySelector(".entity-image").src = image.source;
  517. if (image.bottom) {
  518. selected.querySelector(".entity-image").style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  519. }
  520. updateSizes();
  521. updateEntityOptions(entities[selected.dataset.key], e.target.value);
  522. updateViewOptions(entities[selected.dataset.key], e.target.value);
  523. });
  524. clearViewList();
  525. document.querySelector("#menu-clear").addEventListener("click", e => {
  526. removeAllEntities();
  527. });
  528. document.querySelector("#menu-order-height").addEventListener("click", e => {
  529. const order = Object.keys(entities).sort((a, b) => {
  530. const entA = entities[a];
  531. const entB = entities[b];
  532. const viewA = document.querySelector("#entity-" + a).dataset.view;
  533. const viewB = document.querySelector("#entity-" + b).dataset.view;
  534. const heightA = entA.views[viewA].height.to("meter").value;
  535. const heightB = entB.views[viewB].height.to("meter").value;
  536. return heightA - heightB;
  537. });
  538. arrangeEntities(order);
  539. });
  540. document.querySelector("#options-world-fit").addEventListener("click", fitWorld);
  541. document.querySelector("#options-world-autofit").addEventListener("input", e => {
  542. config.autoFit = e.target.value;
  543. if (config.autoFit) {
  544. fitWorld();
  545. }
  546. });
  547. document.addEventListener("keydown", e => {
  548. console.log(e)
  549. if (e.key == "Delete" || e.key == "Backspace") {
  550. if (selected) {
  551. removeEntity(selected);
  552. selected = null;
  553. }
  554. }
  555. })
  556. prepareEntities();
  557. });
  558. function prepareEntities() {
  559. availableEntities["buildings"] = makeBuildings();
  560. availableEntities["characters"] = makeCharacters();
  561. const holder = document.querySelector("#spawners");
  562. Object.entries(availableEntities).forEach(([category, entityList]) => {
  563. const select = document.createElement("select");
  564. select.id = "create-entity-" + category;
  565. for (let i = 0; i < entityList.length; i++) {
  566. const entity = entityList[i];
  567. const option = document.createElement("option");
  568. option.value = i;
  569. option.innerText = entity.name;
  570. select.appendChild(option);
  571. };
  572. const button = document.createElement("button");
  573. button.innerText = "Create " + category;
  574. button.addEventListener("click", e => {
  575. const newEntity = entityList[select.value].constructor()
  576. displayEntity(newEntity, newEntity.defaultView, 0.5, 1);
  577. });
  578. holder.appendChild(select);
  579. holder.appendChild(button);
  580. });
  581. }
  582. window.addEventListener("resize", () => {
  583. updateSizes();
  584. })
  585. document.addEventListener("mousemove", (e) => {
  586. if (clicked) {
  587. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  588. clicked.dataset.x = position.x;
  589. clicked.dataset.y = position.y;
  590. updateEntityElement(entities[clicked.dataset.key], clicked);
  591. if (hoveringInDeleteArea(e)) {
  592. document.querySelector("#menubar").classList.add("hover-delete");
  593. } else {
  594. document.querySelector("#menubar").classList.remove("hover-delete");
  595. }
  596. }
  597. });
  598. document.addEventListener("touchmove", (e) => {
  599. if (clicked) {
  600. e.preventDefault();
  601. let x = e.touches[0].clientX;
  602. let y = e.touches[0].clientY;
  603. const position = snapRel(abs2rel({ x: x - dragOffsetX, y: y - dragOffsetY }));
  604. clicked.dataset.x = position.x;
  605. clicked.dataset.y = position.y;
  606. updateEntityElement(entities[clicked.dataset.key], clicked);
  607. // what a hack
  608. // I should centralize this 'fake event' creation...
  609. if (hoveringInDeleteArea({ clientY: y })) {
  610. document.querySelector("#menubar").classList.add("hover-delete");
  611. } else {
  612. document.querySelector("#menubar").classList.remove("hover-delete");
  613. }
  614. }
  615. }, { passive: false });
  616. function fitWorld() {
  617. let max = math.unit(0, "meter");
  618. Object.entries(entities).forEach(([key, entity]) => {
  619. const view = document.querySelector("#entity-" + key).dataset.view;
  620. max = math.max(max, entity.views[view].height);
  621. });
  622. setWorldHeight(config.height, math.multiply(max, 1.1));
  623. }
  624. function updateWorldHeight() {
  625. const value = Math.max(1, document.querySelector("#options-height-value").value);
  626. const unit = document.querySelector("#options-height-unit").value;
  627. const oldHeight = config.height;
  628. setWorldHeight(oldHeight, math.unit(value, unit));
  629. }
  630. function setWorldHeight(oldHeight, newHeight) {
  631. config.height = newHeight;
  632. Object.entries(entities).forEach(([key, entity]) => {
  633. const element = document.querySelector("#entity-" + key);
  634. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  635. element.dataset.x = newPosition.x;
  636. element.dataset.y = newPosition.y;
  637. });
  638. updateSizes();
  639. }