Class: GifCountdown::Generator
- Inherits:
-
Object
- Object
- GifCountdown::Generator
- Defined in:
- lib/gif_countdown/generator.rb
Instance Attribute Summary collapse
-
#font ⇒ Object
readonly
Returns the value of attribute font.
-
#fontsize ⇒ Object
readonly
Returns the value of attribute fontsize.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#padding_x ⇒ Object
readonly
Returns the value of attribute padding_x.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #annotate_with_countdown(image:, seconds:) ⇒ Object
- #call ⇒ Object
-
#initialize(distance_in_seconds:, width: 535, height: 61, fontsize: 30, padding_x: 10, duration: 60, font: 'Ubuntu') ⇒ Generator
constructor
A new instance of Generator.
- #pluralize(count, key) ⇒ Object
- #seconds_to_countdown(seconds) ⇒ Object
Constructor Details
#initialize(distance_in_seconds:, width: 535, height: 61, fontsize: 30, padding_x: 10, duration: 60, font: 'Ubuntu') ⇒ Generator
Returns a new instance of Generator.
6 7 8 9 10 11 12 13 14 |
# File 'lib/gif_countdown/generator.rb', line 6 def initialize(distance_in_seconds:, width: 535, height: 61, fontsize: 30, padding_x: 10, duration: 60, font: 'Ubuntu') @distance_in_seconds = distance_in_seconds @width = width @height = height @fontsize = fontsize @padding_x = padding_x @duration = duration @font = font end |
Instance Attribute Details
#font ⇒ Object (readonly)
Returns the value of attribute font.
4 5 6 |
# File 'lib/gif_countdown/generator.rb', line 4 def font @font end |
#fontsize ⇒ Object (readonly)
Returns the value of attribute fontsize.
4 5 6 |
# File 'lib/gif_countdown/generator.rb', line 4 def fontsize @fontsize end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/gif_countdown/generator.rb', line 4 def height @height end |
#padding_x ⇒ Object (readonly)
Returns the value of attribute padding_x.
4 5 6 |
# File 'lib/gif_countdown/generator.rb', line 4 def padding_x @padding_x end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/gif_countdown/generator.rb', line 4 def width @width end |
Instance Method Details
#annotate_with_countdown(image:, seconds:) ⇒ Object
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 78 79 80 81 82 |
# File 'lib/gif_countdown/generator.rb', line 34 def annotate_with_countdown(image:, seconds:) this = self draw = Magick::Draw.new draw.gravity = Magick::NorthGravity boxwidth = (width - padding_x * 5) / 4.0 parts = seconds_to_countdown(seconds) text_y = height / 2 + padding_x / 2 x = padding_x image.annotate(draw, boxwidth, height / 2, x, 0, parts[:days]) { |img| img.pointsize = this.fontsize img.font_family = this.font } image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:days], :days)) { |img| img.pointsize = (this.fontsize * 0.7).round img.font_family = this.font } x = padding_x * 2 + boxwidth image.annotate(draw, boxwidth, height / 2, x, 0, parts[:hours]) { |img| img.pointsize = this.fontsize img.font_family = this.font } image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:hours], :hours)) { |img| img.pointsize = (this.fontsize * 0.7).round img.font_family = this.font } x = padding_x * 3 + boxwidth * 2 image.annotate(draw, boxwidth, height / 2, x, 0, parts[:minutes]) { |img| img.pointsize = this.fontsize img.font_family = this.font } image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:minutes], :minutes)) { |img| img.pointsize = (this.fontsize * 0.7).round img.font_family = this.font } x = padding_x * 4 + boxwidth * 3 image.annotate(draw, boxwidth, height / 2, x, 0, parts[:seconds]) { |img| img.pointsize = this.fontsize img.font_family = this.font } image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:seconds], :seconds)) { |img| img.pointsize = (this.fontsize * 0.7).round img.font_family = this.font } end |
#call ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gif_countdown/generator.rb', line 16 def call gif = Magick::ImageList.new gif.delay = 100 gif.ticks_per_second = 100 @duration.times do |i| image = Magick::Image.new(width, height) image.delay = 100 annotate_with_countdown(image: image, seconds: @distance_in_seconds - i) gif << image end gif.to_blob { |img| img.format = 'gif' } end |
#pluralize(count, key) ⇒ Object
30 31 32 |
# File 'lib/gif_countdown/generator.rb', line 30 def pluralize(count, key) I18n.t("gif_countdown.#{key}", count: count) end |
#seconds_to_countdown(seconds) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gif_countdown/generator.rb', line 84 def seconds_to_countdown(seconds) days = seconds / 1.day seconds -= days * 1.day.to_i hours = seconds / 1.hour seconds -= hours * 1.hour.to_i minutes = seconds / 1.minute seconds -= minutes * 1.minute.to_i { days: sprintf("%02d", days), hours: sprintf("%02d", hours), minutes: sprintf("%02d", minutes), seconds: sprintf("%02d", seconds) } end |