Class: Inkcite::Renderer::SpecialEffect::EffectContext

Inherits:
Object
  • Object
show all
Defined in:
lib/inkcite/renderer/special_effect.rb

Overview

A convenience class for accessing the attributes that are common to special effects like snow and sparkle.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, opt, ctx, defaults = {}) ⇒ EffectContext

Returns a new instance of EffectContext.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/inkcite/renderer/special_effect.rb', line 20

def initialize tag, opt, ctx, defaults={}

  @tag = tag

  # Merge the provided opts over the defaults to ensure the values
  # provided by the developer take precedence.
  @opt = defaults.merge(opt)
  @ctx = ctx

  # Request a unique ID for this special effect which will be used
  # to uniquely identify the classes and animations associated with
  # this special effect.
  @uuid = ctx.unique_id(:sfx)

end

Instance Attribute Details

#ctxObject (readonly)

Expose the opt and ctx attributes



12
13
14
# File 'lib/inkcite/renderer/special_effect.rb', line 12

def ctx
  @ctx
end

#optObject (readonly)

Expose the opt and ctx attributes



12
13
14
# File 'lib/inkcite/renderer/special_effect.rb', line 12

def opt
  @opt
end

#uuidObject (readonly)

Returns the value of attribute uuid.



9
10
11
# File 'lib/inkcite/renderer/special_effect.rb', line 9

def uuid
  @uuid
end

Instance Method Details

#all_children_class_nameObject



36
37
38
# File 'lib/inkcite/renderer/special_effect.rb', line 36

def all_children_class_name
  obfuscate_class_names? ? "sfx#{@uuid}c" : "#{@tag}#{@uuid}-children"
end

#animation_class_name(child_index) ⇒ Object



40
41
42
# File 'lib/inkcite/renderer/special_effect.rb', line 40

def animation_class_name child_index
  obfuscate_class_names? ? "#{@tag}#{@uuid}-anim#{child_index + 1}" : "sfx#{@uuid}a#{child_index + 1}"
end

#child_class_name(child_index) ⇒ Object



44
45
46
# File 'lib/inkcite/renderer/special_effect.rb', line 44

def child_class_name child_index
  obfuscate_class_names? ? "sfx#{@uuid}c#{child_index + 1}" : "#{@tag}#{@uuid}-child#{child_index + 1}"
end

#colorObject



48
49
50
# File 'lib/inkcite/renderer/special_effect.rb', line 48

def color
  Renderer.hex(@opt[:color])
end

#countObject

Returns the number of children in the special effects - e.g. the number of snowflakes or sparkles in the animation. :flakes and :sparks are technically deprecated.



55
56
57
# File 'lib/inkcite/renderer/special_effect.rb', line 55

def count
  (@opt[:sparks] || @opt[:flakes] || @opt[:count]).to_i
end

#equal_distribution(qty) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/inkcite/renderer/special_effect.rb', line 59

def equal_distribution qty

  # Space the children generally equally across the width of the
  # container div.  Random distribution sometimes ends up with
  # children clumped at one edge or the other.
  spacing = POSITION_CEIL / qty.to_f

  # Now build up a pool of equally-spaced starting positions.
  # TODO: This is probably a perfect spot to use inject()
  start_left = spacing / 2.0

  # This array will hold all of the positions
  positions = [start_left]

  # Now, for the remaining positions needed, adjust by the
  # spacing and push onto the list.
  (qty - 1).times { |f| positions << start_left += spacing }

  positions
end

#heightObject



80
81
82
# File 'lib/inkcite/renderer/special_effect.rb', line 80

def height
  @opt[:height].to_i
end

#max_opacityObject



84
85
86
# File 'lib/inkcite/renderer/special_effect.rb', line 84

def max_opacity
  @opt[OPACITY_MAX].to_f
end

#max_sizeObject



88
89
90
# File 'lib/inkcite/renderer/special_effect.rb', line 88

def max_size
  @opt[SIZE_MAX].to_i
end

#max_speedObject



96
97
98
# File 'lib/inkcite/renderer/special_effect.rb', line 96

def max_speed
  @opt[SPEED_MAX].to_f
end

#min_opacityObject



