Class: RecursiveImageComposite

Inherits:
Object
  • Object
show all
Defined in:
lib/ordinals/recursive/composite.rb

Instance Method Summary collapse

Constructor Details

#initialize(cols, rows, width: 100, height: 100) ⇒ RecursiveImageComposite

Returns a new instance of RecursiveImageComposite.



6
7
8
9
10
11
12
13
14
15
# File 'lib/ordinals/recursive/composite.rb', line 6

def initialize( cols, rows, 
                width: 100, height: 100 )
   @tile_cols = cols
   @tile_rows = rows
 
   @tile_width  = width
   @tile_height = height

   @recursions = []
end

Instance Method Details

#add(obj) ⇒ Object Also known as: <<



26
# File 'lib/ordinals/recursive/composite.rb', line 26

def add( obj ) @recursions << obj; end

#countObject Also known as: size



18
# File 'lib/ordinals/recursive/composite.rb', line 18

def count() @recursions.size; end

#heightObject



24
# File 'lib/ordinals/recursive/composite.rb', line 24

def height() @tile_height*@tile_rows; end

#tile_heightObject



22
# File 'lib/ordinals/recursive/composite.rb', line 22

def tile_height() @tile_height; end

#tile_widthObject



21
# File 'lib/ordinals/recursive/composite.rb', line 21

def tile_width() @tile_width; end

#to_svgObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ordinals/recursive/composite.rb', line 29

def to_svg 
buf =<<TXT
<svg
xmlns="http://www.w3.org/2000/svg"
width="100%" height="100%"
viewBox="0 0 #{width} #{height}">
TXT

@recursions.each_with_index do |recursion,i|
  y,x = i.divmod( @tile_cols ) 

  if recursion.is_a?( RecursiveImage )
    comment  =  "#{i} @ (#{x}/#{y})"
buf += <<TXT 
<g transform="translate(#{x*@tile_width},#{y*@tile_height})">
 <!-- #{comment} -->
 #{recursion.to_svg( :inline )}
</g>
TXT
  else
    id, opts =  recursion.is_a?( Array )? recursion : [recursion, {}]

    pixelate = opts.has_key?(:pixelate) ? opts[:pixelate]
                                        : false
    comment  = opts.has_key?(:comment) ? opts[:comment]
                                       : "#{i} @ (#{x}/#{y})"
                          
  
    style = pixelate ? %Q[style="image-rendering: pixelated;"] : ''

buf += <<TXT 
<g transform="translate(#{x*@tile_width},#{y*@tile_height})">
 <!-- #{comment} -->
 <image
   width="#{@tile_width}" height="#{@tile_height}"
   href="/content/#{id}"
   #{style} />
</g>
TXT
  end
end


buf += <<TXT
</svg>
TXT

  buf
end

#widthObject



23
# File 'lib/ordinals/recursive/composite.rb', line 23

def width() @tile_width*@tile_cols; end