Module: Plushie::Canvas::Shape

Defined in:
lib/plushie/canvas/shape.rb,
lib/plushie/canvas/shape/clip.rb,
lib/plushie/canvas/shape/dash.rb,
lib/plushie/canvas/shape/line.rb,
lib/plushie/canvas/shape/path.rb,
lib/plushie/canvas/shape/rect.rb,
lib/plushie/canvas/shape/group.rb,
lib/plushie/canvas/shape/circle.rb,
lib/plushie/canvas/shape/stroke.rb,
lib/plushie/canvas/shape/hit_rect.rb,
lib/plushie/canvas/shape/transform.rb,
lib/plushie/canvas/shape/canvas_svg.rb,
lib/plushie/canvas/shape/canvas_text.rb,
lib/plushie/canvas/shape/drag_bounds.rb,
lib/plushie/canvas/shape/shape_style.rb,
lib/plushie/canvas/shape/canvas_image.rb,
lib/plushie/canvas/shape/linear_gradient.rb,
sig/plushie/canvas/shape.rbs

Overview

Pure builder functions for canvas shape descriptors.

Returns typed Data structs with #to_wire methods for wire transport. Use these inside canvas DSL blocks or pass to Widget::Canvas#set_shapes / #add_layer.

Defined Under Namespace

Classes: CanvasImage, CanvasSvg, CanvasText, Circle, Clip, Dash, DragBounds, Group, HitRect, Line, LinearGradient, Path, Rect, Rotate, Scale, ShapeStyle, Stroke, Translate

Class Method Summary collapse

Class Method Details

.arc(cx, cy, r, start_angle, end_angle) ⇒ Array[untyped]

Arc path segment.

Parameters:

  • cx (Numeric)
  • cy (Numeric)
  • r (Numeric)
  • start_angle (Numeric)
  • end_angle (Numeric)

Returns:

  • (Array[untyped])


124
125
126
# File 'lib/plushie/canvas/shape.rb', line 124

def arc(cx, cy, r, start_angle, end_angle)
  ["arc", cx, cy, r, start_angle, end_angle]
end

.bezier_to(cp1x, cp1y, cp2x, cp2y, x, y) ⇒ Array[untyped]

Cubic bezier curve segment.

Parameters:

  • cp1x (Numeric)
  • cp1y (Numeric)
  • cp2x (Numeric)
  • cp2y (Numeric)
  • x (Numeric)
  • y (Numeric)

Returns:

  • (Array[untyped])


114
115
116
# File 'lib/plushie/canvas/shape.rb', line 114

def bezier_to(cp1x, cp1y, cp2x, cp2y, x, y)
  ["bezier_to", cp1x, cp1y, cp2x, cp2y, x, y]
end

.canvas_image(source, x, y, w, h, **opts) ⇒ CanvasImage

Canvas image shape.

Parameters:

  • source (String)
  • x (Numeric)
  • y (Numeric)
  • w (Numeric)
  • h (Numeric)
  • opts (Object)

Returns:



57
58
59
# File 'lib/plushie/canvas/shape.rb', line 57

def canvas_image(source, x, y, w, h, **opts)
  CanvasImage.new(source: source, x: x, y: y, w: w, h: h, **opts)
end

.canvas_svg(source, x, y, w, h, **opts) ⇒ CanvasSvg

Canvas SVG shape.

Parameters:

  • source (String)
  • x (Numeric)
  • y (Numeric)
  • w (Numeric)
  • h (Numeric)
  • opts (Object)

Returns:



62
63
64
# File 'lib/plushie/canvas/shape.rb', line 62

def canvas_svg(source, x, y, w, h, **opts)
  CanvasSvg.new(source: source, x: x, y: y, w: w, h: h, **opts)
end

.canvas_text(x, y, content, **opts) ⇒ CanvasText

Text shape (canvas context).

Parameters:

  • x (Numeric)
  • y (Numeric)
  • content (String)
  • opts (Object)

Returns:



