@nativescript/canvas-svg
@nativescript/canvas-svg renders SVG natively on iOS, tvOS, visionOS, Android, Windows and Linux with Skia. Version 3.0 is a ground-up rewrite.
- Animation included. SMIL (
<animate>,<animateTransform>,<animateMotion>,<set>) and CSS@keyframesplay without any extra code. - A real DOM. When
@nativescript/canvas-polyfillis installed, you can build and change an SVG withdocument.createElementNS,setAttributeandappendChild, as in a browser. - GPU rasterization. Metal on Apple platforms, and Vulkan or GL on Android. Drawing happens off the UI thread, and the view falls back to the CPU automatically. Windows and Linux rasterize on the CPU.
- Works as a canvas image source. You can pass an SVG to
drawImage,createPattern,createImageBitmap,texImage2Dand WebGPU'scopyExternalImageToTexture.
Installation
bash
npm install @nativescript/canvas-svgIt needs the same NativeScript 9.1+ runtime as @nativescript/canvas.
Quick start
src accepts an app-relative path, an absolute path, a URL, or inline markup.
xml
<Page xmlns:svg="@nativescript/canvas-svg">
<svg:Svg src="~/assets/rocket.svg" width="100%" height="100%" />
</Page>ts
import { Svg } from '@nativescript/canvas-svg';
const view = new Svg();
view.src = '~/assets/rocket.svg';Any animations in the source start once the view is loaded, and pause while it is off screen.
Guides
- Properties and Events
- Styling
- Building SVG with the DOM
- Animation
- Drawing SVG into a Canvas
- Rendering and Performance
Limitations
- A document loaded from
srccannot be scripted. This is what allows views to share it. - For text elements in a document loaded from
src, you can only read or replace text that was added through the DOM. steps()easing runs as linear.- A SMIL
beginthat waits for an event (such asclick) never starts on its own.