selectedFrameRenderer.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {mat3} from 'gl-matrix';
  2. import {getContext, Rect} from '../gl/utils';
  3. class SelectedFrameRenderer {
  4. canvas: HTMLCanvasElement;
  5. context: CanvasRenderingContext2D;
  6. constructor(canvas: HTMLCanvasElement) {
  7. this.canvas = canvas;
  8. this.context = getContext(canvas, '2d');
  9. }
  10. // We allow for passing of different contexts, this allows us to use a
  11. // single instance of the renderer to draw overlays on multiple canvases
  12. draw(
  13. frame: Rect,
  14. style: {BORDER_COLOR: string; BORDER_WIDTH: number},
  15. configViewToPhysicalSpace: mat3,
  16. context: CanvasRenderingContext2D = this.context
  17. ): void {
  18. const frameInPhysicalSpace = frame.transformRect(configViewToPhysicalSpace);
  19. // We draw the border in the center of the flamegraph, so we need to increase
  20. // the width by border width and negatively offset it by half the border width
  21. const borderRect = frameInPhysicalSpace
  22. .withWidth(frameInPhysicalSpace.width - style.BORDER_WIDTH)
  23. .withHeight(frameInPhysicalSpace.height - style.BORDER_WIDTH)
  24. .translate(
  25. frameInPhysicalSpace.x + style.BORDER_WIDTH / 2,
  26. frameInPhysicalSpace.y + style.BORDER_WIDTH / 2
  27. );
  28. context.beginPath();
  29. context.strokeStyle = style.BORDER_COLOR;
  30. context.lineWidth = style.BORDER_WIDTH;
  31. context.strokeRect(borderRect.x, borderRect.y, borderRect.width, borderRect.height);
  32. }
  33. }
  34. export {SelectedFrameRenderer};