Blendend.Canvas.Fill (blendend v0.2.0)

View Source

Fill functions for Blendend.Canvas to paint a geometry on the canvas with a style (color, gradient or pattern).

Prefer the Blendend.Draw macros; they call into this module under the hood.

use Blendend.Draw

draw 200, 200, "fill_example.png" do
  clear fill: rgb(240, 240, 240)

  rect 30, 30, 140, 140, fill: rgb(255, 80, 80)
  circle 100, 100, 40, fill: rgb(255, 255, 255), alpha: 0.7
end

Summary

Functions

Fills a box given by corner coordinates {x0, y0, x1, y1}.

Same as box/6, but returns the canvas and raises on error.

Fills multiple boxes in one call.

Same as box_array/3, but returns the canvas and raises on error.

Fills a chord (arc + straight line between its endpoints).

Same as chord/8, but returns the canvas and raises on error.

Fills a circle at (cx, cy) with radius r.

Same as circle/5, but returns the canvas and raises on error.

Fills an ellipse centered at (cx, cy) with radii rx and ry.

Same as ellipse/6, but returns the canvas and raises on error.

Fills path on canvas using the given style options.

Same as path/3, but returns the canvas and raises on error.

Same as pie/8, but returns the canvas and raises on error.

Fills a polygon from a list of {x, y} points.

Same as polygon/3, but returns the canvas and raises on error.

Fills an axis–aligned rectangle (x, y, w, h).

Same as rect/6, but returns the canvas and raises on error.

Fills multiple rectangles in one call.

Same as rect_array/3, but returns the canvas and raises on error.

Fills a rounded rectangle (x, y, w, h) with corner radii (rx, ry).

Same as round_rect/8, but returns the canvas and raises on error.

Fills a triangle specified by its three vertices.

Same as triangle/8, but returns the canvas and raises on error.

Fills a UTF-8 text string on canvas using a font.

Same as utf8_text/6, but returns the canvas and raises on error.

Types

canvas()

@type canvas() :: Blendend.Canvas.t()

opts()

@type opts() :: keyword()

Functions

box(canvas, x0, y0, x1, y1, opts \\ [])

@spec box(canvas(), number(), number(), number(), number(), opts()) ::
  :ok | {:error, term()}

Fills a box given by corner coordinates {x0, y0, x1, y1}.

Uses the same style options as path/3.

box!(canvas, x0, y0, x1, y1, opts \\ [])

@spec box!(canvas(), number(), number(), number(), number(), opts()) :: canvas()

Same as box/6, but returns the canvas and raises on error.

On success, returns canvas.

On failure, raises Blendend.Error.

box_array(canvas, boxes, opts \\ [])

@spec box_array(canvas(), [{number(), number(), number(), number()}], opts()) ::
  :ok | {:error, term()}

Fills multiple boxes in one call.

boxes is a list of {x0, y0, x1, y1} tuples.

Uses the same style options as path/3.

box_array!(canvas, boxes, opts \\ [])

@spec box_array!(canvas(), [{number(), number(), number(), number()}], opts()) ::
  canvas()

Same as box_array/3, but returns the canvas and raises on error.

chord(canvas, cx, cy, rx, ry, start_angle, sweep_angle, opts \\ [])

@spec chord(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) :: :ok | {:error, term()}

Fills a chord (arc + straight line between its endpoints).

Parameters describe an ellipse centered at (cx, cy) with radii rx, ry and angles start_angle / sweep_angle in radians.

Uses the same style options as path/3.

chord!(canvas, cx, cy, rx, ry, start_angle, sweep_angle, opts \\ [])

@spec chord!(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) :: canvas()

Same as chord/8, but returns the canvas and raises on error.

circle(canvas, cx, cy, r, opts \\ [])

@spec circle(canvas(), number(), number(), number(), opts()) :: :ok | {:error, term()}

Fills a circle at (cx, cy) with radius r.

Uses the same style options as path/3.

circle!(canvas, cx, cy, r, opts \\ [])

@spec circle!(canvas(), number(), number(), number(), opts()) :: canvas()

Same as circle/5, but returns the canvas and raises on error.

On success, returns canvas.

On failure, raises Blendend.Error.

ellipse(canvas, cx, cy, rx, ry, opts \\ [])

@spec ellipse(canvas(), number(), number(), number(), number(), opts()) ::
  :ok | {:error, term()}

Fills an ellipse centered at (cx, cy) with radii rx and ry.

Uses the same style options as path/3.

ellipse!(canvas, cx, cy, rx, ry, opts \\ [])

@spec ellipse!(canvas(), number(), number(), number(), number(), opts()) :: canvas()

Same as ellipse/6, but returns the canvas and raises on error.

path(canvas, path, opts \\ [])

@spec path(canvas(), Blendend.Path.t(), opts()) :: :ok | {:error, term()}

Fills path on canvas using the given style options.

path must be a Blendend.Path.t().

Style options

We normally provide exactly one brush with :fill and optionally decorate it with :alpha and/or :comp_op. If no brush is provided

