traceView.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {mat3} from 'gl-matrix';
  2. // Computes the transformation matrix that is used to render scaled
  3. // elements to the DOM and draw the view.
  4. export class TraceView {
  5. public x: number;
  6. public y: number;
  7. public width: number;
  8. public height: number;
  9. constructor(x: number, y: number, width: number, height: number) {
  10. this.x = x;
  11. this.y = y;
  12. this.width = width;
  13. this.height = height;
  14. }
  15. static From(view: TraceView): TraceView {
  16. return new TraceView(view.x, view.y, view.width, view.height);
  17. }
  18. static Empty(): TraceView {
  19. return new TraceView(0, 0, 1000, 1);
  20. }
  21. serialize() {
  22. return [this.x, this.y, this.width, this.height];
  23. }
  24. between(to: TraceView): mat3 {
  25. return mat3.fromValues(
  26. to.width / this.width,
  27. 0,
  28. 0,
  29. to.height / this.height,
  30. 0,
  31. 0,
  32. to.x - this.x * (to.width / this.width),
  33. to.y - this.y * (to.height / this.height),
  34. 1
  35. );
  36. }
  37. transform(mat: mat3): [number, number, number, number] {
  38. const x = this.x * mat[0] + this.y * mat[3] + mat[6];
  39. const y = this.x * mat[1] + this.y * mat[4] + mat[7];
  40. const width = this.width * mat[0] + this.height * mat[3];
  41. const height = this.width * mat[1] + this.height * mat[4];
  42. return [x, y, width, height];
  43. }
  44. get center() {
  45. return this.x + this.width / 2;
  46. }
  47. get left() {
  48. return this.x;
  49. }
  50. get right() {
  51. return this.x + this.width;
  52. }
  53. get top() {
  54. return this.y;
  55. }
  56. get bottom() {
  57. return this.y + this.height;
  58. }
  59. }