packages/core/src/lib/hooks/use-canvas-context.ts
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import { GLOBAL_STATE } from '../global-state.js';
2 import type { RenderingContext2D } from '../types.js';
3
4 const PIBBL_CANVAS_CONTEXT_ERROR =
5 'useCanvasContext() is available only during Pibbl rendering and cannot be ' +
6 'used during pure measurement.';
7
8 /**
9 * Returns the current component's isolated Canvas 2D context.
10 *
11 * @returns The active Canvas 2D rendering context for this component evaluation. See
12 * {@link RenderingContext2D} .
13 *
14 * @see {@link RenderingContext2D}
15 */
16 export function useCanvasContext(): RenderingContext2D {
17 const context = GLOBAL_STATE.renderContext;
18 if (
19 !context ||
20 GLOBAL_STATE.backendDriver?.name !== 'canvas2d' ||
21 !GLOBAL_STATE.componentRefs ||
22 !GLOBAL_STATE.currentPibblInstance ||
23 !GLOBAL_STATE.canvas
24 ) {
25 throw new Error(PIBBL_CANVAS_CONTEXT_ERROR);
26 }
27 return context;
28 }
29
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision 2dccb19. ALPHA — NOT FOR PRODUCTION USE.