Module: Sumi::Graphics::Mixtures::Tile

Defined in:
graphics/lib/sumi/graphics/mixtures/tile.rb

Overview

DSL helpers for defining one SVG template and repeating it through use elements.

Use Sundries::Tile instead when Ruby code needs inspectable repeated geometry or row/column bounds rather than SVG references.

Constant Summary collapse

PREFIX =

Stable prefix used for generated tile CSS classes.

"tile"

Instance Method Summary collapse

Instance Method Details

#Tile(id = Undefined, nx: Undefined, dx: Undefined, ox: 0, ny: Undefined, dy: Undefined, oy: 0, proc: nil) { ... } ⇒ Sumi::Graphics::Element

Builds a two-dimensional tile grid. Each use id has the form id-row-column, with one-based row and column numbers. Generated classes identify the one-based row and column and mark their first and last positions. A block defines the referenced template as a group under defs before the uses are added.

Examples:

Define and customize a tile grid

customize = proc { |use, x:, y:, nx:, ny:| use[:opacity] = (x + y + 1).fdiv(nx + ny) }
Sumi::Graphics.SVG(:minimal) do
  Tile("dot", nx: 2, dx: 10, ny: 2, dy: 10, proc: customize) { circle r: 2 }
end

Parameters:

  • id (String) (defaults to: Undefined) —

    referenced template id

  • nx (Integer) (defaults to: Undefined) —

    number of columns

  • dx (Numeric) (defaults to: Undefined) —

    finite horizontal spacing, normalized before coordinates are rendered

  • ox (Numeric) (defaults to: 0) —

    finite horizontal offset, normalized before coordinates are rendered

  • ny (Integer) (defaults to: Undefined) —

    number of rows

  • dy (Numeric) (defaults to: Undefined) —

    finite vertical spacing, normalized before coordinates are rendered

  • oy (Numeric) (defaults to: 0) —

    finite vertical offset, normalized before coordinates are rendered

  • proc (Proc, nil) (defaults to: nil) —

    optional callback invoked for each use as (element, x:, y:, nx:, ny:), with zero-based coordinates and total counts. The callback can mutate the element. Its return value is ignored

Yields:

  • evaluates the template drawing DSL in a generated defs group named by id

Yield Returns:

  • (Object) —

    ignored block result

Returns:

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'graphics/lib/sumi/graphics/mixtures/tile.rb', line 38

def Tile(
  id = Undefined,
  nx: Undefined,
  dx: Undefined,
  ox: 0,
  ny: Undefined,
  dy: Undefined,
  oy: 0,
  proc: nil,
  &block
)
  id, nx, dx, ox, ny, dy, oy, callback = Helper
    .normalize(id:, nx:, dx:, ox:, ny:, dy:, oy:, proc:)
    .values_at(:id, :nx, :dx, :ox, :ny, :dy, :oy, :proc)

  defs { g(id:, &block) } if block

  Within do
    ny.times do |y|
      rs = Helper.classify(as: "row", index: y, upper: ny)

      nx.times do |x|
        cs = Helper.classify(as: "col", index: x, upper: nx)

        element = use(
          id: [id, y + 1, x + 1].join("-"),
          href: "##{id}",
          class: [*rs, *cs].join(" "),
          **Helper.coordinates(
            x: Scalar.number((x * dx) + ox, context: "tile", field: :x),
            y: Scalar.number((y * dy) + oy, context: "tile", field: :y)
          )
        )
        callback&.call(element, x:, y:, nx:, ny:)
      end
    end
  end
end

#TileX(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil) { ... } ⇒ Sumi::Graphics::Element

Builds a one-dimensional horizontal tile row. Each use id has the form id-column, with a one-based column number. Generated classes identify the column and mark its first and last positions. A block defines the referenced template as a group under defs before the uses are added.

Parameters:

  • id (String) (defaults to: Undefined) —

    referenced template id

  • n (Integer) (defaults to: Undefined) —

    number of instances

  • d (Numeric) (defaults to: Undefined) —

    finite horizontal spacing, normalized before coordinates are rendered

  • o (Numeric) (defaults to: 0) —

    finite horizontal offset, normalized before coordinates are rendered

  • proc (Proc, nil) (defaults to: nil) —

    optional callback invoked for each use as (element, x:, n:), with a zero-based column and total count. The callback can mutate the element. Its return value is ignored

Yields:

  • evaluates the template drawing DSL in a generated defs group named by id

Yield Returns:

  • (Object) —

    ignored block result

Returns:

Raises:



91
92
93
# File 'graphics/lib/sumi/graphics/mixtures/tile.rb', line 91

def TileX(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil, &block)
  Helper.axis(self, axis: :x, role: "col", id:, n:, d:, o:, callback: proc, &block)
end

#TileY(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil) { ... } ⇒ Sumi::Graphics::Element

Builds a one-dimensional vertical tile column. Each use id has the form id-row, with a one-based row number. Generated classes identify the row and mark its first and last positions. A block defines the referenced template as a group under defs before the uses are added.

Parameters:

  • id (String) (defaults to: Undefined) —

    referenced template id

  • n (Integer) (defaults to: Undefined) —

    number of instances

  • d (Numeric) (defaults to: Undefined) —

    finite vertical spacing, normalized before coordinates are rendered

  • o (Numeric) (defaults to: 0) —

    finite vertical offset, normalized before coordinates are rendered

  • proc (Proc, nil) (defaults to: nil) —

    optional callback invoked for each use as (element, y:, n:), with a zero-based row and total count. The callback can mutate the element. Its return value is ignored

Yields:

  • evaluates the template drawing DSL in a generated defs group named by id

Yield Returns:

  • (Object) —

    ignored block result

Returns:

Raises:



109
110
111
# File 'graphics/lib/sumi/graphics/mixtures/tile.rb', line 109

def TileY(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil, &block)
  Helper.axis(self, axis: :y, role: "row", id:, n:, d:, o:, callback: proc, &block)
end