Class: VectorSalad::StandardShapes::Clip

Inherits:
BasicShape
  • Object
show all
Includes:
VectorSalad::StandardShapes
Defined in:
lib/vector_salad/standard_shapes/clip.rb

Overview

Perform a clipping operation on the contained paths or shapes. It's easier to use one of the subclasses; (see Difference, Intersection, Union, Exclusion).

Direct Known Subclasses

Difference, Exclusion, Intersection, Union

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (MultiPath) initialize(operation, **options, &block)

The first path is used as the subject, subsequent paths are applied to the first using the specified operation.

Parameters:

  • operation (Or[*%i(difference intersection union xor)])
  • options ({})

    +{}+

  • block (Proc)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vector_salad/standard_shapes/clip.rb', line 20

def initialize(operation, **options, &block)
  instance_eval(&block) # canvas is populated

  clipper = Clipper::Clipper.new

  i = 0
  canvas.each do |shape|
    method = i == 0 ? "subject" : "clip"
    path = shape.to_simple_path.to_a
    if path[0][0].instance_of? Array # MultiPath
      clipper.send("add_#{method}_polygons".to_sym, path)
    else # Path
      clipper.send("add_#{method}_polygon".to_sym, path)
    end
    i += 1
  end

  @path = MultiPath.new(
    *clipper.send(operation, :non_zero, :non_zero), **options
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class VectorSalad::DSL

Instance Attribute Details

- (Object) options Originally defined in class BasicShape

Returns the value of attribute options

Instance Method Details

- (Object) canvas

The canvas the clipping is done on.



43
44
45
# File 'lib/vector_salad/standard_shapes/clip.rb', line 43

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

- (Object) to_path

Convert the shape to a path



48
49
50
# File 'lib/vector_salad/standard_shapes/clip.rb', line 48

def to_path
  @path
end