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.
 
 
 

397 lignes
11 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 config = {
  11. height: math.unit(10, "meters"),
  12. minLineSize: 50,
  13. maxLineSize: 250
  14. }
  15. const entities = {
  16. }
  17. function constrainRel(coords) {
  18. return {
  19. x: Math.min(Math.max(coords.x, 0), 1),
  20. y: Math.min(Math.max(coords.y, 0), 1)
  21. }
  22. }
  23. function snapRel(coords) {
  24. return constrainRel({
  25. x: coords.x,
  26. y: altHeld ? coords.y : (Math.abs(coords.y - 1) < 0.05 ? 1 : coords.y)
  27. });
  28. }
  29. function adjustAbs(coords, oldHeight, newHeight) {
  30. return { x: coords.x, y: 1 + (coords.y - 1) * math.divide(oldHeight, newHeight) };
  31. }
  32. function rel2abs(coords) {
  33. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  34. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  35. return { x: coords.x * canvasWidth, y: coords.y * canvasHeight };
  36. }
  37. function abs2rel(coords) {
  38. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  39. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  40. return { x: coords.x / canvasWidth, y: coords.y / canvasHeight };
  41. }
  42. function updateEntityElement(entity, element) {
  43. const position = rel2abs({ x: element.dataset.x, y: element.dataset.y });
  44. const view = element.dataset.view;
  45. element.style.left = position.x + "px";
  46. element.style.top = position.y + "px";
  47. const canvasHeight = document.querySelector("#display").clientHeight;
  48. const pixels = math.divide(entity.views[view].height, config.height) * canvasHeight;
  49. element.style.setProperty("--height", pixels + "px");
  50. }
  51. function updateSizes() {
  52. drawScale();
  53. Object.entries(entities).forEach(([key, entity]) => {
  54. const element = document.querySelector("#entity-" + key);
  55. updateEntityElement(entity, element);
  56. });
  57. }
  58. function drawScale() {
  59. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer, heightPer) {
  60. let total = heightPer.clone();
  61. total.value = 0;
  62. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  63. drawTick(ctx, 50, y, total);
  64. total = math.add(total, heightPer);
  65. }
  66. }
  67. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y, value) {
  68. const oldStroke = ctx.strokeStyle;
  69. const oldFill = ctx.fillStyle;
  70. ctx.beginPath();
  71. ctx.moveTo(x, y);
  72. ctx.lineTo(x + 20, y);
  73. ctx.strokeStyle = "#000000";
  74. ctx.stroke();
  75. ctx.beginPath();
  76. ctx.moveTo(x + 20, y);
  77. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  78. ctx.strokeStyle = "#aaaaaa";
  79. ctx.stroke();
  80. ctx.beginPath();
  81. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  82. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  83. ctx.strokeStyle = "#000000";
  84. ctx.stroke();
  85. const oldFont = ctx.font;
  86. ctx.font = 'normal 24pt coda';
  87. ctx.fillStyle = "#dddddd";
  88. ctx.beginPath();
  89. ctx.fillText(value.format({ precision: 3 }), x + 20, y + 35);
  90. ctx.font = oldFont;
  91. ctx.strokeStyle = oldStroke;
  92. ctx.fillStyle = oldFill;
  93. }
  94. const canvas = document.querySelector("#display");
  95. /** @type {CanvasRenderingContext2D} */
  96. const ctx = canvas.getContext("2d");
  97. let pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  98. let heightPer = config.height.clone();
  99. heightPer.value = 1;
  100. if (pixelsPer < config.minLineSize) {
  101. heightPer.value /= pixelsPer / config.minLineSize;
  102. pixelsPer = config.minLineSize;
  103. }
  104. if (pixelsPer > config.maxLineSize) {
  105. heightPer.value /= pixelsPer / config.maxLineSize;
  106. pixelsPer = config.maxLineSize;
  107. }
  108. ctx.clearRect(0, 0, canvas.width, canvas.height);
  109. ctx.scale(1, 1);
  110. ctx.canvas.width = canvas.clientWidth;
  111. ctx.canvas.height = canvas.clientHeight;
  112. ctx.beginPath();
  113. ctx.moveTo(50, 50);
  114. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  115. ctx.stroke();
  116. ctx.beginPath();
  117. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  118. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  119. ctx.stroke();
  120. drawTicks(ctx, pixelsPer, heightPer);
  121. }
  122. function makeEntity() {
  123. const entityTemplate = {
  124. name: "",
  125. author: "",
  126. scale: 1,
  127. views: {
  128. body: {
  129. baseHeight: math.unit(Math.random() * 1 + 1, "meter"),
  130. get height() {
  131. return math.multiply(this.parent.scale, this.baseHeight);
  132. },
  133. set height(value) {
  134. this.parent.scale = math.divide(value, this.baseHeight);
  135. }
  136. }
  137. },
  138. init: function () {
  139. console.log(this);
  140. Object.entries(this.views).forEach(([key, val]) => {
  141. val.parent = this;
  142. });
  143. delete this.init;
  144. return this;
  145. }
  146. }.init();
  147. return entityTemplate;
  148. }
  149. function clickDown(e) {
  150. clicked = e.target;
  151. const rect = e.target.getBoundingClientRect();
  152. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  153. let entY = document.querySelector("#entities").getBoundingClientRect().y;
  154. dragOffsetX = e.clientX - rect.left + entX - rect.width / 2;
  155. dragOffsetY = e.clientY - rect.top + entY - rect.height;
  156. clickTimeout = setTimeout(() => { dragging = true }, 100)
  157. }
  158. function clickUp() {
  159. clearTimeout(clickTimeout);
  160. if (clicked) {
  161. if (dragging) {
  162. dragging = false;
  163. } else {
  164. select(clicked);
  165. }
  166. clicked = null;
  167. }
  168. }
  169. function deselect() {
  170. if (selected) {
  171. selected.classList.remove("selected");
  172. }
  173. selected = null;
  174. clearEntityOptions();
  175. clearViewOptions();
  176. }
  177. function select(target) {
  178. deselect();
  179. selected = target;
  180. selectedEntity = entities[target.dataset.key];
  181. selected.classList.add("selected");
  182. entityInfo(selectedEntity, target.dataset.view);
  183. configEntityOptions(selectedEntity);
  184. configViewOptions(selectedEntity, target.dataset.view);
  185. }
  186. function entityInfo(entity, view) {
  187. document.querySelector("#entity-name").innerText = "Name: " + entity.name;
  188. document.querySelector("#entity-author").innerText = "Author: " + entity.author;
  189. document.querySelector("#entity-height").innerText = "Height: " + entity.views[view].height.format({ precision: 3 });
  190. }
  191. function configEntityOptions(entity) {
  192. const holder = document.querySelector("#options-entity");
  193. holder.innerHTML = "";
  194. const scaleLabel = document.createElement("div");
  195. scaleLabel.classList.add("options-label");
  196. scaleLabel.innerText = "Scale";
  197. const scaleRow = document.createElement("div");
  198. scaleRow.classList.add("options-row");
  199. const scaleInput = document.createElement("input");
  200. scaleInput.classList.add("options-field-numeric");
  201. scaleInput.addEventListener("input", e => {
  202. entity.scale = e.target.value;
  203. updateSizes();
  204. });
  205. scaleInput.setAttribute("min", 1);
  206. scaleInput.setAttribute("type", "number");
  207. scaleInput.value = entity.scale;
  208. scaleRow.appendChild(scaleInput);
  209. holder.appendChild(scaleLabel);
  210. holder.appendChild(scaleRow);
  211. }
  212. function clearEntityOptions() {
  213. const holder = document.querySelector("#options-entity");
  214. holder.innerHTML = "";
  215. }
  216. function configViewOptions(entity, view) {
  217. }
  218. function clearViewOptions(entity, view) {
  219. }
  220. // this is a crime against humanity, and also stolen from
  221. // stack overflow
  222. // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent
  223. const testCtx = document.createElement("canvas").getContext("2d");
  224. function testClick(event) {
  225. const target = event.target;
  226. // Get click coordinates
  227. var x = event.clientX - target.getBoundingClientRect().x,
  228. y = event.clientY - target.getBoundingClientRect().y,
  229. w = testCtx.canvas.width = target.width,
  230. h = testCtx.canvas.height = target.height,
  231. alpha;
  232. console.log(x, y);
  233. // Draw image to canvas
  234. // and read Alpha channel value
  235. testCtx.drawImage(target, 0, 0, w, h);
  236. alpha = testCtx.getImageData(x, y, 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  237. console.log(alpha)
  238. // If pixel is transparent,
  239. // retrieve the element underneath and trigger it's click event
  240. if (alpha === 0) {
  241. const oldDisplay = target.style.display;
  242. target.style.display = "none";
  243. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  244. newTarget.dispatchEvent(new MouseEvent("mousedown", {
  245. "clientX": event.clientX,
  246. "clientY": event.clientY
  247. }));
  248. target.style.display = oldDisplay;
  249. } else {
  250. clickDown(event);
  251. }
  252. }
  253. function displayEntity(entity, view, x, y) {
  254. const location = entity.location;
  255. const img = document.createElement("img");
  256. img.src = "./man.svg"
  257. img.classList.add("entity");
  258. img.dataset.x = x;
  259. img.dataset.y = y;
  260. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  261. img.id = "entity-" + entityIndex;
  262. img.dataset.key = entityIndex;
  263. img.dataset.view = view;
  264. entities[entityIndex] = entity;
  265. entityIndex += 1;
  266. const world = document.querySelector("#entities");
  267. world.appendChild(img);
  268. updateEntityElement(entity, img);
  269. }
  270. document.addEventListener("DOMContentLoaded", () => {
  271. for (let x = 0; x < 5; x++) {
  272. const entity = makeEntity();
  273. entity.name = "Dude";
  274. entity.author = "Fen"
  275. const x = 0.25 + Math.random() * 0.5;
  276. const y = 0.25 + Math.random() * 0.5;
  277. displayEntity(entity, "body", x, y);
  278. }
  279. document.querySelector("body").appendChild(testCtx.canvas);
  280. updateSizes();
  281. document.querySelector("#options-height-value").addEventListener("input", e => {
  282. updateWorldHeight();
  283. })
  284. document.querySelector("#options-height-unit").addEventListener("input", e => {
  285. updateWorldHeight();
  286. })
  287. world.addEventListener("mousedown", e => deselect());
  288. document.addEventListener("mouseup", e => clickUp());
  289. });
  290. window.addEventListener("resize", () => {
  291. updateSizes();
  292. })
  293. document.addEventListener("mousemove", (e) => {
  294. if (clicked) {
  295. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  296. clicked.dataset.x = position.x;
  297. clicked.dataset.y = position.y;
  298. updateEntityElement(entities[clicked.dataset.key], clicked);
  299. }
  300. });
  301. function updateWorldHeight() {
  302. const value = Math.max(1, document.querySelector("#options-height-value").value);
  303. const unit = document.querySelector("#options-height-unit").value;
  304. const oldHeight = config.height;
  305. config.height = math.unit(value + " " + unit)
  306. Object.entries(entities).forEach(([key, entity]) => {
  307. const element = document.querySelector("#entity-" + key);
  308. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  309. element.dataset.x = newPosition.x;
  310. element.dataset.y = newPosition.y;
  311. });
  312. updateSizes();
  313. }