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.
 
 
 

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