less copy protection, more size visualization
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

625 lines
19 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. ],
  15. mass: [
  16. "kilograms"
  17. ]
  18. }
  19. const config = {
  20. height: math.unit(10, "meters"),
  21. minLineSize: 50,
  22. maxLineSize: 250
  23. }
  24. const entities = {
  25. }
  26. function constrainRel(coords) {
  27. return {
  28. x: Math.min(Math.max(coords.x, 0), 1),
  29. y: Math.min(Math.max(coords.y, 0), 1)
  30. }
  31. }
  32. function snapRel(coords) {
  33. return constrainRel({
  34. x: coords.x,
  35. y: altHeld ? coords.y : (Math.abs(coords.y - 1) < 0.05 ? 1 : coords.y)
  36. });
  37. }
  38. function adjustAbs(coords, oldHeight, newHeight) {
  39. return { x: coords.x, y: 1 + (coords.y - 1) * math.divide(oldHeight, newHeight) };
  40. }
  41. function rel2abs(coords) {
  42. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  43. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  44. return { x: coords.x * canvasWidth, y: coords.y * canvasHeight };
  45. }
  46. function abs2rel(coords) {
  47. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  48. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  49. return { x: coords.x / canvasWidth, y: coords.y / canvasHeight };
  50. }
  51. function updateEntityElement(entity, element) {
  52. const position = rel2abs({ x: element.dataset.x, y: element.dataset.y });
  53. const view = element.dataset.view;
  54. element.style.left = position.x + "px";
  55. element.style.top = position.y + "px";
  56. const canvasHeight = document.querySelector("#display").clientHeight;
  57. const pixels = math.divide(entity.views[view].height, config.height) * (canvasHeight - 100);
  58. element.style.setProperty("--height", pixels + "px");
  59. element.querySelector(".entity-name").innerText = entity.name;
  60. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  61. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  62. bottomName.style.left = position.x + entX + "px";
  63. bottomName.style.top = "95vh";
  64. bottomName.innerText = entity.name;
  65. }
  66. function updateSizes() {
  67. drawScale();
  68. Object.entries(entities).forEach(([key, entity]) => {
  69. const element = document.querySelector("#entity-" + key);
  70. updateEntityElement(entity, element);
  71. });
  72. }
  73. function drawScale() {
  74. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer, heightPer) {
  75. let total = heightPer.clone();
  76. total.value = 0;
  77. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  78. drawTick(ctx, 50, y, total);
  79. total = math.add(total, heightPer);
  80. }
  81. }
  82. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y, value) {
  83. const oldStroke = ctx.strokeStyle;
  84. const oldFill = ctx.fillStyle;
  85. ctx.beginPath();
  86. ctx.moveTo(x, y);
  87. ctx.lineTo(x + 20, y);
  88. ctx.strokeStyle = "#000000";
  89. ctx.stroke();
  90. ctx.beginPath();
  91. ctx.moveTo(x + 20, y);
  92. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  93. ctx.strokeStyle = "#aaaaaa";
  94. ctx.stroke();
  95. ctx.beginPath();
  96. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  97. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  98. ctx.strokeStyle = "#000000";
  99. ctx.stroke();
  100. const oldFont = ctx.font;
  101. ctx.font = 'normal 24pt coda';
  102. ctx.fillStyle = "#dddddd";
  103. ctx.beginPath();
  104. ctx.fillText(value.format({ precision: 3 }), x + 20, y + 35);
  105. ctx.font = oldFont;
  106. ctx.strokeStyle = oldStroke;
  107. ctx.fillStyle = oldFill;
  108. }
  109. const canvas = document.querySelector("#display");
  110. /** @type {CanvasRenderingContext2D} */
  111. const ctx = canvas.getContext("2d");
  112. let pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  113. let heightPer = config.height.clone();
  114. heightPer.value = 1;
  115. if (pixelsPer < config.minLineSize) {
  116. heightPer.value /= pixelsPer / config.minLineSize;
  117. pixelsPer = config.minLineSize;
  118. }
  119. if (pixelsPer > config.maxLineSize) {
  120. heightPer.value /= pixelsPer / config.maxLineSize;
  121. pixelsPer = config.maxLineSize;
  122. }
  123. ctx.clearRect(0, 0, canvas.width, canvas.height);
  124. ctx.scale(1, 1);
  125. ctx.canvas.width = canvas.clientWidth;
  126. ctx.canvas.height = canvas.clientHeight;
  127. ctx.beginPath();
  128. ctx.moveTo(50, 50);
  129. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  130. ctx.stroke();
  131. ctx.beginPath();
  132. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  133. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  134. ctx.stroke();
  135. drawTicks(ctx, pixelsPer, heightPer);
  136. }
  137. function makeEntity() {
  138. const entityTemplate = {
  139. name: "",
  140. author: "",
  141. scale: 1,
  142. views: {
  143. body: {
  144. attributes: {
  145. height: {
  146. name: "Height",
  147. power: 1,
  148. type: "length",
  149. base: math.unit(1, "meter")
  150. },
  151. weight: {
  152. name: "Weight",
  153. power: 3,
  154. type: "mass",
  155. base: math.unit(80, "kg")
  156. }
  157. },
  158. image: "./silhouette.png",
  159. name: "Body"
  160. },
  161. pepper: {
  162. attributes: {
  163. height: {
  164. name: "Height",
  165. power: 1,
  166. type: "length",
  167. base: math.unit(50, "centimeter")
  168. },
  169. weight: {
  170. name: "Weight",
  171. power: 3,
  172. type: "mass",
  173. base: math.unit(1, "kg")
  174. }
  175. },
  176. image: "./pepper.png",
  177. name: "Pepper"
  178. }
  179. },
  180. init: function () {
  181. Object.values(this.views).forEach(view => {
  182. view.parent = this;
  183. Object.entries(view.attributes).forEach(([key, val]) => {
  184. Object.defineProperty(
  185. view,
  186. key,
  187. {
  188. get: function() {
  189. return math.multiply(Math.pow(this.parent.scale, this.attributes[key].power), this.attributes[key].base);
  190. },
  191. set: function(value) {
  192. const newScale = Math.pow(math.divide(value, this.attributes[key].base), 1 / this.attributes[key].power);
  193. this.parent.scale = newScale;
  194. }
  195. }
  196. )
  197. });
  198. });
  199. delete this.init;
  200. return this;
  201. }
  202. }.init();
  203. return entityTemplate;
  204. }
  205. function clickDown(target, x, y) {
  206. clicked = target;
  207. const rect = target.getBoundingClientRect();
  208. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  209. let entY = document.querySelector("#entities").getBoundingClientRect().y;
  210. dragOffsetX = x - rect.left + entX;
  211. dragOffsetY = y - rect.top + entY;
  212. clickTimeout = setTimeout(() => { dragging = true }, 200)
  213. }
  214. function clickUp() {
  215. clearTimeout(clickTimeout);
  216. if (clicked) {
  217. if (dragging) {
  218. dragging = false;
  219. } else {
  220. select(clicked);
  221. }
  222. clicked = null;
  223. }
  224. }
  225. function deselect() {
  226. if (selected) {
  227. selected.classList.remove("selected");
  228. }
  229. selected = null;
  230. clearViewList();
  231. clearEntityOptions();
  232. clearViewOptions();
  233. }
  234. function select(target) {
  235. deselect();
  236. selected = target;
  237. selectedEntity = entities[target.dataset.key];
  238. selected.classList.add("selected");
  239. entityInfo(selectedEntity, target.dataset.view);
  240. configViewList(selectedEntity, target.dataset.view);
  241. configEntityOptions(selectedEntity, target.dataset.view);
  242. configViewOptions(selectedEntity, target.dataset.view);
  243. }
  244. function entityInfo(entity, view) {
  245. document.querySelector("#entity-name").innerText = "Name: " + entity.name;
  246. document.querySelector("#entity-author").innerText = "Author: " + entity.author;
  247. document.querySelector("#entity-height").innerText = "Height: " + entity.views[view].height.format({ precision: 3 });
  248. }
  249. function configViewList(entity, selectedView) {
  250. const list = document.querySelector("#entity-view");
  251. list.innerHTML = "";
  252. list.style.display = "block";
  253. Object.keys(entity.views).forEach(view => {
  254. const option = document.createElement("option");
  255. option.innerText = entity.views[view].name;
  256. option.value = view;
  257. if (view === selectedView) {
  258. option.selected = true;
  259. }
  260. list.appendChild(option);
  261. });
  262. }
  263. function clearViewList() {
  264. const list = document.querySelector("#entity-view");
  265. list.innerHTML = "";
  266. list.style.display = "none";
  267. }
  268. function configEntityOptions(entity, view) {
  269. const holder = document.querySelector("#options-entity");
  270. holder.innerHTML = "";
  271. const scaleLabel = document.createElement("div");
  272. scaleLabel.classList.add("options-label");
  273. scaleLabel.innerText = "Scale";
  274. const scaleRow = document.createElement("div");
  275. scaleRow.classList.add("options-row");
  276. const scaleInput = document.createElement("input");
  277. scaleInput.classList.add("options-field-numeric");
  278. scaleInput.id = "options-entity-scale";
  279. scaleInput.addEventListener("input", e => {
  280. entity.scale = e.target.value;
  281. updateSizes();
  282. updateEntityOptions(entity, view);
  283. updateViewOptions(entity, view);
  284. });
  285. scaleInput.setAttribute("min", 1);
  286. scaleInput.setAttribute("type", "number");
  287. scaleInput.value = entity.scale;
  288. scaleRow.appendChild(scaleInput);
  289. holder.appendChild(scaleLabel);
  290. holder.appendChild(scaleRow);
  291. const nameLabel = document.createElement("div");
  292. nameLabel.classList.add("options-label");
  293. nameLabel.innerText = "Name";
  294. const nameRow = document.createElement("div");
  295. nameRow.classList.add("options-row");
  296. const nameInput = document.createElement("input");
  297. nameInput.classList.add("options-field-text");
  298. nameInput.value = entity.name;
  299. nameInput.addEventListener("input", e => {
  300. entity.name = e.target.value;
  301. updateSizes();
  302. })
  303. nameRow.appendChild(nameInput);
  304. holder.appendChild(nameLabel);
  305. holder.appendChild(nameRow);
  306. }
  307. function updateEntityOptions(entity, view) {
  308. const scaleInput = document.querySelector("#options-entity-scale");
  309. scaleInput.value = entity.scale;
  310. }
  311. function clearEntityOptions() {
  312. const holder = document.querySelector("#options-entity");
  313. holder.innerHTML = "";
  314. }
  315. function configViewOptions(entity, view) {
  316. const holder = document.querySelector("#options-view");
  317. holder.innerHTML = "";
  318. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  319. const label = document.createElement("div");
  320. label.classList.add("options-label");
  321. label.innerText = val.name;
  322. holder.appendChild(label);
  323. const row = document.createElement("div");
  324. row.classList.add("options-row");
  325. holder.appendChild(row);
  326. const input = document.createElement("input");
  327. input.classList.add("options-field-numeric");
  328. input.id = "options-view-" + key + "-input";
  329. input.setAttribute("type", "number");
  330. input.setAttribute("min", 1);
  331. input.value = entity.views[view][key].value;
  332. const select = document.createElement("select");
  333. select.id = "options-view-" + key + "-select"
  334. unitChoices[val.type].forEach(name => {
  335. const option = document.createElement("option");
  336. option.innerText = name;
  337. select.appendChild(option);
  338. });
  339. input.addEventListener("input", e => {
  340. entity.views[view][key] = math.unit(input.value, select.value);
  341. updateSizes();
  342. updateEntityOptions(entity, view);
  343. updateViewOptions(entity, view, key);
  344. });
  345. select.addEventListener("input", e => {
  346. entity.views[view][key] = math.unit(input.value, select.value);
  347. updateSizes();
  348. updateEntityOptions(entity, view);
  349. updateViewOptions(entity, view, key);
  350. });
  351. row.appendChild(input);
  352. row.appendChild(select);
  353. });
  354. }
  355. function updateViewOptions(entity, view, changed) {
  356. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  357. if (key != changed) {
  358. const input = document.querySelector("#options-view-" + key + "-input");
  359. const select = document.querySelector("#options-view-" + key + "-select");
  360. const currentUnit = select.value;
  361. const convertedAmount = entity.views[view][key].to(currentUnit);
  362. console.log(convertedAmount);
  363. input.value = math.round(convertedAmount.value, 5);
  364. }
  365. });
  366. }
  367. function clearViewOptions() {
  368. const holder = document.querySelector("#options-view");
  369. holder.innerHTML = "";
  370. }
  371. // this is a crime against humanity, and also stolen from
  372. // stack overflow
  373. // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent
  374. const testCanvas = document.createElement("canvas");
  375. testCanvas.id = "test-canvas";
  376. const testCtx = testCanvas.getContext("2d");
  377. function testClick(event) {
  378. const target = event.target;
  379. // Get click coordinates
  380. var x = event.clientX - target.getBoundingClientRect().x,
  381. y = event.clientY - target.getBoundingClientRect().y,
  382. w = testCtx.canvas.width = target.width,
  383. h = testCtx.canvas.height = target.height,
  384. alpha;
  385. // Draw image to canvas
  386. // and read Alpha channel value
  387. testCtx.drawImage(target, 0, 0, w, h);
  388. alpha = testCtx.getImageData(x, y, 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  389. // If pixel is transparent,
  390. // retrieve the element underneath and trigger it's click event
  391. if (alpha === 0) {
  392. const oldDisplay = target.style.display;
  393. target.style.display = "none";
  394. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  395. newTarget.dispatchEvent(new MouseEvent(event.type, {
  396. "clientX": event.clientX,
  397. "clientY": event.clientY
  398. }));
  399. target.style.display = oldDisplay;
  400. } else {
  401. clickDown(target.parentElement, event.clientX, event.clientY);
  402. }
  403. }
  404. function displayEntity(entity, view, x, y) {
  405. const box = document.createElement("div");
  406. box.classList.add("entity-box");
  407. const img = document.createElement("img");
  408. img.classList.add("entity-image");
  409. const nameTag = document.createElement("div");
  410. nameTag.classList.add("entity-name");
  411. nameTag.innerText = entity.name;
  412. box.appendChild(img);
  413. box.appendChild(nameTag);
  414. img.src = entity.views[view].image
  415. box.dataset.x = x;
  416. box.dataset.y = y;
  417. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  418. img.addEventListener("touchstart", e => {
  419. const fakeEvent = {
  420. target: e.target,
  421. clientX: e.touches[0].clientX,
  422. clientY: e.touches[0].clientY
  423. };
  424. testClick(fakeEvent);});
  425. box.id = "entity-" + entityIndex;
  426. box.dataset.key = entityIndex;
  427. box.dataset.view = view;
  428. entities[entityIndex] = entity;
  429. const world = document.querySelector("#entities");
  430. world.appendChild(box);
  431. const bottomName = document.createElement("div");
  432. bottomName.classList.add("bottom-name");
  433. bottomName.id = "bottom-name-" + entityIndex;
  434. bottomName.innerText = entity.name;
  435. bottomName.addEventListener("click", () => select(box));
  436. world.appendChild(bottomName);
  437. entityIndex += 1;
  438. updateEntityElement(entity, box);
  439. }
  440. document.addEventListener("DOMContentLoaded", () => {
  441. for (let x = 0; x < 5; x++) {
  442. const entity = makeEntity();
  443. entity.name = "Dude";
  444. entity.author = "Fen"
  445. const x = 0.25 + Math.random() * 0.5;
  446. const y = 0.25 + Math.random() * 0.5;
  447. displayEntity(entity, "body", x, y);
  448. }
  449. document.querySelector("body").appendChild(testCtx.canvas);
  450. updateSizes();
  451. document.querySelector("#options-height-value").addEventListener("input", e => {
  452. updateWorldHeight();
  453. })
  454. document.querySelector("#options-height-unit").addEventListener("input", e => {
  455. updateWorldHeight();
  456. })
  457. world.addEventListener("mousedown", e => deselect());
  458. document.addEventListener("mouseup", e => clickUp());
  459. document.addEventListener("touchend", e => clickUp());
  460. document.querySelector("#entity-view").addEventListener("input", e => {
  461. selected.dataset.view = e.target.value
  462. selected.querySelector(".entity-image").src = entities[selected.dataset.key].views[e.target.value].image;
  463. updateSizes();
  464. updateEntityOptions(entities[selected.dataset.key], e.target.value);
  465. updateViewOptions(entities[selected.dataset.key], e.target.value);
  466. });
  467. clearViewList();
  468. });
  469. window.addEventListener("resize", () => {
  470. updateSizes();
  471. })
  472. document.addEventListener("mousemove", (e) => {
  473. if (clicked) {
  474. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  475. clicked.dataset.x = position.x;
  476. clicked.dataset.y = position.y;
  477. updateEntityElement(entities[clicked.dataset.key], clicked);
  478. }
  479. });
  480. document.addEventListener("touchmove", (e) => {
  481. if (clicked) {
  482. e.preventDefault();
  483. let x = e.touches[0].clientX;
  484. let y = e.touches[0].clientY;
  485. const position = snapRel(abs2rel({ x: x - dragOffsetX, y: y - dragOffsetY }));
  486. clicked.dataset.x = position.x;
  487. clicked.dataset.y = position.y;
  488. updateEntityElement(entities[clicked.dataset.key], clicked);
  489. }
  490. }, {passive: false});
  491. function updateWorldHeight() {
  492. const value = Math.max(1, document.querySelector("#options-height-value").value);
  493. const unit = document.querySelector("#options-height-unit").value;
  494. const oldHeight = config.height;
  495. config.height = math.unit(value + " " + unit)
  496. Object.entries(entities).forEach(([key, entity]) => {
  497. const element = document.querySelector("#entity-" + key);
  498. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  499. element.dataset.x = newPosition.x;
  500. element.dataset.y = newPosition.y;
  501. });
  502. updateSizes();
  503. }