Class: Inkcite::Renderer::SpecialEffect::EffectContext
- Inherits:
-
Object
- Object
- Inkcite::Renderer::SpecialEffect::EffectContext
- 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
-
#ctx ⇒ Object
readonly
Expose the opt and ctx attributes.
-
#opt ⇒ Object
readonly
Expose the opt and ctx attributes.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Instance Method Summary collapse
- #all_children_class_name ⇒ Object
- #animation_class_name(child_index) ⇒ Object
- #child_class_name(child_index) ⇒ Object
- #color ⇒ Object
-
#count ⇒ Object
Returns the number of children in the special effects - e.g.
- #equal_distribution(qty) ⇒ Object
- #height ⇒ Object
-
#initialize(tag, opt, ctx, defaults = {}) ⇒ EffectContext
constructor
A new instance of EffectContext.
- #max_opacity ⇒ Object
- #max_size ⇒ Object
- #max_speed ⇒ Object
- #min_opacity ⇒ Object
- #min_size ⇒ Object
- #min_speed ⇒ Object
- #obfuscate_class_names? ⇒ Boolean
- #opacity_range ⇒ Object
-
#positions_x ⇒ Object
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.
-
#positions_y ⇒ Object
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.
- #rand_opacity ⇒ Object
- #rand_rotation ⇒ Object
- #rand_size ⇒ Object
- #rand_speed ⇒ Object
- #rotation? ⇒ Boolean
- #rotation_range ⇒ Object
- #size_range ⇒ Object
- #speed_range ⇒ Object
- #src ⇒ Object
- #start_time(child_index) ⇒ Object
- #time ⇒ Object
-
#time_interval ⇒ Object
Amount of time between each child starting its animation to create a even but varied distribution.
-
#webkit_only? ⇒ Boolean
Returns true if CSS animations should be limited to webkit- powered email clients.
- #wrap_class_name ⇒ Object
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
#ctx ⇒ Object (readonly)
Expose the opt and ctx attributes
12 13 14 |
# File 'lib/inkcite/renderer/special_effect.rb', line 12 def ctx @ctx end |
#opt ⇒ Object (readonly)
Expose the opt and ctx attributes
12 13 14 |
# File 'lib/inkcite/renderer/special_effect.rb', line 12 def opt @opt end |
#uuid ⇒ Object (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_name ⇒ Object
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 |
#color ⇒ Object
48 49 50 |
# File 'lib/inkcite/renderer/special_effect.rb', line 48 def color Renderer.hex(@opt[:color]) end |
#count ⇒ Object
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 |
#height ⇒ Object
80 81 82 |
# File 'lib/inkcite/renderer/special_effect.rb', line 80 def height @opt[:height].to_i end |
#max_opacity ⇒ Object
84 85 86 |
# File 'lib/inkcite/renderer/special_effect.rb', line 84 def max_opacity @opt[OPACITY_MAX].to_f end |
#max_size ⇒ Object
88 89 90 |
# File 'lib/inkcite/renderer/special_effect.rb', line 88 def max_size @opt[SIZE_MAX].to_i end |
#max_speed ⇒ Object
96 97 98 |
# File 'lib/inkcite/renderer/special_effect.rb', line 96 def max_speed @opt[SPEED_MAX].to_f end |
#min_opacity ⇒ Object
92 93 94 |
# File 'lib/inkcite/renderer/special_effect.rb', line 92 def min_opacity @opt[OPACITY_MIN].to_f end |
#min_size ⇒ Object
100 101 102 |
# File 'lib/inkcite/renderer/special_effect.rb', line 100 def min_size @opt[SIZE_MIN].to_i end |
#min_speed ⇒ Object
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
108 109 110 |
# File 'lib/inkcite/renderer/special_effect.rb', line 108 def obfuscate_class_names? false && @ctx.production? end |
#opacity_range ⇒ Object
112 113 114 |
# File 'lib/inkcite/renderer/special_effect.rb', line 112 def opacity_range (min_opacity..max_opacity) end |
#positions_x ⇒ Object
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_y ⇒ Object
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_opacity ⇒ Object
116 117 118 |
# File 'lib/inkcite/renderer/special_effect.rb', line 116 def rand_opacity rand(opacity_range).round(1) end |
#rand_rotation ⇒ Object
120 121 122 |
# File 'lib/inkcite/renderer/special_effect.rb', line 120 def rand_rotation rand(rotation_range).round(1) end |
#rand_size ⇒ Object
124 125 126 |
# File 'lib/inkcite/renderer/special_effect.rb', line 124 def rand_size rand(size_range).round(0) end |
#rand_speed ⇒ Object
128 129 130 |
# File 'lib/inkcite/renderer/special_effect.rb', line 128 def rand_speed rand(speed_range).round(1) end |
#rotation? ⇒ Boolean
132 133 134 |
# File 'lib/inkcite/renderer/special_effect.rb', line 132 def rotation? @opt[:rotate] || @opt[:rotation] end |
#rotation_range ⇒ Object
136 137 138 |
# File 'lib/inkcite/renderer/special_effect.rb', line 136 def rotation_range (-270..270) end |
#size_range ⇒ Object
140 141 142 |
# File 'lib/inkcite/renderer/special_effect.rb', line 140 def size_range (min_size..max_size) end |
#speed_range ⇒ Object
144 145 146 |
# File 'lib/inkcite/renderer/special_effect.rb', line 144 def speed_range (min_speed..max_speed) end |
#src ⇒ Object
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 |
#time ⇒ Object
180 181 182 |
# File 'lib/inkcite/renderer/special_effect.rb', line 180 def time @opt[:time].to_f end |
#time_interval ⇒ Object
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.
192 193 194 |
# File 'lib/inkcite/renderer/special_effect.rb', line 192 def webkit_only? !(@ctx.development? || @ctx.browser?) end |
#wrap_class_name ⇒ Object
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 |