Class: DYI::Canvas
- Inherits:
-
GraphicalElement
- Object
- Element
- GraphicalElement
- DYI::Canvas
- Defined in:
- lib/dyi/canvas.rb,
lib/ironruby.rb
Overview
The body of Vector-Image. This class is a container for all graphical elements that make up the image.
Constant Summary collapse
- IMPLEMENT_ATTRIBUTES =
[:view_box, :preserve_aspect_ratio]
Constants inherited from GraphicalElement
GraphicalElement::CLASS_REGEXP
Constants inherited from Element
Instance Attribute Summary collapse
-
#child_elements ⇒ Array<Element>
readonly
Returns an array of child elements.
-
#event_listeners ⇒ Hash
readonly
Returns hash of event listners.
-
#metadata ⇒ Object
Returns a metadata object that the image has.
-
#scripts ⇒ Array<Script::SimpleScript>
readonly
Returns an array of scripts.
-
#stylesheets ⇒ Array<Stylesheet::Style>
readonly
Returns an array of stylesheets.
Attributes inherited from GraphicalElement
Attributes inherited from Element
Instance Method Summary collapse
-
#add_initialize_script(script_body) ⇒ Object
Registers a script with the image for initialization.
-
#add_script(script_body, content_type = 'application/ecmascript') ⇒ Object
Registers a script.
-
#add_stylesheet(style_body, content_type = 'text/css') ⇒ Object
Registers a stylesheet with the image.
-
#attributes ⇒ Hash
Returns optional attributes.
-
#canvas ⇒ Canvas
Returns the canvas where the shape is drawn.
-
#initialize(width, height, real_width = nil, real_height = nil, preserve_aspect_ratio = 'none', options = {}) ⇒ Canvas
constructor
A new instance of Canvas.
-
#publish_shape_id ⇒ String
Create a new id for a descendant element.
-
#puts_in_io(format = nil, io = $>, options = {}) ⇒ Object
Puts in io.
-
#real_height ⇒ Length
Returns height of the image.
-
#real_height=(height) ⇒ Object
Sets height of the image.
-
#real_width ⇒ Length
Returns width of the image.
-
#real_width=(width) ⇒ Object
Sets width of the image.
-
#receive_event? ⇒ Boolean
Returns whether an event is set to the shape.
-
#reference_script_file(reference_path, content_type = 'application/ecmascript') ⇒ Object
Registers a reference to a script file with the image.
-
#reference_stylesheet_file(reference_path, content_type = 'text/css') ⇒ Object
Registers a reference to a stylesheet file with the image.
-
#root_element? ⇒ Boolean
Returns whether this instance is root element of the shape.
-
#root_node? ⇒ Boolean
deprecated
Deprecated.
Use #root_element? instead.
-
#save(file_name, format = nil, options = {}) ⇒ Object
Saves as image file.
-
#set_event(event) ⇒ Object
Sets event to the image.
-
#string(format = nil, options = {}) ⇒ String
Returns data that means the image.
- #to_reused_source ⇒ Object
-
#write_as(formatter, io = $>) ⇒ Object
Writes image on io object.
Methods inherited from GraphicalElement
#add_css_class, #add_event_listener, #css_classes, #event_target?, #remove_css_class, #remove_event_listener
Methods inherited from Element
#has_uri_reference?, #id, #id=, #include_external_file?, #inner_id
Constructor Details
#initialize(width, height, real_width = nil, real_height = nil, preserve_aspect_ratio = 'none', options = {}) ⇒ Canvas
Returns a new instance of Canvas.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dyi/canvas.rb', line 81 def initialize(width, height, real_width = nil, real_height = nil, preserve_aspect_ratio='none', ={}) self.width = width self.height = height @view_box = "0 0 #{width} #{height}" @preserve_aspect_ratio = preserve_aspect_ratio @child_elements = [] @scripts = [] @event_listeners = {} @stylesheets = [] @seed_of_id = -1 @receive_event = false self.css_class = [:css_class] self.real_width = real_width self.real_height = real_height end |
Instance Attribute Details
#child_elements ⇒ Array<Element> (readonly)
Returns an array of child elements.
50 51 52 |
# File 'lib/dyi/canvas.rb', line 50 def child_elements @child_elements end |
#event_listeners ⇒ Hash (readonly)
Returns hash of event listners.
55 56 57 |
# File 'lib/dyi/canvas.rb', line 55 def event_listeners @event_listeners end |
#metadata ⇒ Object
Returns a metadata object that the image has.
70 71 72 |
# File 'lib/dyi/canvas.rb', line 70 def end |
#scripts ⇒ Array<Script::SimpleScript> (readonly)
Returns an array of scripts.
65 66 67 |
# File 'lib/dyi/canvas.rb', line 65 def scripts @scripts end |
#stylesheets ⇒ Array<Stylesheet::Style> (readonly)
Returns an array of stylesheets.
60 61 62 |
# File 'lib/dyi/canvas.rb', line 60 def stylesheets @stylesheets end |
Instance Method Details
#add_initialize_script(script_body) ⇒ Object
Registers a script with the image for initialization.
258 259 260 261 262 263 264 265 |
# File 'lib/dyi/canvas.rb', line 258 def add_initialize_script(script_body) if @init_script @init_script.append_body(script_body) else @init_script = Script::EcmaScript::EventListener.new(script_body) add_event_listener(:load, @init_script) end end |
#add_script(script_body, content_type = 'application/ecmascript') ⇒ Object
Registers a script.
222 223 224 225 226 227 228 |
# File 'lib/dyi/canvas.rb', line 222 def add_script(script_body, content_type = 'application/ecmascript') if script_body.respond_to?(:include_external_file?) @scripts << script_body unless @scripts.include?(script_body) else @scripts << Script::SimpleScript.new(script_body, content_type) end end |
#add_stylesheet(style_body, content_type = 'text/css') ⇒ Object
Registers a stylesheet with the image.
242 243 244 |
# File 'lib/dyi/canvas.rb', line 242 def add_stylesheet(style_body, content_type = 'text/css') @stylesheets << Stylesheet::Style.new(style_body, content_type) end |
#attributes ⇒ Hash
Returns optional attributes.
185 186 187 188 189 190 191 192 |
# File 'lib/dyi/canvas.rb', line 185 def attributes IMPLEMENT_ATTRIBUTES.inject({}) do |hash, attribute| variable_name = '@' + attribute.to_s.split(/(?=[A-Z])/).map{|str| str.downcase}.join('_') value = instance_variable_get(variable_name) hash[attribute] = value.to_s if value hash end end |
#canvas ⇒ Canvas
Returns the canvas where the shape is drawn.
141 142 143 |
# File 'lib/dyi/canvas.rb', line 141 def canvas self end |
#publish_shape_id ⇒ String
Create a new id for a descendant element.
197 198 199 |
# File 'lib/dyi/canvas.rb', line 197 def publish_shape_id 'elm%04d' % (@seed_of_id += 1) end |
#puts_in_io(format = nil, io = $>, options = {}) ⇒ Object
Puts in io.
169 170 171 |
# File 'lib/dyi/canvas.rb', line 169 def puts_in_io(format=nil, io=$>, ={}) get_formatter(format, ).puts(io) end |
#real_height ⇒ Length
Returns height of the image.
113 114 115 |
# File 'lib/dyi/canvas.rb', line 113 def real_height @real_height || height end |
#real_height=(height) ⇒ Object
Sets height of the image.
119 120 121 |
# File 'lib/dyi/canvas.rb', line 119 def real_height=(height) @real_height = Length.new_or_nil(height) end |
#real_width ⇒ Length
Returns width of the image.
101 102 103 |
# File 'lib/dyi/canvas.rb', line 101 def real_width @real_width || width end |
#real_width=(width) ⇒ Object
Sets width of the image.
107 108 109 |
# File 'lib/dyi/canvas.rb', line 107 def real_width=(width) @real_width = Length.new_or_nil(width) end |
#receive_event? ⇒ Boolean
Returns whether an event is set to the shape.
212 213 214 |
# File 'lib/dyi/canvas.rb', line 212 def receive_event? @receive_event end |
#reference_script_file(reference_path, content_type = 'application/ecmascript') ⇒ Object
Registers a reference to a script file with the image.
234 235 236 |
# File 'lib/dyi/canvas.rb', line 234 def reference_script_file(reference_path, content_type = 'application/ecmascript') @scripts << Script::ScriptReference.new(reference_path, content_type) end |
#reference_stylesheet_file(reference_path, content_type = 'text/css') ⇒ Object
Registers a reference to a stylesheet file with the image.
250 251 252 |
# File 'lib/dyi/canvas.rb', line 250 def reference_stylesheet_file(reference_path, content_type = 'text/css') @stylesheets << Stylesheet::StyleReference.new(reference_path, content_type) end |
#root_element? ⇒ Boolean
Returns whether this instance is root element of the shape.
134 135 136 |
# File 'lib/dyi/canvas.rb', line 134 def root_element? true end |
#root_node? ⇒ Boolean
Use #root_element? instead.
124 125 126 127 128 129 |
# File 'lib/dyi/canvas.rb', line 124 def root_node? msg = [__FILE__, __LINE__, ' waring'] msg << ' DYI::Canvas#root_node? is deprecated; use DYI::Canvas#root_element?' warn(msg.join(':')) true end |
#save(file_name, format = nil, options = {}) ⇒ Object
Saves as image file.
159 160 161 |
# File 'lib/dyi/canvas.rb', line 159 def save(file_name, format=nil, ={}) get_formatter(format, ).save(file_name) end |
#set_event(event) ⇒ Object
Sets event to the image.
204 205 206 207 |
# File 'lib/dyi/canvas.rb', line 204 def set_event(event) super @receive_event = true end |
#string(format = nil, options = {}) ⇒ String
Returns data that means the image.
179 180 181 |
# File 'lib/dyi/canvas.rb', line 179 def string(format=nil, ={}) get_formatter(format, ).string end |
#to_reused_source ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/dyi/canvas.rb', line 268 def to_reused_source = {} [:css_class] = css_class template = Shape::GraphicalTemplate.new(width, height, preserve_aspect_ratio, ) template.instance_variable_set('@child_elements', child_elements) template end |
#write_as(formatter, io = $>) ⇒ Object
Writes image on io object.
149 150 151 |
# File 'lib/dyi/canvas.rb', line 149 def write_as(formatter, io=$>) formatter.write_canvas(self, io) end |