Class: MagicCloud::Canvas
- Inherits:
-
Object
- Object
- MagicCloud::Canvas
- Defined in:
- lib/magic_cloud/canvas.rb
Overview
Thin wrapper around RMagick, incapsulating ALL the real drawing. As it’s only class that “knows” about underlying graphics library, it should be possible to replace it with another canvas with same interface, not using RMagick.
Constant Summary collapse
- RADIANS =
Math::PI / 180
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#internal ⇒ Object
readonly
Returns the value of attribute internal.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #draw_text(text, options = {}) ⇒ Object
-
#initialize(w, h, back = 'transparent') ⇒ Canvas
constructor
A new instance of Canvas.
- #measure_text(text, options) ⇒ Object
- #pixels(x, y, w, h) ⇒ Object
-
#render ⇒ Object
rubocop:disable TrivialAccessors.
Constructor Details
#initialize(w, h, back = 'transparent') ⇒ Canvas
Returns a new instance of Canvas.
10 11 12 13 |
# File 'lib/magic_cloud/canvas.rb', line 10 def initialize(w, h, back = 'transparent') @width, @height = w, h @internal = Magick::Image.new(w, h){|i| i.background_color = back} end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
15 16 17 |
# File 'lib/magic_cloud/canvas.rb', line 15 def height @height end |
#internal ⇒ Object (readonly)
Returns the value of attribute internal.
15 16 17 |
# File 'lib/magic_cloud/canvas.rb', line 15 def internal @internal end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
15 16 17 |
# File 'lib/magic_cloud/canvas.rb', line 15 def width @width end |
Instance Method Details
#draw_text(text, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/magic_cloud/canvas.rb', line 19 def draw_text(text, = {}) draw = Magick::Draw.new # FIXME: is it necessary every time? x = .fetch(:x, 0) y = .fetch(:y, 0) rotate = .fetch(:rotate, 0) (draw, ) rect = _measure_text(draw, text, rotate) draw. translate(x + rect.width/2, y + rect.height/2). rotate(rotate). translate(0, rect.height/8). # RMagick text_align seems really weird text(0, 0, text). draw(@internal) rect end |
#measure_text(text, options) ⇒ Object
40 41 42 43 44 |
# File 'lib/magic_cloud/canvas.rb', line 40 def measure_text(text, ) draw = Magick::Draw.new (draw, ) _measure_text(draw, text, .fetch(:rotate, 0)) end |
#pixels(x, y, w, h) ⇒ Object
46 47 48 |
# File 'lib/magic_cloud/canvas.rb', line 46 def pixels(x, y, w, h) @internal.export_pixels(x, y, w, h, 'RGBA') end |
#render ⇒ Object
rubocop:disable TrivialAccessors
51 52 53 |
# File 'lib/magic_cloud/canvas.rb', line 51 def render @internal end |