Skip to content

Instantly share code, notes, and snippets.

@ajstarks
Created November 26, 2010 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajstarks/716351 to your computer and use it in GitHub Desktop.
Save ajstarks/716351 to your computer and use it in GitHub Desktop.
Comparison of draw2d and SVGo
package main
import (
"os"
"fmt"
"math"
"github.com/ajstarks/svgo"
)
var (
g = svg.New(os.Stdout)
width = 1024
height = 768
)
func TestLineCap() {
frame()
g.Gstyle("stroke-width:30;stroke:black")
g.Line(64, 50, 64, 200, "stroke-linecap:butt")
g.Line(128, 50, 128, 200, "stroke-linecap:round")
g.Line(192, 50, 192, 200, "stroke-linecap:square")
g.Gend()
g.Gstyle("stroke-width:3;stroke:rgb(255,51,51)")
g.Line(64, 50, 64, 200)
g.Line(128, 50, 128, 200)
g.Line(192, 50, 192, 200)
g.Gend()
}
func TestPath() {
frame()
p := "M10 10 L100 10 L100 100 L10 100 L10 10z"
g.Path(p, "fill:white;stroke:black")
}
func TestLineJoin() {
x := 77
y := 84
o := 51
var xp = []int{x, x + o, x + (o * 2)}
var yp = []int{y, y - o, y}
frame()
g.Gstyle("stroke-width:30;stroke:black;fill:none")
g.Polyline(xp, yp, "stroke-linejoin:miter")
g.Polyline(xp, yp, "stroke:red; stroke-width:3")
g.Gtransform(fmt.Sprintf("translate(0, %d)", x))
g.Polyline(xp, yp, "stroke-linejoin:bevel")
g.Polyline(xp, yp, "stroke:red; stroke-width:3")
g.Gend()
g.Gtransform(fmt.Sprintf("translate(0, %d)", x*2))
g.Polyline(xp, yp, "stroke-linejoin:round")
g.Polyline(xp, yp, "stroke:red; stroke-width:3")
g.Gend()
g.Gend()
}
func TestDrawCubicCurve() {
var x = []int{25, 102, 153, 230}
var y = []int{128, 230, 25, 128}
frame()
g.Gstyle("fill:none")
g.Bezier(x[0], y[0], x[1], y[1], x[2], y[2], x[3], y[3], "stroke:black;stroke-width:10")
g.Polyline(x, y, "stroke:#FF3333;stroke-width:6;stroke-opacity:0.5")
g.Gend()
}
func polar(cx, cy, r, t int) (int, int) {
theta := float64(t) * (math.Pi / 180.0)
radius := float64(r)
x := radius * math.Cos(theta)
y := radius * math.Sin(theta)
return int(x) + cx, int(y) + cy
}
func arclines(xc, yc, radius int) (int, int, int, int) {
xa, ya := polar(xc, yc, radius, 60)
xb, yb := polar(xc, yc, radius, 180)
g.Gstyle("stroke-linecap:round;stroke-width:6;fill:none")
g.Gstyle("stroke:#ff3333;stroke-opacity:0.5")
g.Line(xa, ya, xc, yc)
g.Line(xb, yb, xc, yc)
g.Gend()
g.Circle(xc, yc, 10, "fill:#ff3333;stroke:none;fill-opacity:0.5")
g.Gend()
return xa, ya, xb, yb
}
func TestDrawArc() {
frame()
xa, ya, xb, yb := arclines(128, 128, 100)
g.Arc(xa, ya, 100, 100, 0, false, true, xb, yb, "fill:none;stroke:black;stroke-width:10")
}
func TestDrawNegativeArc() {
frame()
xa, ya, xb, yb := arclines(128, 128, 100)
g.Arc(xb, yb, 100, 100, 0, true, true, xa, ya, "fill:none;stroke:black;stroke-width:10")
}
func TestRoundRectangle() {
frame()
g.Roundrect(25, 25, 205, 205, 10, 10,
"fill:rgb(128, 128, 255); stroke:rgb(128,0,0);stroke-width:10")
}
func TestMultiSegmentCaps() {
frame()
g.Gstyle("stroke-linecap:round;stroke-width:30;stroke:black")
g.Line(50, 75, 200, 75)
g.Line(50, 125, 200, 125)
g.Line(50, 175, 200, 175)
g.Gend()
}
func frame() {
g.Rect(0, 0, 256, 256, "stroke:lightgray;fill:rgb(240,240,240)")
}
func TestDash() {
frame()
p := "M128,25.6 L128,25.6 L230.4,230.4 l-102.4,0.0 C51.2,230.4 51.2,128.0 128.0,128.0"
s := `fill:none;
stroke:black;
stroke-linecap:butt;
stroke-linejoin:bevel;
stroke-width:10;
stroke-dasharray:50,10,10,10`
g.Path(p, s)
}
func TestFillStrokePath() {
frame()
p1 := "M128,25.6 L128,25.6 L230.4,230.4 l-102.4,0.0 C51.2,230.4 51.2,128.0 128.0,128.0z"
p2 := "M64.0,25.6 l51.2,51.2 l-51.2,51.2 l-51.2,-51.2z"
g.Gstyle("stroke:black;stroke-width:10;fill:blue;stroke-linejoin:round")
g.Path(p1)
g.Path(p2)
g.Gend()
}
func TestFillStyle() {
x1 := 40
y1 := 84
x2 := x1 + 120
y2 := 84
alen := 60
asize := 20
frame()
g.Gstyle("fill:#00B200;stroke:black;stroke-width:6")
g.Rect(12, 12, 230, 70)
g.Arc(x1, y1, asize, asize, 0, true, false, x1+alen, y1)
g.Arc(x2, y2, asize, asize, 0, true, false, x2+alen, y2)
g.Gend()
g.Gstyle("fill:white;stroke:black;stroke-width:6")
g.Arc(x1, y1, asize, asize, 0, true, true, x1+alen, y1)
g.Arc(x2, y2, asize, asize, 0, true, true, x2+alen, y2)
g.Gend()
y1 += 116
y2 += 116
g.Gstyle("fill:#0000E5;stroke:black;stroke-width:6")
g.Rect(12, 128, 230, 70)
g.Arc(x1, y1, asize, asize, 0, true, false, x1+alen, y1)
g.Arc(x2, y2, asize, asize, 0, true, false, x2+alen, y2)
g.Arc(x1, y2, asize, asize, 0, true, true, x1+alen, y2)
g.Arc(x2, y2, asize, asize, 0, true, true, x2+alen, y2, "fill:white")
g.Gend()
}
func TestFillStroke() {
frame()
g.Gstyle("stroke:black;stroke-width:10;fill:blue;stroke-linejoin:round;stroke-linecap:round")
g.Polyline([]int{128, 128, 230, 128}, []int{128, 25, 230, 230})
g.Arc(128, 230, 51, 51, 0, true, true, 128, 128)
g.Polygon([]int{64, 115, 64, 13}, []int{25, 76, 127, 76})
g.Gend()
}
func main() {
g.Start(width, height)
g.Rect(0,0,width,height,g.RGB(240,240,240))
TestDrawCubicCurve()
g.Gtransform("translate(256,0)")
TestLineCap()
g.Gend()
g.Gtransform("translate(512,0)")
TestLineJoin()
g.Gend()
g.Gtransform("translate(768,0)")
TestDrawArc()
g.Gend()
g.Gtransform("translate(0, 256)")
TestRoundRectangle()
g.Gend()
g.Gtransform("translate(256, 256)")
TestMultiSegmentCaps()
g.Gend()
g.Gtransform("translate(512, 256)")
TestPath()
g.Gend()
g.Gtransform("translate(768, 256)")
TestDrawNegativeArc()
g.Gend()
g.Gtransform("translate(0, 512)")
TestDash()
g.Gend()
g.Gtransform("translate(256, 512)")
TestFillStroke()
g.Gend()
g.Gtransform("translate(512, 512)")
TestFillStyle()
g.Gend()
g.End()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment