Class: VectorSalad::StandardShapes::Custom

Inherits:
BasicShape
  • Object
show all
Defined in:
lib/vector_salad/standard_shapes/custom.rb

Overview

Make your own custom shape.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Any) initialize(name, **options, &block)

You must name your custom shape to be able to use it later. The block passed to custom shape is what will create your shape. If you create just 1 shape (e.g. with MultiPath or one of the VectorSalad::StandardShapes::Clip operations which return a MultiPath) then that's what's added to the canvas, but if you create multiple shapes they will be wrapped in a Group.

Examples:

Using DSL:

custom(:donut) do |size| # name shape and specify parameters
  difference do # return a single shape
    circle(size / 2)
    circle(size / 3)
  end
end

donut(100)

Parameters:

  • name (Symbol)

    The name of your shape in snake_case

  • options ({})

    +{}+

  • block (Proc)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vector_salad/standard_shapes/custom.rb', line 31

def initialize(name, **options, &block)
  const = name.to_s.capitalize.to_sym
  ::VectorSalad::StandardShapes.const_set(const, Class.new(Custom) do
    include VectorSalad::DSL
    include VectorSalad::StandardShapes

    define_method(:initialize, &block)

    def canvas
      @canvas ||= VectorSalad::Canvas.new
    end

    def to_path
      if canvas.length == 1
        canvas[0]
      else
        Group.new.tap(&:canvas=.(@canvas))
      end
    end
  end)
end

Instance Attribute Details

- (Object) options Originally defined in class BasicShape

Returns the value of attribute options