25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2KB

  1. // Package path provides objects to define lines on a sewing pattern.
  2. package path
  3. import (
  4. "image/color"
  5. "git.wtrh.nl/wouter/gopatterns/pkg/pattern/point"
  6. "github.com/tdewolff/canvas"
  7. )
  8. // Path defines a set of straight lines through points.
  9. type Path struct {
  10. points []point.Point
  11. thickness float64
  12. color color.RGBA
  13. }
  14. // NewPath returns a new [Path].
  15. func NewPath(points ...point.Point) Path {
  16. black := canvas.Black
  17. return Path{points: points, color: black, thickness: 0.2}
  18. }
  19. // NewPathWithStyle returns a new [Path] with the specified thickness and color.
  20. func NewPathWithStyle(thickness float64, color color.RGBA, points ...point.Point) Path {
  21. return Path{points: points, color: color, thickness: thickness}
  22. }
  23. // Draw the path to the provided [canvas.Canvas].
  24. func (p Path) Draw(c *canvas.Canvas) error {
  25. polyline := canvas.Polyline{}
  26. for _, next := range p.points {
  27. polyline.Add(next.Vector().Values())
  28. }
  29. c.RenderPath(polyline.ToPath(),
  30. canvas.Style{
  31. Fill: canvas.Paint{},
  32. Stroke: canvas.Paint{Color: p.color},
  33. StrokeWidth: p.thickness,
  34. StrokeCapper: canvas.RoundCap,
  35. StrokeJoiner: canvas.BevelJoin,
  36. DashOffset: 0,
  37. Dashes: nil,
  38. FillRule: 0,
  39. }, canvas.Identity)
  40. return nil
  41. }