47
48
49
# File 'lib/plushie/canvas/shape.rb', line 47

def canvas_text(x, y, content, **opts)
  CanvasText.new(x: x, y: y, content: content, **opts)
end

.circle(x, y, r, **opts) ⇒ Circle

Circle shape.

Parameters:

  • x (Numeric)
  • y (Numeric)
  • r (Numeric)
  • opts (Object)

Returns:



37
38
39
# File 'lib/plushie/canvas/shape.rb', line 37

def circle(x, y, r, **opts)
  Circle.new(x: x, y: y, r: r, **opts)
end

.clip(x, y, w, h) ⇒ Clip

Clipping value constructor

Parameters:

  • x (Numeric)
  • y (Numeric)
  • w (Numeric)
  • h (Numeric)

Returns:



180
# File 'lib/plushie/canvas/shape.rb', line 180

def clip(x, y, w, h) = Clip.new(x: x, y: y, w: w, h: h)

.closeArray[String]

Close the current path.

Returns:

  • (Array[String])


111
# File 'lib/plushie/canvas/shape.rb', line 111

def close = ["close"]

.group(id_or_children = nil, children = nil, x: nil, y: nil, transforms: nil, **opts) ⇒ Group

Group of shapes. Accepts an optional id as first positional arg.

x: and y: kwargs are desugared into a leading Translate in the transforms array.

Parameters:

  • id_or_children (String, Array) (defaults to: nil)

    group id or children array

  • children (Array, nil) (defaults to: nil)

    children array when id is given

  • x: (Numeric, nil) (defaults to: nil)
  • y: (Numeric, nil) (defaults to: nil)
  • transforms: (Array[untyped], nil) (defaults to: nil)
  • opts (Object)

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/plushie/canvas/shape.rb', line 73

def group(id_or_children = nil, children = nil, x: nil, y: nil, transforms: nil, **opts)
  if id_or_children.is_a?(String)
    id = id_or_children
    children ||= []
  else
    id = opts.delete(:id)
    children = id_or_children || children || []
  end

  xforms = Array(transforms)
  xforms.unshift(Translate.new(x: x, y: y)) if x || y

  Group.new(
    children: children,
    transforms: xforms.empty? ? nil : xforms,
    id: id,
    **opts
  )
end

.interactive(shape, id, **opts) ⇒ Group

Wrap a shape with interactive properties. If the shape is a Group, merge the interactive fields directly. If it is a leaf shape, wrap it in a Group as the sole child.

Parameters:

  • shape (Object)
  • id (String)
  • opts (Object)

Returns:



96
97
98
99
100
101
102
# File 'lib/plushie/canvas/shape.rb', line 96

def interactive(shape, id, **opts)
  if shape.is_a?(Group)
    shape.with(id: id, **opts)
  else
    Group.new(children: [shape], id: id, **opts)
  end
end

.line(x1, y1, x2, y2, **opts) ⇒ Line

Line shape.

Parameters:

  • x1 (Numeric)
  • y1 (Numeric)
  • x2 (Numeric)
  • y2 (Numeric)
  • opts (Object)

Returns:



42
43
44
# File 'lib/plushie/canvas/shape.rb', line 42

def line(x1, y1, x2, y2, **opts)
  Line.new(x1: x1, y1: y1, x2: x2, y2: y2, **opts)
end

.line_to(x, y) ⇒ Array[untyped]

Line segment to the given point.

Parameters:

  • x (Numeric)
  • y (Numeric)

Returns:

  • (Array[untyped])


108
# File 'lib/plushie/canvas/shape.rb', line 108

def line_to(x, y) = ["line_to", x, y]

.linear_gradient(from, to, stops) ⇒ LinearGradient

Linear gradient descriptor. Returns a typed LinearGradient struct.

Parameters:

  • from (Array[Numeric])
  • to (Array[Numeric])
  • stops (Array[Array[untyped]])

Returns:



136
137
138
# File 'lib/plushie/canvas/shape.rb', line 136

