Styling

Styling

Paths can be styled with colors, stroke widths, pens, dashes, and arrows.

Colors

stroke(color)

Sets the stroke color.

:stroke("red")
:stroke("blue")
:stroke("#ff6600")

fill(color)

Sets the fill color.

:fill("yellow")

Color Formats

Colors can be specified as:

  • CSS color names: "red", "blue", "darkgreen"
  • Hex strings: "#ff0000", "#0066cc"
  • RGB function: h.rgb(r, g, b) with values 0-1
  • Gray function: h.gray(value) with value 0-1
local red = h.rgb(1, 0, 0)
local darkgray = h.gray(0.3)
local css_color = h.color("steelblue")

:stroke(red)
:fill(darkgray)

Stroke Width

strokewidth(w)

Sets the stroke width in points.

:strokewidth(2)
:strokewidth(0.5)

Line Caps and Joins

linecap(style)

Sets how line ends are drawn: "butt", "round", "square".

:linecap("round")

linejoin(style)

Sets how line corners are drawn: "miter", "round", "bevel".

:linejoin("round")

Pens

Pens define the shape of the stroke.

pencircle(size)

A circular pen (default).

:pen(h.pencircle(4))

pensquare(size)

A square pen, useful for calligraphic effects.

:pen(h.pensquare(3))

penrazor()

A line pen (zero-width in one direction).

:pen(h.penrazor())

penspeck()

A point pen (zero-dimensional).

:pen(h.penspeck())

Dash Patterns

evenly()

Standard dashed line.

:evenly()
:stroke("black")

withdots()

Dotted line.

:withdots()
:stroke("black")

dash(pattern)

Custom dash pattern.

:dash(h.dashed(8, 2))     -- 8pt dash, 2pt gap
:dash(h.evenly():scaled(2))  -- scaled evenly pattern

Dash patterns can be transformed:

local pattern = h.evenly():scaled(2):shifted(1)
:dash(pattern)

Arrows

arrow()

Adds an arrowhead at the end of the path.

:arrow()
:stroke("black")

dblarrow()

Adds arrowheads at both ends.

:dblarrow()
:stroke("black")

arrowstyle(length, angle)

Customizes arrow appearance.

:arrowstyle(8, 30)   -- length 8, angle 30 degrees
:arrow()

Complete Example

local h = require("hobby")

-- Solid stroke
local line1 = h.path()
    :moveto(h.point(0, 0))
    :lineto(h.point(100, 0))
    :stroke("black")
    :build()

-- Thick colored stroke
local line2 = h.path()
    :moveto(h.point(0, 20))
    :lineto(h.point(100, 20))
    :pen(h.pencircle(4))
    :stroke("blue")
    :build()

-- Square pen
local line3 = h.path()
    :moveto(h.point(0, 40))
    :lineto(h.point(100, 40))
    :pen(h.pensquare(3))
    :stroke("red")
    :build()

-- Dashed
local line4 = h.path()
    :moveto(h.point(0, 60))
    :lineto(h.point(100, 60))
    :evenly()
    :stroke("green")
    :build()

-- Dotted
local line5 = h.path()
    :moveto(h.point(0, 80))
    :lineto(h.point(100, 80))
    :withdots()
    :stroke("purple")
    :build()

-- Arrow
local arrow = h.path()
    :moveto(h.point(0, 100))
    :lineto(h.point(100, 100))
    :arrow()
    :stroke("black")
    :build()

h.svg()
    :padding(10)
    :add(line1)
    :add(line2)
    :add(line3)
    :add(line4)
    :add(line5)
    :add(arrow)
    :write("styling.svg")

Styling example