gorough

package module
v0.0.0-...-153a594 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2021 License: MIT Imports: 8 Imported by: 0

README

Gorough

Go Reference GitHub

Based on Rough.js.

Gorough works with SVG.

Install

go get -u github.com/NovikovRoman/gorough

Usage

Create SVG canvas
width := 500
height := 500
// w - io.Writer interface
canvas := svg.New(w)
canvas.Start(width, height)
// drawing code
canvas.End()
Lines

Lines

line := gorough.NewLine(gorough.Point{X: 30, Y: 100}, gorough.Point{X: 270, Y: 20}, &gorough.LineOptions{
    Styles: &gorough.Styles{
        Stroke: "#ff0000",
    },
})

line2 := gorough.NewLine(gorough.Point{X: 60, Y: 10}, gorough.Point{X: 230, Y: 110}, &gorough.LineOptions{
    Styles: &gorough.Styles{
        Stroke: "#00ff00",
    },
})

line3 := gorough.NewLine(gorough.Point{X: 10, Y: 70}, gorough.Point{X: 250, Y: 90}, &gorough.LineOptions{
    Styles: &gorough.Styles{
        Stroke: "#0000ff",
    },
})

gorough.DrawSVG(canvas, line, nil)
gorough.DrawSVG(canvas, line2, nil)
gorough.DrawSVG(canvas, line3, nil)
Rectangles

Rectangle

rectangle := gorough.NewRectangle(gorough.Point{X: 20, Y: 20}, 240, 120, nil)

square := gorough.NewRectangle(gorough.Point{X: 10, Y: 10}, 70, 70, &gorough.RectangleOptions{
    Styles: &gorough.Styles{
        Stroke: "#ff0080",
    },
})

gorough.DrawSVG(canvas, rectangle, nil)
gorough.DrawSVG(canvas, square, nil)
Poligons

Poligons

poligon := gorough.NewPoligon([]gorough.Point{
    {X: 10, Y: 10},
    {X: 200, Y: 10},
    {X: 100, Y: 100},
    {X: 300, Y: 100},
    {X: 60, Y: 200},
}, nil)

poligon2 := gorough.NewPoligon([]gorough.Point{
    {X: 50, Y: 20},
    {X: 30, Y: 200},
    {X: 300, Y: 180},
    {X: 280, Y: 30},
    {X: 170, Y: 180},
}, &gorough.LineOptions{
    Styles: &gorough.Styles{
        Stroke: "#ff0000",
    },
})

gorough.DrawSVG(canvas, poligon, nil)
gorough.DrawSVG(canvas, poligon2, nil)
Ellipses

Ellipses

circle := gorough.NewCircle(gorough.Point{X: 40, Y: 80}, 50, &gorough.EllipseOptions{
    Styles: &gorough.Styles{
        Stroke:      "#ff0080",
        StrokeWidth: 1,
    },
})

ellipseVert := gorough.NewEllipse(gorough.Point{X: 150, Y: 60}, 60, 110, &gorough.EllipseOptions{
    Styles: &gorough.Styles{
        Stroke:      "#00ff80",
    },
})

ellipseHoriz := gorough.NewEllipse(gorough.Point{X: 250, Y: 60}, 150, 80, &gorough.EllipseOptions{
    Styles: &gorough.Styles{
        Stroke:      "#660066",
        StrokeWidth: 1,
    },
})

gorough.DrawSVG(canvas, circle, nil)
gorough.DrawSVG(canvas, ellipseVert, nil)
gorough.DrawSVG(canvas, ellipseHoriz, nil)
Arcs

Arcs

arc := gorough.NewArc(gorough.Point{X: 40, Y: 80}, 100, 100, -math.Pi/2, 0, true,
		&gorough.EllipseOptions{
			Styles: &gorough.Styles{
				Stroke: "#ff0080",
			},
		})

arc2 := gorough.NewArc(gorough.Point{X: 150, Y: 60}, 60, 110, -math.Pi/6, math.Pi, true,
    &gorough.EllipseOptions{
        Styles: &gorough.Styles{
            Stroke: "#00ff80",
        },
    })

arc3 := gorough.NewArc(gorough.Point{X: 250, Y: 60}, 150, 80, -math.Pi/2, math.Pi/2, false,
    &gorough.EllipseOptions{
        Styles: &gorough.Styles{
            Stroke: "#660066",
        },
    })

gorough.DrawSVG(canvas, arc, nil)
gorough.DrawSVG(canvas, arc2, nil)
gorough.DrawSVG(canvas, arc3, nil)
Linear Paths

Linear Path

linearPath := gorough.NewLinearPath([]gorough.Point{
    {X: 10, Y: 10},
    {X: 200, Y: 10},
    {X: 100, Y: 100},
    {X: 300, Y: 100},
    {X: 60, Y: 200},
}, nil)

linearPath2 := gorough.NewLinearPath([]gorough.Point{
    {X: 20, Y: 50},
    {X: 50, Y: 200},
    {X: 80, Y: 50},
    {X: 110, Y: 200},
    {X: 140, Y: 50},
    {X: 170, Y: 200},
    {X: 200, Y: 50},
    {X: 230, Y: 200},
    {X: 260, Y: 50},
    {X: 290, Y: 200},
}, &gorough.LineOptions{
    Styles: &gorough.Styles{
        Stroke: "#00ff00",
    },
})

