Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

doc.gno

2.09 Kb · 47 lines
 1/*
 2Package svg is a minimalist and extensible SVG generation library for Gno.
 3
 4It is the B+ tree successor to [gno.land/p/moul/svg/v0] (whose canvas styles are
 5backed by an AVL tree): a bump to v2 because the backing data structure used for
 6the canvas style map — and thus the on-chain storage layout — changed from
 7[gno.land/p/nt/avl/v0] to [gno.land/p/nt/bptree/v0]. The exported API is
 8otherwise identical to v1.
 9
10A B+ tree packs many entries per persisted node, so a stored style entry costs
11less storage than the AVL backing (and inserts spend materially less gas).
12Prefer v2 when the canvas is part of persisted realm state.
13
14Two behavioral caveats inherited from the in-place-mutating B+ tree backing:
15
16  - do NOT call AddStyle from inside a Canvas.String/Render iteration — the AVL
17    backing tolerated mutation during iteration, the B+ tree one does not;
18  - do NOT copy a non-zero Canvas by value once styles have been added — the
19    copies would share live B+ tree nodes (v1's AVL copies were independent).
20
21It 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.
22
23Each SVG element embeds a BaseAttrs struct, which supports common SVG attributes like `id`, `class`, `style`, `fill`, `stroke`, and `transform`.
24
25Canvas objects represent the root SVG container and support global dimensions, viewBox configuration, embedded styles, and element composition.
26
27Example:
28
29	import "gno.land/p/moul/svg/v0"
30
31	func Foo() string {
32		canvas := svg.NewCanvas(200, 200).WithViewBox(0, 0, 200, 200)
33		canvas.AddStyle(".my-rect", "stroke:black;stroke-width:2")
34		canvas.Append(
35			svg.NewRectangle(60, 40, 100, 50, "red").WithClass("my-rect"),
36			svg.NewCircle(50, 80, 40, "blue"),
37			&svg.Path{D: `M 10,30
38			A 20,20 0,0,1 50,30
39				A 20,20 0,0,1  90,30
40				Q 90,60 50,90
41				Q 10,60 10,30 z`, Fill: "magenta"},
42			svg.NewText(20, 50, "Hello SVG", "black"),
43		)
44		mysvg := canvas.Base64()
45	}
46*/
47package svg // import "gno.land/p/moul/svg/v0"