:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(quill-charts): free the hover canvas on charts nobody hovers (#125075)
Matt Pua73983ff17c7c
diff --git a/packages/quill/packages/charts/src/core/hooks/clearCanvas.ts b/packages/quill/packages/charts/src/core/hooks/clearCanvas.tsindex 8f77b5531dbd..71f92d97e0de 100644--- a/packages/quill/packages/charts/src/core/hooks/clearCanvas.ts+++ b/packages/quill/packages/charts/src/core/hooks/clearCanvas.ts@@ -21,3 +21,15 @@ export function clearAndPrepare(ctx: CanvasRenderingContext2D, dimensions: Chartctx.setTransform(scaleX, 0, 0, scaleY, 0, 0)ctx.clearRect(0, 0, dimensions.width, dimensions.height)}++/** Free the backing store of a canvas that has nothing to show. A 0×0 canvas holds no bitmap and+ * gets no compositor layer, so idle hover overlays cost nothing. `clearAndPrepare` sizes it again+ * on the next draw. */+export function releaseCanvas(canvas: HTMLCanvasElement): void {+ if (canvas.width !== 0) {+ canvas.width = 0+ }+ if (canvas.height !== 0) {+ canvas.height = 0+ }+}diff --git a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsx b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsxindex 2b46753a9bd2..6bfd78d1fd2e 100644--- a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsx+++ b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsx@@ -1,4 +1,4 @@-import { act } from '@testing-library/react'+import { act, fireEvent } from '@testing-library/react'import { mockRect, renderChart } from '../../testing'import { Chart } from '../Chart'@@ -99,6 +99,29 @@ describe('useChartCanvas', () => {expect(drawStatic).toHaveBeenCalled()})+ it('holds a hover overlay bitmap only while the chart is hovered', () => {+ const { chart } = renderChart(+ <Chart+ series={SERIES}+ labels={LABELS}+ theme={THEME}+ createScales={createScales}+ drawStatic={() => {}}+ drawHover={() => true}+ />+ )+ const overlay = chart.element.querySelector<HTMLCanvasElement>('canvas[aria-hidden="true"]')!+ expect([overlay.width, overlay.height]).toEqual([0, 0])++ chart.hoverAtIndex(1)+ expect([overlay.width, overlay.height]).toEqual([mockRect.width, mockRect.height])++ act(() => {+ fireEvent.mouseLeave(chart.element)+ })+ expect([overlay.width, overlay.height]).toEqual([0, 0])+ })+it('repaints against a restored backing store after the 2D context is lost', () => {const drawStatic = jest.fn()const canvas = renderCanvas(drawStatic)diff --git a/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.ts b/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.tsindex 358130273d69..a8c6d345080a 100644--- a/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.ts+++ b/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.ts@@ -10,7 +10,7 @@ import type {DrawHoverResult,ResolvedSeries,} from '../types'-import { clearAndPrepare } from './clearCanvas'+import { clearAndPrepare, releaseCanvas } from './clearCanvas'import { useLatest } from './useLatest'interface UseHoverAnimationOptions {@@ -91,7 +91,16 @@ export function useHoverAnimation({}const elapsed = monotonicNow() - hoverAnimRef.current.startTimeconst hoverProgress = hoverAnimationMs > 0 ? Math.min(1, elapsed / hoverAnimationMs) : 1- clearAndPrepare(overlayCtx, dimensions)+ // Idle (no hover, no drag) draws nothing, so release the bitmap instead of clearing a+ // full-size one. drawHover still runs, against the empty canvas, so chart types can+ // reset their own hover state.+ const idle = hoverIndex < 0 && !dragRectRef.current+ if (idle) {+ releaseCanvas(overlayCtx.canvas)+ overlayCtx.save()+ } else {+ clearAndPrepare(overlayCtx, dimensions)+ }let drewVisible: DrawHoverResulttry {drewVisible = drawHoverRef.current({