def linear_gradient(from, to, stops)
  LinearGradient.new(from: from, to: to, stops: stops)
end

.move_to(x, y) ⇒ Array[untyped]

Path commands

Parameters:

  • x (Numeric)
  • y (Numeric)

Returns:

  • (Array[untyped])


106
107
# File 'lib/plushie/canvas/shape.rb', line 106

def move_to(x, y) = ["move_to", x, y]
# Line segment to the given point.

.path(commands, **opts) ⇒ Path

Arbitrary path shape built from path commands.

Parameters:

  • commands (Array[Array[untyped]])
  • opts (Object)

Returns:



52
53
54
# File 'lib/plushie/canvas/shape.rb', line 52

def path(commands, **opts)
  Path.new(commands: commands, **opts)
end

.quadratic_to(cpx, cpy, x, y) ⇒ Array[untyped]

Quadratic bezier curve segment.

Parameters:

  • cpx (Numeric)
  • cpy (Numeric)
  • x (Numeric)
  • y (Numeric)

Returns:

  • (Array[untyped])


119
120
121
# File 'lib/plushie/canvas/shape.rb', line 119

def quadratic_to(cpx, cpy, x, y)
  ["quadratic_to", cpx, cpy, x, y]
end

.rect(x, y, w, h, **opts) ⇒ Rect

Rectangle shape.

Parameters:

  • x (Numeric)
  • y (Numeric)
  • w (Numeric)
  • h (Numeric)
  • opts (Object)

Returns:



32
33
34
# File 'lib/plushie/canvas/shape.rb', line 32

def rect(x, y, w, h, **opts)
  Rect.new(x: x, y: y, w: w, h: h, **opts)
end

.rotate(angle = nil, degrees: nil, radians: nil) ⇒ Rotate

Rotate the coordinate system.

Accepts degrees by default (matching the Rust SDK convention). Use degrees: or radians: for explicit units.

rotate(45)              # 45 degrees
rotate(degrees: 45)     # explicit degrees
rotate(radians: 0.785)  # explicit radians (converted to degrees)

Parameters:

  • angle (Numeric, nil) (defaults to: nil)
  • degrees: (Numeric, nil) (defaults to: nil)
  • radians: (Numeric, nil) (defaults to: nil)

Returns:



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/plushie/canvas/shape.rb', line 153

def rotate(angle = nil, degrees: nil, radians: nil)
  if radians
    Rotate.new(angle: radians * 180.0 / Math::PI)
  elsif degrees
    Rotate.new(angle: degrees.to_f)
  elsif angle
    Rotate.new(angle: angle.to_f)
  else
    raise ArgumentError, "rotate requires an angle, degrees:, or radians:"
  end
end

.scale(x, y = nil) ⇒ Scale

Scale the coordinate system (independent axes).

Parameters:

  • x (Numeric)
  • y (Numeric, nil) (defaults to: nil)

Returns:



166
167
168
169
170
171
172
# File 'lib/plushie/canvas/shape.rb', line 166

def scale(x, y = nil)
  if y
    Scale.new(x: x, y: y)
  else
    Scale.new(factor: x)
  end
end

.scale_uniform(factor) ⇒ Scale

Uniform scale convenience constructor.

Parameters:

  • factor (Numeric)

Returns:



175
# File 'lib/plushie/canvas/shape.rb', line 175

def scale_uniform(factor) = Scale.new(factor: factor)

.stroke(color, width, **opts) ⇒ Stroke

Style helpers

Parameters:

  • color (Object)
  • width (Numeric)
  • opts (Object)

Returns:



131
132
133
# File 'lib/plushie/canvas/shape.rb', line 131

def stroke(color, width, **opts)
  Stroke.new(color: color, width: width, **opts)
end

.translate(x, y) ⇒ Translate

Transform value constructors

Parameters:

  • x (Numeric)
  • y (Numeric)

Returns:



143
# File 'lib/plushie/canvas/shape.rb', line 143

def translate(x, y) = Translate.new(x: x, y: y)