Skip to content

Instantly share code, notes, and snippets.

@jphsd
Last active September 12, 2026 16:51
Show Gist options
  • Select an option

  • Save jphsd/12050b3e2648e165944e4ea2eb521dda to your computer and use it in GitHub Desktop.

Select an option

Save jphsd/12050b3e2648e165944e4ea2eb521dda to your computer and use it in GitHub Desktop.
Using graphics2d to create a stamp mask and then applying it along a path
package main
import (
"image/draw"
stdimg "image"
"math"
g2d "github.com/jphsd/graphics2d"
"github.com/jphsd/graphics2d/color"
"github.com/jphsd/graphics2d/image"
)
// Stamping along a path with a predefined mask.
// Limitations - mask offsets must be whole pixels,
// mask can't be rotated to match path tangent.
// Compare and contrast with CapProc.
func main() {
//path := g2d.Circle([]float64{280, 150}, 110)
// Line, MPD it, round it - a wriggle
path := g2d.Line([]float64{30, 150}, []float64{530, 150})
path = path.Process(&g2d.MPDProc{.3, 3, 0.5, false})[0]
path = path.Process(&g2d.RoundedProc{1000})[0]
//shape := g2d.NewShape(g2d.Circle([]float64{0, 0}, 10))
shape := g2d.NewShape(g2d.ReentrantPolygon([]float64{0, 0}, 20, 5, 0.5, 0))
stamped := StampedShape(path, 10, shape, image.Point{}, color.Alpha16{0x3f3f})
img := image.NewRGBA(560, 300, color.White)
rect := stamped.Bounds()
g2d.DrawMask(img, rect, color.Orange, stamped, rect.Min, draw.Over)
g2d.DrawPath(img, path, g2d.BlackPen)
image.SaveImage(img, "stamp")
}
func StampedShape(path *g2d.Path, step float64, stamp *g2d.Shape, offs image.Point, alpha color.Alpha16) *image.Alpha {
return StampedMask(path, step, stamp.AlphaMask(alpha), offs)
}
func StampedMask(path *g2d.Path, step float64, mask *image.Alpha, offs image.Point) *image.Alpha {
// Construct image large enough to hold stamped path
pbounds, sbounds := path.Bounds(), mask.Bounds()
minx, maxx := offs.X-sbounds.Min.X, sbounds.Max.X-offs.X
miny, maxy := offs.Y-sbounds.Min.Y, sbounds.Max.Y-offs.Y
pbounds.Min.X -= minx
pbounds.Min.Y -= miny
pbounds.Max.X += maxx
pbounds.Max.Y += maxy
img := stdimg.NewGray(pbounds)
// Figure stamp points
paths := path.Process(g2d.NewFSnipProc(2, []float64{step, step}, 0))
for i, path := range paths {
parts := path.Parts()
if i == 0 {
// First point
p := parts[0][0]
x, y := int(math.Floor(p[0])), int(math.Floor(p[1]))
rect := image.Rectangle{image.Point{x - minx, y - miny}, image.Point{x + maxx, y + maxy}}
g2d.DrawMask(img, rect, color.White, mask, sbounds.Min, draw.Over)
if len(parts[0]) == 1 {
// Only one point
return image.GrayToAlpha(img)
}
}
// Last point in last part
nparts := len(parts)
last := parts[nparts-1]
np := len(last)
p := last[np-1]
x, y := int(math.Floor(p[0])), int(math.Floor(p[1]))
rect := image.Rectangle{image.Point{x - minx, y - miny}, image.Point{x + maxx, y + maxy}}
g2d.DrawMask(img, rect, color.White, mask, sbounds.Min, draw.Over)
}
return image.GrayToAlpha(img)
}

How To Run

  • Download Go for your platform from here
  • Create a new directory and switch to it
  • Download the .go files from this Gist into it
  • Run $ go mod init main this will create a file called go.mod that's needed for the next step
  • Run $ go mod tidy this will pull down the packages needed to run the program
  • Run $ go run *.go
  • The result will be in stamp.png. stamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment