Class: Compass::Magick::Canvas
- Inherits:
-
ChunkyPNG::Canvas
- Object
- ChunkyPNG::Canvas
- Compass::Magick::Canvas
- Includes:
- Scriptable, Utils
- Defined in:
- lib/magick/canvas.rb
Overview
A Canvas class that inherits ChunkyPNG::Canvas to represent the image as a matrix of pixels.
The canvas is constructed from a given width and height on a transparent background. The list of commands is executed in order and the resulting image is returned as a Base64 encoded PNG-24 Data URI.
Instance Attribute Summary
Attributes included from Scriptable
Instance Method Summary collapse
-
#initialize(*commands) ⇒ Canvas
constructor
Initializes a new Canvas instance.
-
#tile(width, height) ⇒ Canvas
Tile the Canvas to fill the region defined by ‘width` and `height`.
-
#to_bool ⇒ Boolean
‘true` (the Ruby boolean value).
-
#to_data_uri(options = {}) ⇒ String
(also: #to_s)
Serializes the Canvas as a Base64 encoded PNG-24 Data URI.
Methods included from Utils
#assert_one_of, #assert_type, #to_canvas, #to_chunky_color, #value_of
Constructor Details
#initialize(canvas, *commands) ⇒ Canvas #initialize(data, *commands) ⇒ Canvas #initialize(url, *commands) ⇒ Canvas #initialize(path, *commands) ⇒ Canvas #initialize(width, height, *commands) ⇒ Canvas
Initializes a new Canvas instance.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/magick/canvas.rb', line 47 def initialize(*commands) from_any(commands) list = [] commands.each do |command| if command.kind_of?(Sass::Script::List) list.push(command.value) else list.push(command) end end list.flatten! list.each_with_index { |command, index| assert_type "command[#{index}]", command, Command } list.each do |command| result = command.block.call(self) inherit result, false if result.kind_of?(ChunkyPNG::Canvas) unless result == self end end |
Instance Method Details
#tile(width, height) ⇒ Canvas
Tile the Canvas to fill the region defined by ‘width` and `height`.
84 85 86 87 88 89 90 91 92 |
# File 'lib/magick/canvas.rb', line 84 def tile(width, height) canvas = ChunkyPNG::Canvas.new(width, height) for y in (0...height) for x in (0...width) canvas.set_pixel(x, y, @pixels[(x % @width) + (y % @height) * @width]) end end canvas end |
#to_bool ⇒ Boolean
Returns ‘true` (the Ruby boolean value).
77 78 79 |
# File 'lib/magick/canvas.rb', line 77 def to_bool true end |
#to_data_uri(options = {}) ⇒ String Also known as: to_s
Serializes the Canvas as a Base64 encoded PNG-24 Data URI.
69 70 71 72 |
# File 'lib/magick/canvas.rb', line 69 def to_data_uri( = {}) data = Base64.encode64(to_blob).gsub("\n", '') "url('data:image/png;base64,#{data}')" end |