Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

55 linhas
1.4KB

  1. // Package path provides objects to define lines on a sewing pattern.
  2. package path
  3. import (
  4. "image/color"
  5. "git.wtrh.nl/patterns/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. // SetThickness updates the tickness of the line
  24. func (p *Path) SetThickness(thickness float64) {
  25. p.thickness = thickness
  26. }
  27. // Draw the path to the provided [canvas.Canvas].
  28. func (p *Path) Draw(c *canvas.Canvas) error {
  29. polyline := canvas.Polyline{}
  30. for _, next := range p.points {
  31. polyline.Add(next.Vector().Values())
  32. }
  33. c.RenderPath(polyline.ToPath(),
  34. canvas.Style{
  35. Fill: canvas.Paint{},
  36. Stroke: canvas.Paint{Color: p.color},
  37. StrokeWidth: p.thickness,
  38. StrokeCapper: canvas.RoundCap,
  39. StrokeJoiner: canvas.BevelJoin,
  40. DashOffset: 0,
  41. Dashes: nil,
  42. FillRule: 0,
  43. }, canvas.Identity)
  44. return nil
  45. }