Open Source Go Library for SVG Creation and Manipulation
A Very Stable Go API that Supports Generating SVG Drawings. You can Add Shapes, Lines, Text, Images, Paths and Apply Color, Gradients, Animations and More.
What is SVGo?
SVGo is a powerful, open-source Go library that empowers software developers to generate SVG (Scalable Vector Graphics) content directly within their applications, adhering to the SVG 1.1 Specification. It provides comprehensive support for creating core SVG shapes, including circles, ellipses, polygons, and rectangles. Furthermore, the library enables sophisticated SVG transformations such as translate, rotate, scale, and skew, offering precise control over graphical elements. Its design philosophy emphasizes producing standard, clean, and readable output code that rivals hand-tuned, efficient SVG markup.
Fully portable, SVGo runs seamlessly wherever the Go language is supported, including major operating systems like Linux, Mac OS X, and Windows. The library encompasses a wide array of essential features for advanced vector graphics, covering shapes, lines, text, complex drawing paths, images, gradients, and filter effects. For user convenience, it includes built-in filters like Gaussian blur by standard deviation, greyscale conversion, hue rotation, color inversion, and sepia tone application. With additional capabilities in image transformation, applying animations, and handling metadata elements, SVGo is a complete toolkit for generating dynamic and high-quality scalable vector graphics in Go.
Getting Started with Imaging
The easiest and recommend way to install SVGo is via GitHub. Please use the following command for an easy and smooth installation.
Install SVGo via GitHub
go get -u github.com/ajstarks/svgoGenerate SVG via GO Library
The open source SVGo library enables software developers to programmatically generate SVG drawings inside their own application using Go commands. Developers need to define the width and height of the SVG canvas as well as the destination of SVG. Developers can easily draw shapes like circles, ellipses, polygons, etc. as well as can enter the text of their choice with ease.
How to Create SVG Image via GO API?
package main
import (
"github.com/ajstarks/svgo"
"os"
)
func main() {
width := 500
height := 500
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Circle(width/2, height/2, 100)
canvas.Text(width/2, height/2, "Hello, SVG", "text-anchor:middle;font-size:30px;fill:white")
canvas.End()
}Drawing Shapes & Paths
The SVGo library has provided functionality for creating different kinds of shapes inside their own applications. It supports drawing a circle, ellipse, polygon, rectangle or a rounded rectangle, square, series of line segments, and more. You can also easily draw lines and paths with ease. You can draw an elliptical arc, cure, cubic bezier curve, quadratic bezier curve, and so on.
How to Draw Circle Online via GO?
package main
import (
"log"
"github.com/ajstarks/svgo"
"net/http"
)
func main() {
http.Handle("/circle", http.HandlerFunc(circle))
err := http.ListenAndServe(":2003", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func circle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
s := svg.New(w)
s.Start(500, 500)
s.Circle(250, 250, 125, "fill:none;stroke:black")
s.End()
}Image and Text Support
The open source SVGo library has provided complete support for inserting images and text while creating an SVG drawing using Go commands. While placing an image or text you need to s specify the place where you want to put it and can also define the width and height of the image. You can also insert multiple lines of text and define size, fill, alignment, and spacing. It also supports placing styled text along with the path. You can also define the color of your choice using the RGB scheme.
How to Add Image & Text to SVG inside GO Apps?
import (
"os"
svg "github.com/ajstarks/svgo"
)
func main() {
width := 500
height := 500
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Circle(width/2, height/2, 100)
canvas.Gstyle("text-anchor:middle;font-family:sans;fill:white")
canvas.Textspan(width/2, height/2, "Hello ", "font-size:30px")
canvas.Span("SVG", "font-family:serif;font-size:50px;fill:yellow")
canvas.TextEnd()
canvas.Gend()
canvas.End()
}Applying Animation to SVG
SVG animation elements were developed in collaboration with the World Wide Web Consortium (W3C) Synchronized Multimedia Working Group. The SVGo enables developers to make stunning animations and export a single animated SVG file with ease. The library allows animating the item referenced by the link, animates the referenced object along the specified path, animates the translation transformation, animates the rotation transformation, animates the scale or skewX transformation, and so on.
How to Add Animation to SVG via GO library?
func main() {
width, height := 500, 500
rsize := 100
csize := rsize / 2
duration := 5.0
repeat := 5
imw, imh := 100, 144
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Circle(csize, csize, csize, `fill="red"`, `id="circle"`)
canvas.Image((width/2)-(imw/2), 0, imw, imh, "gopher.jpg", `id="gopher"`)
canvas.Square(width-rsize, 0, rsize, `fill="blue"`, `id="square"`)
canvas.Animate("#circle", "cx", 0, width, duration, repeat)
canvas.Animate("#circle", "cy", 0, height, duration, repeat)
canvas.Animate("#square", "x", width, 0, duration, repeat)
canvas.Animate("#square", "y", height, 0, duration, repeat)
canvas.Animate("#gopher", "y", 0, height, duration, repeat)
canvas.End()
}