selectedFrameRenderer.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. frames: Rect[],
  14. style: {BORDER_COLOR: string; BORDER_WIDTH: number},
  15. configViewToPhysicalSpace: mat3,
  16. context: CanvasRenderingContext2D = this.context
  17. ): void {
  18. context.strokeStyle = style.BORDER_COLOR;
  19. context.lineWidth = style.BORDER_WIDTH;
  20. for (let i = 0; i < frames.length; i++) {
  21. const frameInPhysicalSpace = frames[i].transformRect(configViewToPhysicalSpace);
  22. // We draw the border in the center of the flamegraph, so we need to increase
  23. // the width by border width and negatively offset it by half the border width
  24. const borderRect = frameInPhysicalSpace
  25. .withWidth(frameInPhysicalSpace.width - style.BORDER_WIDTH)
  26. .withHeight(frameInPhysicalSpace.height - style.BORDER_WIDTH)
  27. .translate(
  28. frameInPhysicalSpace.x + style.BORDER_WIDTH / 2,
  29. frameInPhysicalSpace.y + style.BORDER_WIDTH / 2
  30. );
  31. context.beginPath();
  32. context.strokeRect(borderRect.x, borderRect.y, borderRect.width, borderRect.height);
  33. }
  34. }
  35. }
  36. export {SelectedFrameRenderer};