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

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) ⇒ Object

Arc path segment.



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) ⇒ Object

Cubic bezier curve segment.



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) ⇒ Object

Canvas image shape.



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) ⇒ Object

Canvas SVG shape.



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) ⇒ Object

Text shape (canvas context).



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) ⇒ Object

Circle shape.



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) ⇒ Object

Clipping rectangle value.



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)

.closeObject

Close the current path.



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) ⇒ Object

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



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) ⇒ Object

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.



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) ⇒ Object

Line shape.



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) ⇒ Object

Line segment to the given point.



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

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

.linear_gradient(from, to, stops) ⇒ Object

Linear gradient descriptor. Returns a typed LinearGradient struct.



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) ⇒ Object

-- Path commands --------------------------------------------------------



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) ⇒ Object

Arbitrary path shape built from path commands.



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) ⇒ Object

Quadratic bezier curve segment.



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) ⇒ Object

Rectangle shape.



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) ⇒ Object

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)



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) ⇒ Object

Scale the coordinate system (independent axes).



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) ⇒ Object

Uniform scale convenience constructor.



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

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

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

Stroke descriptor. Returns a typed Stroke struct.



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) ⇒ Object

Translate the coordinate origin.



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

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