v0 source pure
Package svg is a minimalist and extensible SVG generation library for Gno.
View source
gno.land/p/moul/svg/v0
Minimalist, extensible SVG generation library for Gno.
The canvas style map is backed by
gno.land/p/nt/bptree/v0: a B+ tree packs
many entries per persisted node, so styles cost less storage and inserts spend
less gas than an AVL backing would.
Compatibility caveats (from the in-place-mutating B+ tree backing):
- do not call
AddStylefrom inside aCanvas.String/Renderiteration; - do not copy a non-zero
Canvasby value once styles have been added — the copies would share live tree nodes.
Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.
Dependency graph:

⚠️ Disclaimer: provided as-is, without warranty; not security-audited. Full disclaimer: DISCLAIMER.
Package svg is a minimalist and extensible SVG generation library for Gno.
It is the B+ tree successor to gno.land/p/moul/svg/v0 (whose canvas styles are backed by an AVL tree): a bump to v2 because the backing data structure used for the canvas style map — and thus the on-chain storage layout — changed from gno.land/p/nt/avl/v0 to gno.land/p/nt/bptree/v0. The exported API is otherwise identical to v1.
A B+ tree packs many entries per persisted node, so a stored style entry costs less storage than the AVL backing (and inserts spend materially less gas). Prefer v2 when the canvas is part of persisted realm state.
Two behavioral caveats inherited from the in-place-mutating B+ tree backing:
- do NOT call AddStyle from inside a Canvas.String/Render iteration — the AVL backing tolerated mutation during iteration, the B+ tree one does not;
- do NOT copy a non-zero Canvas by value once styles have been added — the copies would share live B+ tree nodes (v1's AVL copies were independent).
It provides a structured way to create and compose SVG elements such as rectangles, circles, text, paths, and more. The package is designed to be modular and developer-friendly, enabling optional attributes and method chaining for ease of use.
Each SVG element embeds a BaseAttrs struct, which supports common SVG attributes like `id`, `class`, `style`, `fill`, `stroke`, and `transform`.
Canvas objects represent the root SVG container and support global dimensions, viewBox configuration, embedded styles, and element composition.
Example:
Example
1import "gno.land/p/moul/svg/v0"
2
3func Foo() string {
4 canvas := svg.NewCanvas(200, 200).WithViewBox(0, 0, 200, 200)
5 canvas.AddStyle(".my-rect", "stroke:black;stroke-width:2")
6 canvas.Append(
7 svg.NewRectangle(60, 40, 100, 50, "red").WithClass("my-rect"),
8 svg.NewCircle(50, 80, 40, "blue"),
9 &svg.Path{D: `M 10,30
10 A 20,20 0,0,1 50,30
11 A 20,20 0,0,1 90,30
12 Q 90,60 50,90
13 Q 10,60 10,30 z`, Fill: "magenta"},
14 svg.NewText(20, 50, "Hello SVG", "black"),
15 )
16 mysvg := canvas.Base64()
17}
9
func NewCircle
func NewEllipse
func NewGroup
func NewPath
func NewPolygon
func NewPolyline
func NewRectangle
func NewText
11
type BaseAttrs
structtype Canvas
structtype Circle
structtype Elem
interfacetype Ellipse
structtype Group
structtype Path
structtype Polygon
structtype Polyline
structtype Rectangle
structtype Text
struct4
- encoding/base64 stdlib
- gno.land/p/nt/bptree/v0 package
- gno.land/p/nt/ufmt/v0 package
- strings stdlib