less copy protection, more size visualization
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

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