selectedFrameRenderer.tsx 1.4 KB

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