less copy protection, more size visualization
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

309 строки
8.7 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. }
  175. function select(target) {
  176. deselect();
  177. selected = target;
  178. selectedEntity = entities[target.dataset.key];
  179. selected.classList.add("selected");
  180. entityInfo(selectedEntity, target.dataset.view);
  181. }
  182. function entityInfo(entity, view) {
  183. document.querySelector("#entity-name").innerText = "Name: " + entity.name;
  184. document.querySelector("#entity-author").innerText = "Author: " + entity.author;
  185. document.querySelector("#entity-height").innerText = "Height: " + entity.views[view].height.format({ precision: 3 });
  186. }
  187. function displayEntity(entity, view, x, y) {
  188. const location = entity.location;
  189. const img = document.createElement("img");
  190. img.src = "./pepper.png"
  191. img.classList.add("entity");
  192. img.dataset.x = x;
  193. img.dataset.y = y;
  194. img.addEventListener("mousedown", e => {clickDown(e); e.stopPropagation()});
  195. img.id = "entity-" + entityIndex;
  196. img.dataset.key = entityIndex;
  197. img.dataset.view = view;
  198. entities[entityIndex] = entity;
  199. entityIndex += 1;
  200. const world = document.querySelector("#entities");
  201. world.appendChild(img);
  202. updateEntityElement(entity, img);
  203. }
  204. document.addEventListener("DOMContentLoaded", () => {
  205. for (let x = 0; x < 5; x++) {
  206. const entity = makeEntity();
  207. entity.name = "Green is my pepper";
  208. entity.author = "Fen"
  209. const x = 0.25 + Math.random() * 0.5;
  210. const y = 0.25 + Math.random() * 0.5;
  211. displayEntity(entity, "body", x, y);
  212. }
  213. updateSizes();
  214. document.querySelector("#options-height-value").addEventListener("input", e => {
  215. updateWorldHeight();
  216. })
  217. document.querySelector("#options-height-unit").addEventListener("input", e => {
  218. updateWorldHeight();
  219. })
  220. world.addEventListener("mousedown", e => deselect());
  221. document.addEventListener("mouseup", e => clickUp());
  222. });
  223. window.addEventListener("resize", () => {
  224. updateSizes();
  225. })
  226. document.addEventListener("mousemove", (e) => {
  227. if (clicked) {
  228. const position = snapRel(abs2rel({x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY}));
  229. clicked.dataset.x = position.x;
  230. clicked.dataset.y = position.y;
  231. updateEntityElement(entities[clicked.dataset.key], clicked);
  232. }
  233. });
  234. function updateWorldHeight() {
  235. const value = Math.max(1, document.querySelector("#options-height-value").value);
  236. const unit = document.querySelector("#options-height-unit").value;
  237. const oldHeight = config.height;
  238. config.height = math.unit(value + " " + unit)
  239. Object.entries(entities).forEach(([key, entity]) => {
  240. const element = document.querySelector("#entity-" + key);
  241. const newPosition = adjustAbs({x: element.dataset.x, y: element.dataset.y}, oldHeight, config.height);
  242. element.dataset.x = newPosition.x;
  243. element.dataset.y = newPosition.y;
  244. });
  245. updateSizes();
  246. }