gorough.DrawSVG(canvas, linearPath, nil)
gorough.DrawSVG(canvas, linearPath2, nil)
Curves

Curves

curve := gorough.NewCurve([]gorough.Point{
    {X: 10, Y: 10},
    {X: 200, Y: 10},
    {X: 100, Y: 100},
    {X: 300, Y: 100},
    {X: 60, Y: 200},
}, nil)

curve2 := gorough.NewCurve([]gorough.Point{
    {X: 50, Y: 20},
    {X: 30, Y: 200},
    {X: 300, Y: 180},
    {X: 280, Y: 30},
    {X: 170, Y: 180},
}, &gorough.EllipseOptions{
    Styles: &gorough.Styles{
        Stroke: "#ff0000",
    },
})

gorough.DrawSVG(canvas, curve, nil)
gorough.DrawSVG(canvas, curve2, nil)
Paths

Paths

svgPath, _ := gorough.NewPath("M4.5000 150.1500L4.5000 … 150.1500Z", &gorough.PathOptions{
    Styles: &gorough.Styles{
        Stroke: "#ff0000",
    },
})


svgPath2, _ := gorough.NewPath("M0.6000 184.5000L0.6000 … 184.5000Z", &gorough.PathOptions{
    Styles: &gorough.Styles{
        Stroke: "#0000ff",
    },
})

gorough.DrawSVG(canvas, svgPath, nil)
gorough.DrawSVG(canvas, svgPath2, nil)
Filling

Filling

// Fillers
dashedFiller := gorough.NewDashedFiller()
dotFiller := gorough.NewDotFiller()
hachureFiller := gorough.NewHachureFiller()
hatchFiller := gorough.NewHatchFiller()
zigZagFiller := gorough.NewZigZagFiller()
zigZagHatchFiller := gorough.NewZigZagHatchFiller()

hatch := gorough.NewRectangle(gorough.Point{X: 20, Y: 20}, 60, 60, &gorough.RectangleOptions{
    Styles: &gorough.Styles{
        Stroke:     "#4e9835",
        Fill:       "#60c247",
        FillWeight: 1,
        Filler:     hatchFiller,
    },
})

hatchFiller.SetAngle(90)
hatchFiller.SetGap(8)
hatchAlt := gorough.NewRectangle(gorough.Point{X: 100, Y: 20}, 60, 60, &gorough.RectangleOptions{
    Styles: &gorough.Styles{
        Stroke:     "#4e9835",
        Fill:       "#60c247",
        FillWeight: 1,
        Filler:     hatchFiller,
    },
})

Options

Styles

