Welcome to Part 3 of our series, "Rendering 100k Rows: How Far Can the DOM Go?"
In Part 2, we explored DOM Virtualization, a technique that recycles HTML nodes to render thousands of items smoothly. This works wonders for single-column lists or simple tables, but what happens when your data grows in both dimensions?
Imagine building a massive spreadsheet with 100 columns and 100,000 rows (10 million total cells). If the user scrolls diagonally or resizes columns, even a virtualized grid must coordinate thousands of active DOM operations in real time, leading to noticeable frame drops and lag.
To cross this multi-dimensional performance barrier, high-performance tools like Google Sheets, Airtable, and Figma abandon HTML nodes entirely. Instead, they draw their interfaces on an HTML5 Canvas.
Why Canvas? The Power of a Single DOM Node
An HTML5 Canvas is essentially a blank graphic image container. No matter how complex your grid is, the browser's DOM tree only sees one single element:
<canvas id="grid-canvas"></canvas>
Instead of letting the browser calculate layouts and paint elements automatically, we write JavaScript instructions to paint pixels directly onto this 2D graphic context using the Canvas API (ctx.fillText, ctx.fillRect, ctx.strokeRect).
Since there are no DOM nodes to update, style recalculations, reflows, and layout cycles drop to zero. The rendering speed is limited only by how fast your GPU can paint pixels.
Visualizing Coordinate Math: Hover Event Mapping
Because a Canvas is just a flat image, it has no native concept of cells, click handlers, or hover states.
If a user clicks the canvas, the browser only provides the pixel coordinates (e.g., clientX = 220, clientY = 65). We must write the math to map those pixels back to our database row and column indexes.
Hover your cursor over the visualizer below to see how coordinate mapping calculates the active row and column in real time:
- Row =
Math.floor(MouseY / RowHeight) - Column =
Math.floor(MouseX / ColumnWidth)
Interactive Demo: Simple Canvas Grid
Below is a working implementation of a table rendering 10,000 records on a Canvas.
Open the editor (click Edit Code), and view how the scrolling and drawing cycle performs at 60fps using requestAnimationFrame.
The Canvas Trade-off: Performance vs. Complexity
While Canvas offers absolute rendering performance, it is a double-edged sword. By bypassing the DOM, you lose basic features that the browser normally handles for free:
- Text Overflow & Wrapping: Canvas does not wrap text. If a cell contains a long sentence, you must write JS logic to measure the text width (
ctx.measureText) and break it into substrings. - Scroll Offsets & Events: You must build and handle scrollbars yourself by listening to mouse wheel events (
wheel) and touch scroll gestures, calculating drawing offsets, and manually triggering redraw frames. - Accessibility (a11y): Screen readers cannot read pixels painted on a canvas. You must construct a hidden, overlaying HTML table containing ARIA labels that duplicates the data structure so assistive tools can navigate.
- No Native Selection or Copy-Paste: Users cannot click and drag to highlight text or use
Ctrl+C/Cmd+Cto copy text from cells. To support copy-paste, you must track selection coordinates in JS and manually write text to the Clipboard API. - Goodbye, CSS Styling: You lose hover effects, focus rings, border-radius, fonts, and transitions. All visual styles, spacing, and hover cues must be rendered manually using imperative canvas commands in JavaScript.
- SEO & Indexability: Search engines cannot parse or index the data rendered inside a Canvas. It is completely invisible to SEO.
- High-DPI / Retina Blurriness: A basic Canvas context ignores high-resolution screen scales, resulting in blurry text and pixelated lines. Developers must scale the Canvas dimensions manually using
window.devicePixelRatioand apply a scale transform.
Summary: When to choose Canvas
HTML5 Canvas Grids are the ultimate solution when you need:
- Sub-millisecond rendering loops for high-frequency or real-time data feeds.
- Dense spreadsheets containing millions of individual cells.
- Advanced graphic layouts (like nested hierarchies or canvas-drawn graphics).
If you are only building a standard table with a few hundred columns and don't need real-time formula computation or cell pixel selections, virtualization will offer a simpler design workflow.
In Part 4, we will conclude the series by building a complete benchmark shootout comparing Pagination, DOM Virtualization, and Canvas Grids side-by-side. Stay tuned!