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.
 
 
 

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