Module: Sevgi::Graphics::Mixtures::Tile

Defined in:
lib/sevgi/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.

See Also:

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) { ... } ⇒ Sevgi::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) }
Sevgi::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 may mutate the element and 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:

  • (Sevgi::ArgumentError)

    when a required tile argument is missing or invalid



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 'lib/sevgi/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) { ... } ⇒ Sevgi::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 may mutate the element and 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:

  • (Sevgi::ArgumentError)

    when a required tile argument is missing or invalid



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sevgi/graphics/mixtures/tile.rb', line 91

def TileX(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil, &block)
  id, n, d, o, callback = Helper.normalize(id:, n:, d:, o:, proc:).values_at(:id, :n, :d, :o, :proc)

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

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

      element = use(
        id: [id, x + 1].join("-"),
        href: "##{id}",
        class: cs.join(" "),
        **Helper.coordinates(x: Scalar.number((x * d) + o, context: "tile", field: :x))
      )
      callback&.call(element, x:, n:)
    end
  end
end

#TileY(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil) { ... } ⇒ Sevgi::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 may mutate the element and 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:

  • (Sevgi::ArgumentError)

    when a required tile argument is missing or invalid



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sevgi/graphics/mixtures/tile.rb', line 125

def TileY(id = Undefined, n: Undefined, d: Undefined, o: 0, proc: nil, &block)
  id, n, d, o, callback = Helper.normalize(id:, n:, d:, o:, proc:).values_at(:id, :n, :d, :o, :proc)

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

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

      element = use(
        id: [id, y + 1].join("-"),
        href: "##{id}",
        class: rs.join(" "),
        **Helper.coordinates(y: Scalar.number((y * d) + o, context: "tile", field: :y))
      )
      callback&.call(element, y:, n:)
    end
  end
end