djledda.de main
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

136 linhas
3.1 KiB

  1. // @ts-check
  2. //
  3. import { css, DJElement, h, html, qs } from "./util";
  4. class HalfLifeGraph extends DJElement {
  5. static styles = css`
  6. .container {
  7. margin: auto;
  8. text-align: center;
  9. }
  10. canvas {
  11. border: solid 1px black;
  12. }
  13. `;
  14. static template = html`
  15. <div class="container">
  16. <div>
  17. <canvas />
  18. </div>
  19. <label for="my-input">Test Input</label>
  20. <input type="range" />
  21. </div>
  22. `;
  23. /** @type number */
  24. height = 480;
  25. /** @type number */
  26. width = 640;
  27. /** @type HTMLInputElement */
  28. input1;
  29. /** @type number */
  30. input1Value;
  31. /** @type Function */
  32. renderCb;
  33. constructor() {
  34. super();
  35. const canvas = /** @type HTMLCanvasElement */ (qs("canvas", this.root));
  36. this.input1 = /** @type HTMLInputElement */ (qs("input", this.root));
  37. canvas.width = this.width;
  38. canvas.height = this.height;
  39. const context = canvas.getContext("2d");
  40. if (!context) {
  41. throw new Error("No context available");
  42. }
  43. this.renderCb = () => {
  44. window.requestAnimationFrame(() => this.renderFrame(context));
  45. };
  46. this.input1.addEventListener("input", (e) => {
  47. this.input1Value = /** @type HTMLInputElement */ (e.target).valueAsNumber;
  48. this.renderCb();
  49. });
  50. this.renderCb();
  51. }
  52. /**
  53. * @param {CanvasRenderingContext2D} ctx
  54. */
  55. renderFrame(ctx) {
  56. this.drawAxes(ctx);
  57. this.drawGrid(ctx);
  58. this.drawCurve(ctx);
  59. }
  60. /**
  61. * @param {CanvasRenderingContext2D} ctx
  62. */
  63. drawAxes(ctx) {
  64. const startY = 0;
  65. const endY = 10;
  66. const startX = 0;
  67. const endX = 10;
  68. ctx.beginPath();
  69. ctx.moveTo(startX, this.height - startY);
  70. ctx.lineTo(startX, this.height - endY);
  71. ctx.stroke();
  72. ctx.beginPath();
  73. ctx.moveTo(startX, this.height - startY);
  74. ctx.lineTo(endX, this.height - startY);
  75. ctx.stroke();
  76. }
  77. /**
  78. * @param {CanvasRenderingContext2D} ctx
  79. */
  80. drawGrid(ctx) {
  81. /**
  82. const step = this.width / V0D
  83. for (let i = 0; i < thing; i++) {
  84. ctx.beginPath();
  85. ctx.moveTo(i, );
  86. ctx.lineTo(i);
  87. }
  88. */
  89. }
  90. /**
  91. * @param {CanvasRenderingContext2D} ctx
  92. */
  93. drawCurve(ctx) {
  94. // A = A_0 * (1/2) ^ t / h
  95. // h = half life
  96. // t = point in time
  97. // A_0 = initial
  98. // A = current concentration
  99. // TODO
  100. // https://pharmacy.ufl.edu/files/2013/01/5127-28-equations.pdf
  101. /**
  102. * @param {number} x
  103. */
  104. function y(x) {
  105. return x ** 2;
  106. }
  107. const xEnd = 2;
  108. const yEnd = 2;
  109. const samples = 100;
  110. const step = this.width / samples;
  111. ctx.beginPath();
  112. ctx.moveTo(0, this.height);
  113. for (let i = 0; i < this.width; i += step) {
  114. ctx.lineTo(i, this.height - y(i / this.width * xEnd) * this.height / yEnd);
  115. }
  116. ctx.stroke();
  117. }
  118. }
  119. customElements.define("half-life-graph", HalfLifeGraph);