Share
Embed
Transcript
23 slides
-
Canvas 2D Context Next JSConf JP 2026 pixiv Inc. 森内建太
@petamoriken 2026.11.22 -
Fake: satori (SVG 1.1) Real: Puppeteer (HTML/CSS) SVG 1.1 does
not support -webkit-line-clamp! 5 -
Image rendering in server-side web stacks SVG 1.1 (satori) HTML/CSS
(Puppeteer) Canvas 2D Declarative Declarative ⚠ Imperative ⚠ Text Expressiveness ⚠ ✅ ✅ Rendering Speed ✅ ❌ ✅ Memory Consumption ✅ ❌ ✅ Paradigm 7 -
Canvas 2D • Context creation via getContext("2d") on HTMLCanvasElement (<canvas>)
or OffscreenCanvas • Supported in non-browser runtimes using libraries like node-canvas or skia-canvas • Native non-browser runtime support (OffscreenCanvas) ◦ Deno: WIP by me ◦ Andromeda: supported 8 -
New Canvas 2D Features Discovered While Implementing in Deno (Not
Covering HTML-in-Canvas) 9 -
Self-Introduction petamoriken pixiv WebDev engineer in Fukuoka 10 • A
Deno contributor • An advocate for ES2025 Float16Array • I like to keep up with Web Standards -
Table of Contents • Canvas Text Proposals ◦ Enhanced Text
Metrics ◦ Text Shaping • High Dynamic Range (HDR) Color • Other Proposals 11 -
Measure width of each character const text = "じぇーえすかんふ"; //
Measure width per Grapheme Cluster for (const { segment } of new Intl.Segmenter().segment(text)) { const textMetrics = ctx.measureText(segment); console.log(textMetrics.width); } • Limitations of Grapheme Clusters: ◦ Ligatures ◦ Contextual shaping (e.g., Arabic, Devanagari) 13 -
Proposal: Enhanced Text Metrics (2021~) const text = "じぇーえすかんふ"; const
textMetrics = ctx.measureText(text); // Measure width position per Glyph Cluster at once for (const { begin, end } of textMetrics.getTextClusters()) { const rect = textMetrics.getActualBoundingBox(begin, end); console.log(rect.width); } • Planned to ship in Chrome 160 (2027-01-25) ◦ But... 14 -
Proposal: Text Shaping (2026) const text = "じぇーえすかんふ"; const shapedText
= ctx.shapeText(text); // Measure width position per Glyph Cluster at once for (const { begin, end } of shapedText.glyphClusters()) { const rect = shapedText.getActualBoundingBox(begin, end); console.log(rect.width); } • Create Shaped Text, not Enhanced Text Metrics • WebKit spotted preparing a proposal (last week) 15 -
Proposal: Text Shaping (2026) [Exposed=(Window,Worker)] interface ShapedText { readonly attribute
double width; sequence<GlyphRun> glyphRuns(); sequence<GlyphRun> glyphClusters(); sequence<GlyphRun> glyphClusters([EnforceRange] unsigned long start, [EnforceRange] unsigned long end); sequence<DOMRectReadOnly> selectionRects([EnforceRange] unsigned long start, [EnforceRange] unsigned long end); DOMRectReadOnly actualBoundingBox([EnforceRange] unsigned long start, [EnforceRange] unsigned long end); unsigned long advanceToPosition(double advance); double positionToAdvance([EnforceRange] unsigned long position); }; 16 -
Color Types: unorm8 vs float16 unorm8 (Uint8ClampedArray) 0 255 sRGB
255 Display P3 float16 (Float16Array) 0.0 sRGB Display P3 19 1.0 1.0 -
Canvas Floating Point Color Values const ctx = canvas.getContext("2d", {
colorSpace: "display-p3", colorType: "float16", }); const imageData = ctx.getImageData(0, 0, 1, 1, { pixelFormat: "rgba-float16", }); console.log(imageData.data); // Float16Array 20 -
Do you have any questions? • Canvas Text Proposals ◦
Enhanced Text Metrics ◦ Text Shaping • High Dynamic Range (HDR) Color • Other Proposals 23