MCPcopy Index your code
hub / github.com/ChartGPU/ChartGPU / getCanvasDimensions

Function getCanvasDimensions

src/core/GPUContext.ts:45–77  ·  view source on GitHub ↗

Gets display dimensions - clientWidth/Height for HTMLCanvasElement, width/height for OffscreenCanvas.

(canvas: SupportedCanvas)

Source from the content-addressed store, hash-verified

43
44/** Gets display dimensions - clientWidth/Height for HTMLCanvasElement, width/height for OffscreenCanvas. */
45function getCanvasDimensions(canvas: SupportedCanvas): { width: number; height: number } {
46 if (isHTMLCanvasElement(canvas)) {
47 // Prefer clientWidth/clientHeight (CSS pixels) for HTMLCanvasElement as they reflect actual display size
48 // Fall back to canvas.width/height (device pixels) if client dimensions are 0 or invalid
49 const width = canvas.clientWidth || canvas.width || 0;
50 const height = canvas.clientHeight || canvas.height || 0;
51
52 // Validate dimensions are finite and non-negative
53 // Note: 0 dimensions are allowed here - they'll be clamped to 1 during GPUContext initialization
54 if (!Number.isFinite(width) || !Number.isFinite(height)) {
55 throw new Error(
56 `GPUContext: Invalid canvas dimensions detected: width=${canvas.clientWidth || canvas.width}, ` +
57 `height=${canvas.clientHeight || canvas.height}. ` +
58 `Canvas must have finite dimensions. Ensure canvas is properly sized before initialization.`
59 );
60 }
61
62 return { width, height };
63 }
64 // OffscreenCanvas: dimensions already set by main thread (device pixels)
65 const width = canvas.width;
66 const height = canvas.height;
67
68 // Validate OffscreenCanvas dimensions
69 if (!Number.isFinite(width) || !Number.isFinite(height)) {
70 throw new Error(
71 `GPUContext: Invalid OffscreenCanvas dimensions: width=${width}, height=${height}. ` +
72 `OffscreenCanvas must be initialized with finite dimensions before GPUContext creation.`
73 );
74 }
75
76 return { width, height };
77}
78
79/**
80 * Creates a new GPUContext state with initial values.

Callers 1

initializeGPUContextFunction · 0.85

Calls 1

isHTMLCanvasElementFunction · 0.85

Tested by

no test coverage detected