1. Products
  2.   Image
  3.   GO
  4.   SVGo
 
  

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.  

SVGo is an open source Go language library that enables software developers to create SVG inside their own apps using Scalable Vector Graphics 1.1 Specification. The library provides support for SVG shapes like circles, ellipses, polygons, rectangles, etc. It also supports SVG transformations features like translate, rotate, scale, skew, and so on.

The SVGo library can run anywhere the Go language is available and is completely portable across popular operating systems like Linux, Mac OS X, Windows, etc. The library has included several important features related to shapes, lines, text, drawing paths, images and gradients, image transformation, filter effects, applying animations, metadata elements, and so on.

The library is designed to produce standard, clean, readable code that is close to tuned, hand-crafted code that makes full use of a variety of SVG elements. The library has provided support for built-in filters for user convenience, such as blur function by standard deviation, greyscale filter to the image, Rotate Hues, Invert the image's colors, apply sepia tone, and many more.

Previous Next

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/svgo

Generate 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.

Create SVG Image via GO

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.

Drawing 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.

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.

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()
}
 English