rough

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 7 Imported by: 0

README

rough-go

rough-go is a native Go port of Rough.js 4.0.4. It creates the same ordered drawing operations and SVG path descriptions without a JavaScript runtime.

The compatibility target is the exact Rough.js implementation embedded by D2: version 4.0.4 at commit 35302b8f1119ad004b4550f9a4d4162d3d845b0a. Later Rough.js releases have behavior-changing renderer and option changes, so they are not the oracle for this module.

package main

import (
	"fmt"

	rough "github.com/d2lang/rough-go"
)

func main() {
	g := rough.NewGenerator(nil)
	d := g.Rectangle(0, 0, 120, 60, &rough.Options{
		Fill: rough.String("#f6d32d"),
		Seed: rough.Float64(1),
	})
	for _, path := range g.ToPaths(d) {
		fmt.Println(path.D)
	}
}

The full generator surface is ported: line, rectangle, ellipse, circle, linear path, polygon, arc, curve, SVG path, every 4.0.4 fill style, operation serialization, and ToPaths conversion. Pointer-valued option fields preserve the JavaScript distinction between an omitted option and an explicit zero.

Compatibility tests

The regular suite is pure Go:

go test ./...
go test -race ./...
go vet ./...

The optional differential suite runs the Go port against a frozen Rough.js 4.0.4 browser bundle. Node.js is used only by the test oracle; the library has no JavaScript dependency.

ROUGH_GO_ROUGH_JS=/absolute/path/to/rough-4.0.4.js go test ./...

Rough.js 4.0.4 did not ship automated tests. Accordingly, rough-go uses source-shaped unit tests, deterministic randomized differential tests, and D2's existing exact sketch SVG goldens.

License

MIT. See LICENSE and NOTICE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float64

func Float64(v float64) *float64

Float64 returns a pointer suitable for a numeric Options field.

func NewSeed

func NewSeed() float64

NewSeed returns a nondeterministic seed in Rough.js's supported range.

func NumberString

func NumberString(v float64) string

NumberString formats v the way JavaScript coerces a Number to a string. It is useful when surrounding SVG serialization must remain byte-compatible with Rough.js output.

func String

func String(v string) *string

String returns a pointer suitable for a string Options field.

Types

type Config

type Config struct {
	Options *Options `json:"options,omitempty"`
}

Config configures a Generator.

type Drawable

type Drawable struct {
	Shape   string
	Options *ResolvedOptions
	Sets    []OpSet
}

Drawable is the source-shaped output of Generator drawing methods.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator creates Rough.js-compatible drawing operations.

A Generator retains the same mutable default-option random state as Rough.js. Its methods are safe for concurrent use, though callers that need independent deterministic streams should use separate generators.

func NewGenerator

func NewGenerator(config *Config, surface ...*Surface) *Generator

NewGenerator creates a generator. A nil config uses Rough.js 4.0.4's defaults. The optional surface supplies patterned-path fallback dimensions.

func (*Generator) Arc

func (g *Generator) Arc(x, y, width, height, start, stop float64, closed bool, options *Options) Drawable

Arc creates a rough elliptical arc.

func (*Generator) Circle

func (g *Generator) Circle(x, y, diameter float64, options *Options) Drawable

Circle creates a rough circle centered at x,y.

func (*Generator) Curve

func (g *Generator) Curve(points []Point, options *Options) Drawable

Curve creates a rough curve through points.

func (*Generator) DefaultOptions

func (g *Generator) DefaultOptions() ResolvedOptions

DefaultOptions returns a value copy of the generator's current defaults.

func (*Generator) Ellipse

func (g *Generator) Ellipse(x, y, width, height float64, options *Options) Drawable

Ellipse creates a rough ellipse centered at x,y.

func (*Generator) Line

func (g *Generator) Line(x1, y1, x2, y2 float64, options *Options) Drawable

Line creates a rough line.

func (*Generator) LinearPath

func (g *Generator) LinearPath(points []Point, options *Options) Drawable

LinearPath creates an open rough path through points.

func (*Generator) OpsToPath

func (g *Generator) OpsToPath(drawing OpSet) string

OpsToPath serializes operations using Rough.js's SVG punctuation and number formatting.

func (*Generator) Path

func (g *Generator) Path(pathData string, options *Options) Drawable

Path creates a rough rendering of SVG path data.

func (*Generator) Polygon

func (g *Generator) Polygon(points []Point, options *Options) Drawable

Polygon creates a closed rough polygon.

