* Grid Renderer Test Example * * This example demonstrates the grid renderer functionality with: * - Configurable horizontal and vertical line counts * - Proper coordinate transformation to clip space * - Real-time updates via interactive controls
()
| 13 | */ |
| 14 | |
| 15 | async function main() { |
| 16 | // Get canvas element |
| 17 | const canvas = document.getElementById('canvas') as HTMLCanvasElement; |
| 18 | if (!canvas) { |
| 19 | throw new Error('Canvas element not found'); |
| 20 | } |
| 21 | |
| 22 | // Get control elements |
| 23 | const horizontalSlider = document.getElementById('horizontal') as HTMLInputElement; |
| 24 | const verticalSlider = document.getElementById('vertical') as HTMLInputElement; |
| 25 | const horizontalValue = document.getElementById('horizontal-value') as HTMLSpanElement; |
| 26 | const verticalValue = document.getElementById('vertical-value') as HTMLSpanElement; |
| 27 | |
| 28 | if (!horizontalSlider || !verticalSlider || !horizontalValue || !verticalValue) { |
| 29 | throw new Error('Control elements not found'); |
| 30 | } |
| 31 | |
| 32 | let gpuContext: Awaited<ReturnType<typeof initializeGPUContext>> | null = null; |
| 33 | let animationFrameId: number | null = null; |
| 34 | let gridRenderer: ReturnType<typeof createGridRenderer> | null = null; |
| 35 | let xAxisRenderer: ReturnType<typeof createAxisRenderer> | null = null; |
| 36 | let yAxisRenderer: ReturnType<typeof createAxisRenderer> | null = null; |
| 37 | |
| 38 | // Track current line counts |
| 39 | let currentHorizontal = parseInt(horizontalSlider.value, 10); |
| 40 | let currentVertical = parseInt(verticalSlider.value, 10); |
| 41 | |
| 42 | try { |
| 43 | // Create and initialize GPU context |
| 44 | const ctx = createGPUContext(canvas); |
| 45 | gpuContext = await initializeGPUContext(ctx); |
| 46 | const device = gpuContext.device; |
| 47 | if (!device) { |
| 48 | throw new Error('WebGPU device not available after GPUContext initialization.'); |
| 49 | } |
| 50 | const gpuDevice: GPUDevice = device; |
| 51 | |
| 52 | // Create grid renderer (match pipeline format to configured canvas format) |
| 53 | gridRenderer = createGridRenderer(gpuDevice, { targetFormat: gpuContext.preferredFormat ?? 'bgra8unorm' }); |
| 54 | |
| 55 | // Calculate grid area (margins in CSS pixels) |
| 56 | // Note: devicePixelRatio is required by GridArea interface and is used for CSS-to-device pixel conversion |
| 57 | const gridArea: GridArea = { |
| 58 | left: 60, |
| 59 | right: 20, |
| 60 | top: 40, |
| 61 | bottom: 40, |
| 62 | canvasWidth: canvas.width, |
| 63 | canvasHeight: canvas.height, |
| 64 | devicePixelRatio: gpuContext.devicePixelRatio, |
| 65 | }; |
| 66 | |
| 67 | // Create clip-space scales for axis ticks. |
| 68 | // Note: scale.range() is clip-space [-1, 1] in this project. |
| 69 | const dpr = gridArea.devicePixelRatio; |
| 70 | const plotLeft = gridArea.left * dpr; |
| 71 | const plotRight = gridArea.canvasWidth - gridArea.right * dpr; |
| 72 | const plotTop = gridArea.top * dpr; |
no test coverage detected