Class: Pixelart::Vector::Path

Inherits:
Shape
  • Object
show all
Defined in:
lib/pixelart/vector.rb

Overview

class Circle

Instance Method Summary collapse

Constructor Details

#initialize(fill:, stroke:) ⇒ Path

Returns a new instance of Path.



29
30
31
32
33
# File 'lib/pixelart/vector.rb', line 29

def initialize( fill:, stroke: )
  @commands = []
  @fill     = fill
  @stroke   = stroke
end

Instance Method Details

#line(*coords) ⇒ Object

move_to/line_to all-in-one shortcut



45
46
47
48
49
50
51
52
# File 'lib/pixelart/vector.rb', line 45

def line( *coords )  ## move_to/line_to all-in-one shortcut
  ## todo/check - assert coords has at least two x/y pairs - why? why not?
  move_to( *coords[0..1] )
  coords[2..-1].each_slice( 2) do |coord|
    line_to( *coord )
  end
  self
end

#line_to(x, y) ⇒ Object



40
41
42
43
# File 'lib/pixelart/vector.rb', line 40

def line_to( x, y )
  @commands << ['L', x, y]
  self
end

#move_to(x, y) ⇒ Object

add a move_to instruction



35
36
37
38
# File 'lib/pixelart/vector.rb', line 35

def move_to( x, y )   ## add a move_to instruction
  @commands << ['M', x, y]
  self
end

#to_svgObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pixelart/vector.rb', line 56

def to_svg
   buf =  %Q{<path style="stroke: #{@stroke}; fill: #{@fill || 'none'};" }
   buf << %Q{d="}
   last_command = ''
   @commands.each_with_index do |command,i|
      buf << " "  if i > 0  # add space separator

      ## "optimize" - that is, do not repead command if same as before
      buf << command[0]   if command[0] != last_command
      buf << "#{command[1]} #{command[2]}"
      last_command = command[0]
   end

   buf << %Q{"}
   buf << "/>"
   buf
end