func (*Generator) Rectangle

func (g *Generator) Rectangle(x, y, width, height float64, options *Options) Drawable

Rectangle creates a rough rectangle.

func (*Generator) ToPaths

func (g *Generator) ToPaths(d Drawable) []PathInfo

ToPaths converts a Drawable into the ordered SVG path descriptions emitted by Rough.js's generator API.

type Line

type Line [2]Point

Line is a line segment represented by its two endpoints.

type Op

type Op struct {
	Op   OpType    `json:"op"`
	Data []float64 `json:"data"`
}

Op is one path operation and its ordered numeric arguments.

type OpSet

type OpSet struct {
	Type OpSetType `json:"type"`
	Ops  []Op      `json:"ops"`
	Size *Point    `json:"size,omitempty"`
	Path string    `json:"path,omitempty"`
}

OpSet is one ordered set of path operations.

type OpSetType

type OpSetType string

OpSetType identifies the rendering role of an operation set.

const (
	OpSetPath          OpSetType = "path"
	OpSetFillPath      OpSetType = "fillPath"
	OpSetFillSketch    OpSetType = "fillSketch"
	OpSetPath2DFill    OpSetType = "path2Dfill"
	OpSetPath2DPattern OpSetType = "path2Dpattern"
)

type OpType

type OpType string

OpType identifies a primitive path operation.

const (
	OpMove     OpType = "move"
	OpBCurveTo OpType = "bcurveTo"
	OpLineTo   OpType = "lineTo"
	OpQCurveTo OpType = "qcurveTo"
)

type Options

type Options struct {
	MaxRandomnessOffset *float64 `json:"maxRandomnessOffset,omitempty"`
	Roughness           *float64 `json:"roughness,omitempty"`
	Bowing              *float64 `json:"bowing,omitempty"`
	Stroke              *string  `json:"stroke,omitempty"`
	StrokeWidth         *float64 `json:"strokeWidth,omitempty"`
	CurveFitting        *float64 `json:"curveFitting,omitempty"`
	CurveTightness      *float64 `json:"curveTightness,omitempty"`
	CurveStepCount      *float64 `json:"curveStepCount,omitempty"`
	Fill                *string  `json:"fill,omitempty"`
	FillStyle           *string  `json:"fillStyle,omitempty"`
	FillWeight          *float64 `json:"fillWeight,omitempty"`
	HachureAngle        *float64 `json:"hachureAngle,omitempty"`
	HachureGap          *float64 `json:"hachureGap,omitempty"`
	Simplification      *float64 `json:"simplification,omitempty"`
	DashOffset          *float64 `json:"dashOffset,omitempty"`
	DashGap             *float64 `json:"dashGap,omitempty"`
	ZigzagOffset        *float64 `json:"zigzagOffset,omitempty"`
	Seed                *float64 `json:"seed,omitempty"`
}

Options contains optional Rough.js drawing options. Pointer fields preserve the distinction between an omitted option and an explicit zero value.

type PathInfo

type PathInfo struct {
	D           string
	Stroke      string
	StrokeWidth float64
	Fill        string
	Pattern     *PatternInfo
}

PathInfo is the SVG-oriented representation of an operation set.

type PatternInfo

type PatternInfo struct {
	X            float64
	Y            float64
	Width        float64
	Height       float64
	ViewBox      string
	PatternUnits string
	Path         PathInfo
}

PatternInfo describes the pattern used by a patterned SVG path fill.

type Point

type Point [2]float64

Point is a two-dimensional coordinate.

type Rectangle

type Rectangle struct {
	X      float64
	Y      float64
	Width  float64
	Height float64
}

Rectangle is an axis-aligned rectangle.

type ResolvedOptions

type ResolvedOptions struct {
	MaxRandomnessOffset float64
	Roughness           float64
	Bowing              float64
	Stroke              string
	StrokeWidth         float64
	CurveFitting        float64
	CurveTightness      float64
	CurveStepCount      float64
	Fill                string
	FillStyle           string
	FillWeight          float64
	HachureAngle        float64
	HachureGap          float64
	Simplification      float64
	DashOffset          float64
	DashGap             float64
	ZigzagOffset        float64
	Seed                float64
	RoughnessGain       float64
	// contains filtered or unexported fields
}

ResolvedOptions is the complete option set used by the renderer.

type Surface

type Surface struct {
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

Surface supplies the fallback dimensions used for patterned SVG path fills.

Jump to

Keyboard shortcuts

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