Canvas and SVG
The 2D canvas context and how SVG is rasterized in Aperture UI, with what each one supports and where the limits are.
Aperture UI supports both <canvas> (the 2D context) and <svg> (inline and external). They serve different purposes: canvas for dynamic per-pixel drawing, SVG for vector graphics.
Both end the same way in the engine. Pixels are produced on the CPU into an RGBA surface, and the surface is uploaded to the GPU as a texture whenever it changes. The element is then painted like an image. Neither is a live GPU drawing surface. The game owns the GPU, so getContext("webgl"), "webgl2" and "webgpu" return null.
2D Canvas
canvas.getContext("2d") returns a CanvasRenderingContext2D. The engine rasterizes paths with its own analytic scanline rasterizer and follows the HTML drawing model: coverage, source color, shadow, compositing, then clipping. This is not a complete browser canvas implementation, so check the members you need against the tables below.
<canvas id="radar" width="240" height="240"></canvas>const canvas = document.getElementById('radar');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#0a0a1f';
ctx.fillRect(0, 0, 240, 240);
ctx.strokeStyle = '#5b8cff';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(120, 120, 100, 0, Math.PI * 2);
ctx.stroke();
ctx.fillStyle = '#ffaa00';
ctx.beginPath();
ctx.arc(120, 60, 4, 0, Math.PI * 2);
ctx.fill();Supported operations
| Category | Operations |
|---|---|
| State | save, restore, reset |
| Shapes | fillRect, strokeRect, clearRect |
| Paths | beginPath, closePath, moveTo, lineTo, quadraticCurveTo, bezierCurveTo, arcTo, arc, ellipse, rect, roundRect, fill, stroke, isPointInPath, isPointInStroke |
| Clipping | clip, with nonzero and even-odd fill rules |
| Text | font, textAlign, textBaseline, direction, fillText, strokeText, measureText |
| Images and pixels | drawImage (3-, 5- and 9-argument forms), imageSmoothingEnabled, createImageData, getImageData, putImageData |
| Styles | fillStyle and strokeStyle as a CSS color, lineWidth |
| Gradients and patterns | createLinearGradient, createRadialGradient, createPattern (with repeat, repeat-x, repeat-y, no-repeat) |
| Lines | lineCap, lineJoin, miterLimit, setLineDash, getLineDash, lineDashOffset |
| Transforms | translate, rotate, scale, transform, setTransform, getTransform, resetTransform |
| Compositing | globalAlpha, globalCompositeOperation |
| Shadows | shadowColor, shadowBlur, shadowOffsetX, shadowOffsetY |
Canvas text is drawn with the engine's own font stack, so it uses the same fonts as the rest of the page (see Fonts).
drawImage and createPattern take an <img> or a <canvas> as the source. An <img> that has not finished decoding draws nothing, so wait for it to load first. A <video> is not accepted as a source.
Not in the engine: createConicGradient, filter, imageSmoothingQuality, the ctx.canvas back-reference, Path2D, OffscreenCanvas, createImageBitmap, and canvas.toDataURL() and toBlob(). To read pixels back, use getImageData.
document.createElement("canvas").getContext("2d") works before the canvas is in the document, as chart libraries expect when they measure text. What you draw is kept when the canvas is attached. A test page covers this.
Canvas as replaced content
<canvas> is painted as ordinary replaced content, the same way as an image, video or SVG. It does not get a compositing layer of its own. It only gets a dedicated off-screen layer if it also has fractional opacity, a filter, a blend mode or a mask applied. For UI minimaps, custom radar overlays or charts, one canvas is fine. For dozens of small dynamic graphics, consider drawing them into a single canvas instead.
Performance
- A canvas is a CPU surface. Drawing cost grows with the pixels you touch, and a canvas that changes every frame is uploaded to the GPU every frame, so keep canvases small or redraw them only when their data changes.
clearRectover the area you redraw is the way to start a frame. The canvas does not clear itself between frames.getImageDatacopies pixels out of the CPU surface, and the cost scales with the area you request. Measure it before you call it each frame.- Each
<canvas>is its own surface and its own texture upload, so prefer a few larger drawing areas to many tiny ones. - For animated content, redraw only what changed.
SVG
SVG in the engine goes through lunasvg, which rasterizes a document into a bitmap. The same path serves four kinds of source:
- an inline
<svg>element, <img src="icon.svg">,- a CSS image such as
background-image: url(icon.svg), a mask or aborder-image, - a
data:image/svg+xmlURI.
Inline SVG
<svg width="32" height="32" viewBox="0 0 32 32">
<circle cx="16" cy="16" r="14" fill="#5b8cff" />
<path d="M10 16l4 4 8-8" stroke="white" stroke-width="2" fill="none" />
</svg>Inline SVG is part of the DOM, so you can query and mutate it from JavaScript:
const checkmark = document.querySelector('svg path');
checkmark.setAttribute('stroke', 'lime');For an inline <svg> the engine serializes the subtree from the DOM with the cascade applied. A stylesheet rule that sets fill or stroke on SVG elements applies, currentColor follows the element's computed color, and <use> and url(#id) references to other <svg> elements in the document resolve. The whole <svg> is one raster, so a script can change attributes and the SVG is rasterized again, but a change redraws the entire image.
External SVG
<img src="icons/menu.svg" alt="Menu" width="24" height="24">Or as a CSS background:
.menu-button {
background-image: url('icons/menu.svg');
background-size: 24px 24px;
}In an external SVG file, a <style> element applies rules with simple selectors.
Supported elements
| Element | Status |
|---|---|
<svg>, <g>, <defs>, <symbol>, <use> | Supported |
<rect>, <circle>, <ellipse>, <line>, <polyline>, <polygon> | Supported |
<path> | Supported |
<linearGradient>, <radialGradient>, <stop> | Supported |
<pattern>, <mask>, <clipPath>, <marker> | Supported |
<text>, <tspan> | Supported. The engine shapes them with its own font stack and draws them over the raster. |
<image>, <textPath> | Not rendered |
<filter> and the fe* primitives | Not rendered |
<animate>, <animateTransform> | Not rendered. Use CSS or script on the element that contains the SVG, or Lottie. |
<foreignObject> | Not rendered |
An element the rasterizer does not know is dropped, and the rest of the image still draws.
SVG DOM
SVG elements are DOM elements, so querySelector, setAttribute and classList work on them. A subset of the SVG interfaces is bound for programmatic manipulation: SVGElement, SVGGraphicsElement, SVGSVGElement and SVGTextElement. Element-specific interfaces like SVGPathElement or SVGCircleElement are not implemented, and the geometry methods are not there: there is no getBBox(), getTotalLength() or getPointAtLength().
Performance
- Identical small SVG icons share one raster in a shared atlas texture, so a screen of repeated icons batches into a few draws. Large SVGs get a surface of their own. A test page covers repeated icons at two sizes.
- An SVG used as a CSS image is rasterized with extra resolution so that it scales crisply.
- Prefer
<img src>or a CSS background for an icon that never changes, over an inline<svg>that a script keeps editing.
Choosing Between Canvas, SVG, and CSS
| Use case | Best choice |
|---|---|
| Static icons | SVG, as an image or a CSS background |
| Static illustrations | SVG |
| Animated icons | CSS on an element, or Lottie |
| Per-pixel dynamic content (minimap, radar, charts) | Canvas |
| UI shapes and gradients (rounded buttons, progress bars) | CSS, the cheapest path |
| Particle effects | Canvas, or composite GPU effects from your engine |
| Hand-drawn illustrations | SVG, or a pre-rasterized PNG |
For pure UI work (buttons, panels, progress bars) CSS is cheaper than either canvas or SVG. Reach for canvas only when you need to draw something that CSS cannot express.