Class: Compass::Magick::Canvas

Inherits:
ChunkyPNG::Canvas
  • Object
show all
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

#context, #options

Instance Method Summary collapse

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.

Overloads:

  • #initialize(canvas, *commands) ⇒ Canvas

    Parameters:

    • canvas (Canvas)

      Copy image from another Canvas object.

    • commands (Array<Command>)

      The list of commands to execute on new Canvas instance.

  • #initialize(data, *commands) ⇒ Canvas

    Parameters:

    • data (Sass::Script::String)

      A Base64 encoded Data URL containing the image.

    • commands (Array<Command>)

      The list of commands to execute on the Canvas instance.

  • #initialize(url, *commands) ⇒ Canvas

    Parameters:

    • url (Sass::Script::String)

      The URL to the image, relative to the stylesheet.

    • commands (Array<Command>)

      The list of commands to execute on the Canvas instance.

  • #initialize(path, *commands) ⇒ Canvas

    Parameters:

    • path (Sass::Script::String)

      The path to the image, relative to the configured generated_images_path or images_path.

    • commands (Array<Command>)

      The list of commands to execute on the Canvas instance.

  • #initialize(width, height, *commands) ⇒ Canvas

    Parameters:

    • width (Sass::Script::Number)

      The width of the new transparent Canvas.

    • height (Sass::Script::Number)

      The height of the new transparent Canvas.

    • commands (Array<Command>)

      The list of commands to execute on the 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`.

Returns:

  • (Canvas)

    A new Canvas instance that fills the given region.



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_boolBoolean

Returns ‘true` (the Ruby boolean value).

Returns:

  • (Boolean)

    ‘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.

Returns:

  • (String)

    A Base64 encoded PNG-24 Data URI for the generated image.



69
70
71
72
# File 'lib/magick/canvas.rb', line 69

def to_data_uri(options = {})
  data = Base64.encode64(to_blob).gsub("\n", '')
  "url('data:image/png;base64,#{data}')"
end