Kaynağa Gözat

Add line thickness option for templates

pull/6/head
Wouter Horlings 4 ay önce
ebeveyn
işleme
81e0faa945
4 değiştirilmiş dosya ile 36 ekleme ve 8 silme
  1. +8
    -3
      pkg/pattern/path/path.go
  2. +5
    -3
      pkg/pattern/path/splines.go
  3. +18
    -2
      pkg/pattern/template/line.go
  4. +5
    -0
      spec/pattern.yaml

+ 8
- 3
pkg/pattern/path/path.go Dosyayı Görüntüle

@@ -16,9 +16,9 @@ type Path struct {
}

// NewPath returns a new [Path].
func NewPath(points ...point.Point) Path {
func NewPath(points ...point.Point) *Path {
black := canvas.Black
return Path{points: points, color: black, thickness: 0.2}
return &Path{points: points, color: black, thickness: 0.2}
}

// NewPathWithStyle returns a new [Path] with the specified thickness and color.
@@ -26,8 +26,13 @@ func NewPathWithStyle(thickness float64, color color.RGBA, points ...point.Point
return Path{points: points, color: color, thickness: thickness}
}

// SetThickness updates the tickness of the line
func (p *Path) SetThickness(thickness float64) {
p.thickness = thickness
}

// Draw the path to the provided [canvas.Canvas].
func (p Path) Draw(c *canvas.Canvas) error {
func (p *Path) Draw(c *canvas.Canvas) error {
polyline := canvas.Polyline{}
for _, next := range p.points {
polyline.Add(next.Vector().Values())


+ 5
- 3
pkg/pattern/path/splines.go Dosyayı Görüntüle

@@ -12,7 +12,7 @@ const resolution = 40

// Spline defines a smooth curved path through points.
type Spline struct {
Path
*Path
start, end point.Point
}

@@ -78,8 +78,10 @@ func (p Spline) Draw(c *canvas.Canvas) error {

points = append(points, p.points[len(p.points)-1])

err = NewPath(points...).Draw(c)
if err != nil {
path := NewPath(points...)
path.SetThickness(p.thickness)

if err = path.Draw(c); err != nil {
return fmt.Errorf("draw spline points to canvas: %w", err)
}



+ 18
- 2
pkg/pattern/template/line.go Dosyayı Görüntüle

@@ -13,6 +13,11 @@ type Lines []Line
type Line struct {
Through []point.ID `yaml:"through"`
Curve *Curve `yaml:"curve,omitempty"`
Style *Style `yaml:"style,omitempty"`
}

type Style struct {
Thickness *float64 `yaml:"thickness,omitempty"`
}

// Curve describes if a Line curves and if it has start and end constraints.
@@ -32,9 +37,20 @@ func (l Line) Build(pat *pattern.Pattern) error {
case l.Curve != nil:
startPoint := pat.GetPoint(l.Curve.Start)
endPoint := pat.GetPoint(l.Curve.End)
pat.AddLine(path.NewSpline(startPoint, endPoint, points...))
spline := path.NewSpline(startPoint, endPoint, points...)

if l.Style != nil && l.Style.Thickness != nil {
spline.SetThickness(*l.Style.Thickness)
}

pat.AddLine(spline)
default:
pat.AddLine(path.NewPath(points...))
newPath := path.NewPath(points...)
if l.Style != nil && l.Style.Thickness != nil {
newPath.SetThickness(*l.Style.Thickness)
}

pat.AddLine(newPath)
}

return nil


+ 5
- 0
spec/pattern.yaml Dosyayı Görüntüle

@@ -97,6 +97,11 @@ components:
$ref: '#/components/schemas/pointID'
end:
$ref: '#/components/schemas/pointID'
style:
type: object
properties:
thickness:
type: number
pointID:
oneOf:
- type: integer


Yükleniyor…
İptal
Kaydet