Edits history of script submission #193 for ' SVG Path to PNG (helper)'

  • go
    import (
    	"encoding/base64"
    	"io/ioutil"
    	"github.com/tdewolff/canvas"
    	"github.com/tdewolff/canvas/renderers"
    )
    
    // This is just an example. To convert your own SVG path to a PNG file, you need
    // to adapt the script.
    func main() (interface{}, error) {
    	// Create new canvas of dimension 400x400 mm
    	c := canvas.New(400, 400)
    
    	// Create a canvas context used to keep drawing state
    	ctx := canvas.NewContext(c)
    
    	// Create a triangle path from an SVG path and draw it to the canvas
    	path, err := canvas.ParseSVG("M150 0 L75 200 L225 200 Z")
    	if err != nil {
    		panic(err)
    	}
    	ctx.SetFillColor(canvas.Mediumseagreen)
    	ctx.DrawPath(20, 20, path)
    
    	renderers.Write("tmp.png", c, canvas.DPMM(1.0))
    
    	content, err2 := ioutil.ReadFile("tmp.png")
    	if err2 != nil {
    		panic(err)
    	}
    	output := base64.StdEncoding.EncodeToString([]byte(content))
    
    	return output, nil
    }

    Submitted by jaller94 1314 days ago