github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/svg/svg.gno (about) 1 package svg 2 3 import "gno.land/p/demo/ufmt" 4 5 type Canvas struct { 6 Width int 7 Height int 8 Elems []Elem 9 } 10 11 type Elem interface{ String() string } 12 13 func (c Canvas) String() string { 14 output := "" 15 output += ufmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d">`, c.Width, c.Height) 16 for _, elem := range c.Elems { 17 output += elem.String() 18 } 19 output += "</svg>" 20 return output 21 } 22 23 func (c *Canvas) Append(elem Elem) { 24 c.Elems = append(c.Elems, elem) 25 } 26 27 type Circle struct { 28 CX int // center X 29 CY int // center Y 30 R int // radius 31 Fill string 32 } 33 34 func (c Circle) String() string { 35 return ufmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" fill="%s" />`, c.CX, c.CY, c.R, c.Fill) 36 } 37 38 func (c *Canvas) DrawCircle(cx, cy, r int, fill string) { 39 c.Append(Circle{ 40 CX: cx, 41 CY: cy, 42 R: r, 43 Fill: fill, 44 }) 45 } 46 47 type Rectangle struct { 48 X, Y, Width, Height int 49 Fill string 50 } 51 52 func (c Rectangle) String() string { 53 return ufmt.Sprintf(`<rect x="%d" y="%d" width="%d" height="%d" fill="%s" />`, c.X, c.Y, c.Width, c.Height, c.Fill) 54 } 55 56 func (c *Canvas) DrawRectangle(x, y, width, height int, fill string) { 57 c.Append(Rectangle{ 58 X: x, 59 Y: y, 60 Width: width, 61 Height: height, 62 Fill: fill, 63 }) 64 } 65 66 type Text struct { 67 X, Y int 68 Text, Fill string 69 } 70 71 func (c Text) String() string { 72 return ufmt.Sprintf(`<text x="%d" y="%d" fill="%s">%s</text>`, c.X, c.Y, c.Fill, c.Text) 73 } 74 75 func (c *Canvas) DrawText(x, y int, text, fill string) { 76 c.Append(Text{ 77 X: x, 78 Y: y, 79 Text: text, 80 Fill: fill, 81 }) 82 }