92
93
94
# File 'lib/inkcite/renderer/special_effect.rb', line 92

def min_opacity
  @opt[OPACITY_MIN].to_f
end

#min_sizeObject



100
101
102
# File 'lib/inkcite/renderer/special_effect.rb', line 100

def min_size
  @opt[SIZE_MIN].to_i
end

#min_speedObject



104
105
106
# File 'lib/inkcite/renderer/special_effect.rb', line 104

def min_speed
  @opt[SPEED_MIN].to_f
end

#obfuscate_class_names?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/inkcite/renderer/special_effect.rb', line 108

def obfuscate_class_names?
  false && @ctx.production?
end

#opacity_rangeObject



112
113
114
# File 'lib/inkcite/renderer/special_effect.rb', line 112

def opacity_range
  (min_opacity..max_opacity)
end

#positions_xObject

Creates a permanent list of positions (as percentages of the wrap container’s total width) which can be used for starting or ending position to equally space animated elements.



165
166
167
# File 'lib/inkcite/renderer/special_effect.rb', line 165

def positions_x
  @positions_x ||= equal_distribution(count)
end

#positions_yObject

Creates a permanent list of positions (as percentages of the wrap container’s total height) which can be used for starting or ending position to equally space animated elements.



172
173
174
# File 'lib/inkcite/renderer/special_effect.rb', line 172

def positions_y
  @positions_y ||= equal_distribution(count)
end

#rand_opacityObject



116
117
118
# File 'lib/inkcite/renderer/special_effect.rb', line 116

def rand_opacity
  rand(opacity_range).round(1)
end

#rand_rotationObject



120
121
122
# File 'lib/inkcite/renderer/special_effect.rb', line 120

def rand_rotation
  rand(rotation_range).round(1)
end

#rand_sizeObject



124
125
126
# File 'lib/inkcite/renderer/special_effect.rb', line 124

def rand_size
  rand(size_range).round(0)
end

#rand_speedObject



128
129
130
# File 'lib/inkcite/renderer/special_effect.rb', line 128

def rand_speed
  rand(speed_range).round(1)
end

#rotation?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/inkcite/renderer/special_effect.rb', line 132

def rotation?
  @opt[:rotate] || @opt[:rotation]
end

#rotation_rangeObject



136
137
138
# File 'lib/inkcite/renderer/special_effect.rb', line 136

def rotation_range
  (-270..270)
end

#size_rangeObject



140
141
142
# File 'lib/inkcite/renderer/special_effect.rb', line 140

def size_range
  (min_size..max_size)
end

#speed_rangeObject



144
145
146
# File 'lib/inkcite/renderer/special_effect.rb', line 144

def speed_range
  (min_speed..max_speed)
end

#srcObject



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/inkcite/renderer/special_effect.rb', line 148

def src
  return @src if defined?(@src)

  # Check to see if a source image has been specified for the snowflakes.
  @src = @opt[:src]

  # Release the image name if one has been provided but doesn't exist in
  # the project - this will cause the special effect to default to the
  # non-image default behavior.
  @src = nil if @src && !@ctx.assert_image_exists(@src)

  @src
end

#start_time(child_index) ⇒ Object



176
177
178
# File 'lib/inkcite/renderer/special_effect.rb', line 176

def start_time child_index
  ((time / count) * child_index).round(1)
end

#timeObject



180
181
182
# File 'lib/inkcite/renderer/special_effect.rb', line 180

def time
  @opt[:time].to_f
end

#time_intervalObject

Amount of time between each child starting its animation to create a even but varied distribution.



186
187
188
# File 'lib/inkcite/renderer/special_effect.rb', line 186

def time_interval
  time / count.to_f
end

#webkit_only?Boolean

Returns true if CSS animations should be limited to webkit- powered email clients.

Returns:

  • (Boolean)


192
193
194
# File 'lib/inkcite/renderer/special_effect.rb', line 192

def webkit_only?
  !(@ctx.development? || @ctx.browser?)
end

#wrap_class_nameObject



196
197
198
# File 'lib/inkcite/renderer/special_effect.rb', line 196

def wrap_class_name
  obfuscate_class_names? ? "sfx#{@uuid}w" : "#{@tag}#{@uuid}-wrap"
end