Stroke - String value representing the color of the drawn objects. Default value is black (#000000). If the this is set to none, the shape vectors do not contain a stroke (This is different from having a transparent stroke).

StrokeWidth - Numerical value to set the width of the strokes (in pixels). Default value is 1.

Fill - String value representing the color used to fill a shape. In hachure style fills, this represents the color of the hachure lines. In dots style, it represents the color of the dots.

FillWeight - Numeric value representing the width of the hachure lines. Default value of the fillWeight is set to half the strokeWidth of that shape.

Filler - fill styles (dashedFiller, dotFiller, hachureFiller, hatchFiller, zigZagFiller, zigZagHatchFiller). If filler value is nil and fill is not empty then fill style solid.

PenOptions

PenOptions

The image shows:

  1. Default options
  2. MaxRandomnessOffset - 15
  3. Roughness - 6
  4. Bowing - 30

MaxRandomnessOffset - Default value is 2.

Roughness - Numerical value indicating how rough the drawing is. A rectangle with the roughness of 0 would be a perfect rectangle. Default value is 1. There is no upper limit to this value, but a value over 10 is mostly useless.

Bowing - Numerical value indicating how curvy the lines are when drawing a sketch. A value of 0 will cause straight lines. Default value is 1.

CurveOptions

CurveOptions

The image shows:

  1. Default options
  2. Tightness - 3
  3. Tightness - -3
  4. StepCount - 50
  5. StepCount - 4

Tightness - Concavity of arcs between points. Default value is 0.

Fitting - When drawing ellipses, circles, and arcs, Let RoughJS know how close should the rendered dimensions be when compared to the specified one. Default value is 0.95 - which means the rendered dimensions will be at least 95% close to the specified dimensions. A value of 1 will ensure that the dimensions are almost 100% accurate.

StepCount - When drawing ellipses, circles, and arcs, RoughJS approximates curveStepCount number of points to estimate the shape. Default value is 9.

Attributes

Set attributes for an SVG shape group.

attrs := gorough.Attributes{
    {"id", "myShape"},
}

gorough.DrawSVG(canvas, shape, attrs)

attrs["id"] = "otherShape"
attrs["data-value"] = "123"
gorough.DrawSVG(canvas, shape, attrs)

In SVG:

<g id="myShape">
<path d="M42.8755552948236 … 55.54974206571005" fill='none' stroke='#ff0080' stroke-width='1' fill-weight='0' />
</g>
<g id="otherShape" data-value="123">
<path d="M42.8755552948236 … 55.54974206571005" fill='none' stroke='#ff0080' stroke-width='1' fill-weight='0' />
</g>

Start example

cd examples/ && go build -o "example" && ./example

Example

License

MIT License © Roman Novikov

Documentation

Index

Constants

View Source
const (
	None = "none"
)

Variables

This section is empty.

Functions

func Distance

func Distance(p1 Point, p2 Point) float64

Distance returns the distance between 2 points

func DistanceSq

func DistanceSq(p1 Point, p2 Point) float64

DistanceSq returns the distance between 2 points square

func DistanceToSegmentSq

func DistanceToSegmentSq(p Point, v Point, w Point) float64

DistanceToSegmentSq returns the distance squared from a point p to the line segment vw

func DoIntersect

func DoIntersect(p1, q1, p2, q2 Point) bool

func DrawSVG

func DrawSVG(s *svg.SVG, d Drawable, groupAttrs Attributes)

func IsPointInPolygon

func IsPointInPolygon(points []Point, point Point) bool

func NewArc

func NewArc(center Point, width, height, start, stop float64, closed bool, opt *EllipseOptions) *arc

func NewCircle

func NewCircle(center Point, diameter float64, opt *EllipseOptions) *circle

func NewCurve

func NewCurve(points []Point, opt *EllipseOptions) *curve

func NewEllipse

func NewEllipse(center Point, width, height float64, opt *EllipseOptions) *ellipse

func NewLine

func NewLine(p1, p2 Point, opt *LineOptions) *line

func NewLinearPath

func NewLinearPath(points []Point, opt *LineOptions) *linearPath

func NewPath

func NewPath(d string, opt *PathOptions) (*path, error)

func NewPoligon

func NewPoligon(points []Point, opt *LineOptions) *poligon

func NewRectangle

func NewRectangle(p Point, width, height float64, opt *RectangleOptions) *rectangle

func PointsOnPath

func PointsOnPath(path string, tolerance float64, distance float64) (out [][]Point, err error)

func RotateLines

func RotateLines(lines *[]Line, center Point, degrees float64)

func RotatePoints

func RotatePoints(points *[]Point, center Point, degrees float64)

Types

type Attributes

type Attributes map[string]string

func (Attributes) Exclude

func (a Attributes) Exclude(attrs ...string) Attributes

func (Attributes) HasAttr

func (a Attributes) HasAttr(name string) bool

func (Attributes) String

func (a Attributes) String() string

type CurveOptions

type CurveOptions struct {
	Tightness float64
	Fitting   float64
	StepCount float64
}

func CurveOptionsDefault

func CurveOptionsDefault() *CurveOptions

type Drawable

type Drawable interface {
	Name() string
	Operations() []operation
	Attributes() Attributes
	Styles() *Styles
}

type EllipseOptions

type EllipseOptions struct {
	PenOptions   *PenOptions
	CurveOptions *CurveOptions
	Styles       *Styles
}

type Filler

type Filler interface {
	SetAngle(float64)
	SetGap(float64)
	// contains filtered or unexported methods
}

func NewDashedFiller

func NewDashedFiller(dashOffset ...float64) Filler

func NewDotFiller

func NewDotFiller() Filler

func NewHachureFiller

func NewHachureFiller() Filler

func NewHatchFiller

func NewHatchFiller() Filler

func NewZigZagFiller

func NewZigZagFiller() Filler

func NewZigZagHatchFiller

func NewZigZagHatchFiller() Filler

type Line

type Line struct {
	P1 Point
	P2 Point
}

type LineOptions

type LineOptions struct {
	PenOptions *PenOptions
	Styles     *Styles
}

type PathOptions

type PathOptions struct {
	PenOptions            *PenOptions
	Styles                *Styles
	Simplification        float64
	CombineNestedSvgPaths bool
}

type PenOptions

type PenOptions struct {
	MaxRandomnessOffset float64
	Roughness           float64
	Bowing              float64
}

func PenOptionsDefault

func PenOptionsDefault() *PenOptions

type Point

type Point struct {
	X float64
	Y float64
}

func CurveToBezier

func CurveToBezier(pointsIn []Point, curveTightness float64) (out []Point, err error)

func LineIntersection

func LineIntersection(a, b, c, d Point) (Point, bool)

func PointsOnBezierCurves

func PointsOnBezierCurves(points []Point, tolerance, distance float64) (newPoints []Point)

func Simplify

func Simplify(points []Point, distance float64) []Point

Simplify function

func (Point) Eq

func (p Point) Eq(point Point) bool

type RectangleOptions

type RectangleOptions struct {
	PenOptions *PenOptions
	Styles     *Styles
}

type Styles

type Styles struct {
	Stroke      string
	StrokeWidth float64
	Fill        string
	FillWeight  float64
	Filler      Filler
}

func StylesDefault

func StylesDefault() *Styles

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL