forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add p/demo/svg Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> * chore: fixup * chore: fixup * chore: fixup * chore: add gno.mod Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --------- Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
Package svg is a minimalist SVG generation library for Gno. | ||
The svg package provides a simple and lightweight solution for programmatically generating SVG (Scalable Vector Graphics) markup in Gno. It allows you to create basic shapes like rectangles and circles, and output the generated SVG to a | ||
Example: | ||
import "gno.land/p/demo/svg"" | ||
func Foo() string { | ||
canvas := svg.Canvas{Width: 200, Height: 200} | ||
canvas.DrawRectangle(50, 50, 100, 100, "red") | ||
canvas.DrawCircle(100, 100, 50, "blue") | ||
return canvas.String() | ||
} | ||
*/ | ||
package svg // import "gno.land/p/demo/svg" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module gno.land/p/demo/svg | ||
|
||
require ( | ||
"gno.land/p/demo/ufmt" v0.0.0-latest | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package svg | ||
|
||
import "gno.land/p/demo/ufmt" | ||
|
||
type Canvas struct { | ||
Width int | ||
Height int | ||
Elems []Elem | ||
} | ||
|
||
type Elem interface{ String() string } | ||
|
||
func (c Canvas) String() string { | ||
output := "" | ||
output += ufmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d">`, c.Width, c.Height) | ||
for _, elem := range c.Elems { | ||
output += elem.String() | ||
} | ||
output += "</svg>" | ||
return output | ||
} | ||
|
||
func (c *Canvas) Append(elem Elem) { | ||
c.Elems = append(c.Elems, elem) | ||
} | ||
|
||
type Circle struct { | ||
CX int // center X | ||
CY int // center Y | ||
R int // radius | ||
Fill string | ||
} | ||
|
||
func (c Circle) String() string { | ||
return ufmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" fill="%s" />`, c.CX, c.CY, c.R, c.Fill) | ||
} | ||
|
||
func (c *Canvas) DrawCircle(cx, cy, r int, fill string) { | ||
c.Append(Circle{ | ||
CX: cx, | ||
CY: cy, | ||
R: r, | ||
Fill: fill, | ||
}) | ||
} | ||
|
||
type Rectangle struct { | ||
X, Y, Width, Height int | ||
Fill string | ||
} | ||
|
||
func (c Rectangle) String() string { | ||
return ufmt.Sprintf(`<rect x="%d" y="%d" width="%d" height="%d" fill="%s" />`, c.X, c.Y, c.Width, c.Height, c.Fill) | ||
} | ||
|
||
func (c *Canvas) DrawRectangle(x, y, width, height int, fill string) { | ||
c.Append(Rectangle{ | ||
X: x, | ||
Y: y, | ||
Width: width, | ||
Height: height, | ||
Fill: fill, | ||
}) | ||
} | ||
|
||
type Text struct { | ||
X, Y int | ||
Text, Fill string | ||
} | ||
|
||
func (c Text) String() string { | ||
return ufmt.Sprintf(`<text x="%d" y="%d" fill="%s">%s</text>`, c.X, c.Y, c.Fill, c.Text) | ||
} | ||
|
||
func (c *Canvas) DrawText(x, y int, text, fill string) { | ||
c.Append(Text{ | ||
X: x, | ||
Y: y, | ||
Text: text, | ||
Fill: fill, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// PKGPATH: gno.land/p/demo/svg_test | ||
package svg_test | ||
|
||
import "gno.land/p/demo/svg" | ||
|
||
func main() { | ||
canvas := svg.Canvas{Width: 500, Height: 500} | ||
canvas.DrawRectangle(50, 50, 100, 100, "red") | ||
canvas.DrawCircle(100, 100, 50, "blue") | ||
canvas.DrawText(100, 100, "hello world!", "magenta") | ||
println(canvas) | ||
} | ||
|
||
// Output: | ||
// <svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><rect x="50" y="50" width="100" height="100" fill="red" /><circle cx="100" cy="100" r="50" fill="blue" /><text x="100" y="100" fill="magenta">hello world!</text></svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// PKGPATH: gno.land/p/demo/svg_test | ||
package svg_test | ||
|
||
import "gno.land/p/demo/svg" | ||
|
||
func main() { | ||
canvas := svg.Canvas{ | ||
Width: 500, Height: 500, | ||
Elems: []svg.Elem{ | ||
svg.Rectangle{50, 50, 100, 100, "red"}, | ||
svg.Circle{50, 50, 100, "red"}, | ||
svg.Text{100, 100, "hello world!", "magenta"}, | ||
}, | ||
} | ||
println(canvas) | ||
} | ||
|
||
// Output: | ||
// <svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><rect x="50" y="50" width="100" height="100" fill="red" /><circle cx="50" cy="50" r="100" fill="red" /><text x="100" y="100" fill="magenta">hello world!</text></svg> |