canvas-view
A code editor view for CodeMirror 6 that draws
text with the GPU (WebGL2 or WebGPU) instead of the DOM, and owns its
scrolling with Android's fling physics. Every frame on screen is fully
drawn: no blank areas during fast flings, even on slow Android WebViews.
It runs on CodeMirror's own EditorState, languages, commands and UI
plugins (search, autocomplete, lint, tooltips, panels), so most
CodeMirror code works unchanged. It was built for
Acode, the Android code
editor, and is usable as a standalone library.
Experimental.
Why
DOM editors lay out and render text on the main thread. CodeMirror lets
the compositor scroll, so when the main thread is busy (a language
server, a slow CPU, many decorations) a fast fling reaches lines that
haven't been rendered yet. Ace and Monaco scroll from JavaScript and drop
frames instead.
canvas-view keeps text, selection, gutter and decorations in GPU buffers
it updates incrementally. A scroll frame only changes a few offsets, so
the picture always matches the scroll position, and CodeMirror's UI
still runs as real DOM over and around the canvas.
On a phone (Motorola Edge 40 Neo, Android 15, System WebView 156,
90Hz) with a 120,000-line JavaScript file, CPU slowed 6× to approximate
a low-end device (2026-11-16):
|
canvas-view |
CodeMirror 6 |
Ace |
Monaco |
| Fling frame p95 / worst |
11.2 / 11.2ms |
11.2 / 122.1ms |
55.5 / 77.6ms |
44.4 / 99.8ms |
| Frames with blank areas |
0 |
5 |
0 |
0 |
| Long tasks during flings and typing |
0 |
14 |
4 |
25 |
| Main-thread time per scroll frame |
6.4ms |
7.8ms |
41.6ms |
67.7ms |
| Typing latency p50 / p95 |
36 / 45ms |
63 / 80ms |
57 / 73ms |
55 / 72ms |
| JS heap |
11 MB |
11 MB |
25 MB |
41 MB |
11.1ms is a perfect frame at 90Hz. Reproduce with
node bench/compare.mjs --device (see Development).
Install
npm install @acode.app/canvas-view @codemirror/state @codemirror/view @codemirror/language
Your app must end up with a single copy each of @codemirror/state,
@codemirror/view, @codemirror/language and @lezer/common (1.5.4 or
later), as CodeMirror requires.
Usage
import {EditorState} from "@codemirror/state"
import {javascript} from "@codemirror/lang-javascript"
import {CanvasView, darkTheme} from "@acode.app/canvas-view"
const view = new CanvasView({
parent: document.querySelector("#editor")!,
state: EditorState.create({doc: "console.log('hi')", extensions: [javascript()]}),
theme: darkTheme,
font: {family: "monospace", size: 14},
lineWrapping: true,
stickyScroll: true
})
view.dispatch({changes: {from: 0, insert: "// hello\n"}})
view.reconfigure({rainbowBrackets: true})
To CodeMirror code, view behaves like an EditorView (state,
dispatch, coordsAtPos, posAtCoords, lineBlockAt, dom,
scrollDOM, plugin(), requestMeasure()...), and
view instanceof EditorView is true. @codemirror/commands and keymaps
run against it unchanged.
Features
- Rendering: WebGL2 (default) or WebGPU, recovering from GPU context
loss by itself. Unicode: CJK, emoji and ZWJ sequences, combining marks,
shaped Arabic, Hebrew, Indic and Thai words. Right-to-left text ordered
by the Unicode Bidi Algorithm, with
ltr, rtl and per-line auto
paragraphs.
- Syntax highlighting from any
@codemirror/language language,
parsed in time slices that never block a frame. Colors come from the
state's syntaxHighlighting(...) styles, so they match the DOM editor.
- Scrolling: Android
OverScroller flings, 1:1 drags, wheel,
adjustable speed and scroll-past-end.
- Layout: line wrapping with VS Code-like breaks and continuation
indent, folding, sticky scroll, inline widgets, block widgets and block
replaces (real DOM in rows of their own).
- Editing: EditContext input with IME composition (textarea
fallback), CodeMirror keymaps, history, closeBrackets, multiple cursors,
rectangular selection, text drag-and-drop, read-only modes.
- Touch: selection handles, long press, double and triple tap, edge
autoscroll, on-screen keyboard handling. The selection menu is yours,
driven by hooks.
- Drawn natively: line numbers (absolute or relative), fold markers,
active line, indent guides, rainbow brackets, whitespace markers, lint
underlines, search highlights, and gutter markers from CodeMirror's
gutter().
Options
| Option |
Default |
|
parent |
|
Element to append view.dom to |
state / doc / extensions |
|
The EditorState, or a doc and extensions to make one |
theme |
the state's themes |
A CanvasThemeSpec (see Theming); without one, the state's EditorView.theme colors, or darkTheme when it has none |
themeCSS |
|
CSS text resolved after the state's style modules |
font |
monospace, 14px |
{family, size, lineHeight}. Without lineHeight, rows are as tall as CodeMirror's (.cm-line CSS line height) |
fontLigatures |
from the CSS (on) |
Draw the font's ligatures (=>, !==) like DOM text; off with false, or with font-variant-ligatures: none in the editor's CSS |
backend |
"auto" (WebGL2) |
"webgl2" or "webgpu" (falls back to WebGL2) |
lineNumbers |
true |
false or "relative" |
onLineNumberClick |
fold toggle |
(line, event, view) => boolean; return true to handle it |
foldMarkers |
"fade" |
"always" or "none" |
stickyScroll |
false |
true, or {maxLines} (default 5) |
lineWrapping |
from EditorView.lineWrapping |
|
wrapIndent |
"same" |
"none", "indent", "deepIndent", or extra columns |
textDirection |
"ltr" |
"rtl", or "auto" (per line; default with EditorView.perLineTextDirection) |
indentGuides |
true |
false, or {active, hideOnBlankLines} |
rainbowBrackets |
false |
true, or {colors} |
bracketPairGuides |
false |
true (every pair spanning lines) or "active" (the cursor's): lines joining matching brackets, in their rainbow colors |
showWhitespace |
auto |
true, false, or {spaces, trailing, lineBreaks}; on with highlightWhitespace() |
gutterMarkers |
"shared" |
"columns" (one per gutter()) or "overlay" (over the line numbers) |
highlightActiveLine |
true |
|
cursorWidth |
2 |
CSS px |
cursorBlinkRate |
1200 |
ms, 0 to disable |
cursorStyle |
"line" |
"line-thin", "block", "block-outline", "underline", "underline-thin"; follows Vim's mode with @replit/codemirror-vim |
cursorBlinking |
"blink" |
"smooth", "phase", "expand" (animated) or "solid" |
cursorSmoothCaretAnimation |
"off" |
"on", or "explicit" (not while typing): the cursor slides to its new position |
smoothScrolling |
false |
Animate scrolling from keys and commands (PageDown, go to line, scroll into view) |
readOnlyCursor |
true |
Steady cursor in non-editable views |
placeholder |
|
Shown while the document is empty |
scrollSpeed |
"normal" |
"slow", "fast", "fastX2", or a multiplier |
scrollPastEnd |
true |
Or a fraction of the view height (0.25, 0.5, 1) |
horizontalScrollPastEnd |
0 |
Extra px of horizontal room |
tabSize |
from the state |
|
input |
"auto" |
"editcontext" or "textarea" |
touchSelection |
|
{handleSize, cursorHandleTimeout, onMenu, onHaptic, wordRange} |
nativeContextMenu |
true |
The browser's Cut/Copy/Paste/Select all menu on a mouse right-click; EditorView.domEventHandlers({contextmenu}) can replace it |
lowLatency |
false |
Request a desynchronized canvas |
dispatchTransactions |
|
Like CodeMirror's |
API
Besides the EditorView members:
|
|
view.reconfigure(options) |
Change display options live |
view.setState(state) |
Switch documents (editor tabs); keeps each document's parse, so switching back is instant |
view.setTheme(spec) / view.setFont(font) |
Change the theme or font |
view.touchSelection.openMenu() / hideMenu() |
For toolbar buttons that open your selection menu |
view.destroy() |
Release the GPU context and listeners. Browsers allow ~16 WebGL contexts per page, so destroy views you no longer show |
Theming
Without a theme option the view follows the state's CodeMirror themes,
like a DOM editor: background, text color, cursor, selection, active
line (a fill, or a frame from an inset box-shadow or an outline) and
gutter colors come from the EditorView.theme rules, and highlight,
decoration and gutter element classes from the same CSS. The rules are
resolved from the style modules themselves, with no hidden DOM. Rules
in a page stylesheet aren't seen; pass them as themeCSS (CSS text),
or use a canvas theme. view.setTheme(null) goes back to following the
CSS. A function as theme gets the CSS-resolved theme and returns the
one to use, and runs again when the state's themes change:
theme: css => ({...css, scrollbar: "transparent"}). cssThemeSpec(state)
and themeCSSOf(state) resolve a state's theme without a view.
import {oneDark} from "@codemirror/theme-one-dark"
new CanvasView({state: EditorState.create({doc, extensions: [oneDark]}), parent})
An explicit theme is used as given:
A CanvasThemeSpec has colors (background, foreground, selection,
cursor, activeLine, gutter colors, indentGuide,
stickyBackground...), dark, an optional highlight (a CodeMirror
HighlightStyle, used when the state has no syntaxHighlighting), and
classes, which give mark decorations a canvas style by class name.
darkTheme and lightTheme are exported. Keep your
EditorView.theme(...) extensions in the state too: they style the DOM
parts (tooltips, panels, autocomplete).
Decorations and widgets