The opts keyword list controls the fill style. The style layer currently understands:

  • :fill:
    – solid brush, created with Blendend.Style.Color.* – gradient brush, created with Blendend.Style.Gradient.* – image pattern, created with Blendend.Style.Pattern.create/1
  • :alpha – extra opacity multiplier (values are 0.0..1.0)
  • :comp_op – compositing operator atom
    • :src_over (default)
    • :src_copy
    • :src_in
    • :src_out
    • :dst_over
    • :dst_copy
    • :dst_in
    • :dst_out
    • :dst_atop
    • :difference
    • :multiply
    • :screen
    • :overlay
    • :xor
    • :clear
    • :plus
    • :minus
    • :modulate
    • :darken
    • :lighten
    • :color_dodge
    • :color_burn
    • :linear_burn
    • :pin_light
    • :hard_light
    • :soft_light
    • :exclusion Those terms and the mathematics behind them are clearly explained on this page: Advanced compositing features

Examples

alias Blendend.{Canvas, Path, Style}
alias Blendend.Canvas.Fill
canvas = Canvas.new!(200, 200)
path =
  Path.new!()
  |> Path.move_to!(10, 10)
  |> Path.line_to!(190, 10)
  |> Path.line_to!(100, 180)
  |> Path.close!()
:ok =
  Fill.path(canvas, path,
    fill: Style.color(255, 0, 0),
    alpha: 0.9,
    comp_op: :src_over)

On success, returns :ok.

On failure, returns {:error, reason} from the NIF.

path!(canvas, path, opts \\ [])

@spec path!(canvas(), Blendend.Path.t(), opts()) :: canvas()

Same as path/3, but returns the canvas and raises on error.

On success, returns canvas.

On failure, raises Blendend.Error.

pie(canvas, cx, cy, rx, ry, start_angle, sweep_angle, opts \\ [])

@spec pie(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) :: :ok | {:error, term()}

Fills a pie/sector shape.

Same parameters as chord/8, but the arc is also connected back to the ellipse center.

Uses the same style options as path/3.

pie!(canvas, cx, cy, rx, ry, start_angle, sweep_angle, opts \\ [])

@spec pie!(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) :: canvas()

Same as pie/8, but returns the canvas and raises on error.

polygon(canvas, points, opts \\ [])

@spec polygon(canvas(), [{number(), number()}], opts()) :: :ok | {:error, term()}

Fills a polygon from a list of {x, y} points.

Uses the same style options as path/3.

polygon!(canvas, points, opts \\ [])

@spec polygon!(canvas(), [{number(), number()}], opts()) :: canvas()

Same as polygon/3, but returns the canvas and raises on error.

rect(canvas, x, y, w, h, opts \\ [])

@spec rect(canvas(), number(), number(), number(), number(), opts()) ::
  :ok | {:error, term()}

Fills an axis–aligned rectangle (x, y, w, h).

(x, y) is the top–left corner; w and h are width and height.

Uses the same style options as path/3.

rect!(canvas, x, y, w, h, opts \\ [])

@spec rect!(canvas(), number(), number(), number(), number(), opts()) :: canvas()

Same as rect/6, but returns the canvas and raises on error.

On success, returns canvas.

On failure, raises Blendend.Error.

rect_array(canvas, rects, opts \\ [])

@spec rect_array(canvas(), [{number(), number(), number(), number()}], opts()) ::
  :ok | {:error, term()}

Fills multiple rectangles in one call.

rects is a list of {x, y, w, h} tuples.

Uses the same style options as path/3.

rect_array!(canvas, rects, opts \\ [])

@spec rect_array!(canvas(), [{number(), number(), number(), number()}], opts()) ::
  canvas()

Same as rect_array/3, but returns the canvas and raises on error.

round_rect(canvas, x, y, w, h, rx, ry, opts \\ [])

@spec round_rect(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) ::
  :ok | {:error, term()}

Fills a rounded rectangle (x, y, w, h) with corner radii (rx, ry).

Uses the same style options as path/3.

round_rect!(canvas, x, y, w, h, rx, ry, opts \\ [])

@spec round_rect!(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) :: canvas()

Same as round_rect/8, but returns the canvas and raises on error.

triangle(canvas, x0, y0, x1, y1, x2, y2, opts \\ [])

@spec triangle(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) ::
  :ok | {:error, term()}

Fills a triangle specified by its three vertices.

Uses the same style options as path/3.

triangle!(canvas, x0, y0, x1, y1, x2, y2, opts \\ [])

@spec triangle!(
  canvas(),
  number(),
  number(),
  number(),
  number(),
  number(),
  number(),
  opts()
) :: canvas()

Same as triangle/8, but returns the canvas and raises on error.

utf8_text(canvas, font, x, y, text, opts \\ [])

@spec utf8_text(
  canvas(),
  Blendend.Text.Font.t(),
  number(),
  number(),
  String.t(),
  opts()
) :: :ok | {:error, term()}

Fills a UTF-8 text string on canvas using a font.

Draws the text with its origin at (x, y) in the current canvas transform.

opts is the same style keyword list used by path/3 (for example :color, :gradient, :alpha, :comp_op).

On success, returns :ok.

On failure, returns {:error, reason}.

utf8_text!(canvas, font, x, y, text, opts \\ [])

@spec utf8_text!(
  canvas(),
  Blendend.Text.Font.t(),
  number(),
  number(),
  String.t(),
  opts()
) :: canvas()

Same as utf8_text/6, but returns the canvas and raises on error.

On success, returns canvas.

On failure, raises Blendend.Error.