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

Function prepare

src/renderers/createGridRenderer.ts:193–275  ·  view source on GitHub ↗
(gridArea, lineCountOrOptions)

Source from the content-addressed store, hash-verified

191 };
192
193 const prepare: GridRenderer['prepare'] = (gridArea, lineCountOrOptions) => {
194 assertNotDisposed();
195
196 const isOptionsObject =
197 lineCountOrOptions != null &&
198 typeof lineCountOrOptions === 'object' &&
199 ('lineCount' in lineCountOrOptions || 'color' in lineCountOrOptions);
200
201 const options: GridPrepareOptions | undefined = isOptionsObject
202 ? (lineCountOrOptions as GridPrepareOptions)
203 : undefined;
204
205 const lineCount: GridLineCount | undefined = isOptionsObject
206 ? options?.lineCount
207 : (lineCountOrOptions as GridLineCount | undefined);
208
209 const horizontal = lineCount?.horizontal ?? DEFAULT_HORIZONTAL_LINES;
210 const vertical = lineCount?.vertical ?? DEFAULT_VERTICAL_LINES;
211 const colorString = options?.color ?? DEFAULT_GRID_COLOR;
212
213 // Validate inputs
214 if (horizontal < 0 || vertical < 0) {
215 throw new Error('GridRenderer.prepare: line counts must be non-negative.');
216 }
217 if (
218 !Number.isFinite(gridArea.left) ||
219 !Number.isFinite(gridArea.right) ||
220 !Number.isFinite(gridArea.top) ||
221 !Number.isFinite(gridArea.bottom) ||
222 !Number.isFinite(gridArea.canvasWidth) ||
223 !Number.isFinite(gridArea.canvasHeight)
224 ) {
225 throw new Error('GridRenderer.prepare: gridArea dimensions must be finite numbers.');
226 }
227 if (gridArea.canvasWidth <= 0 || gridArea.canvasHeight <= 0) {
228 throw new Error('GridRenderer.prepare: canvas dimensions must be positive.');
229 }
230
231 // Early return if no lines to draw
232 if (horizontal === 0 && vertical === 0) {
233 vertexCount = 0;
234 return;
235 }
236
237 // Generate vertices
238 const vertices = generateGridVertices(gridArea, horizontal, vertical);
239 const requiredSize = vertices.byteLength;
240
241 // Ensure minimum buffer size of 4 bytes
242 const bufferSize = Math.max(4, requiredSize);
243
244 // Create or recreate vertex buffer if needed
245 if (!vertexBuffer || vertexBuffer.size < bufferSize) {
246 if (vertexBuffer) {
247 try {
248 vertexBuffer.destroy();
249 } catch {
250 // best-effort

Callers

nothing calls this directly

Calls 6

writeUniformBufferFunction · 0.90
parseCssColorToRgba01Function · 0.90
generateGridVerticesFunction · 0.85
assertNotDisposedFunction · 0.70
createIdentityMat4BufferFunction · 0.70
destroyMethod · 0.45

Tested by

no test coverage detected