Class: Fontaine::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/fontaine/canvas.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, width, height, alt = "", html_options = {}) ⇒ Canvas

Returns a new instance of Canvas.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fontaine/canvas.rb', line 14

def initialize(id, width, height, alt = "", html_options = {})
  @id = id
  @width = width
  @height = height
  @alt = alt
  @html_options = html_options
  @attributes = {
    #set the initial styles
    :fill_style => "#000000", 
    :stroke_style => '#000000',
    :shadow_color => '#000000',
    :shadow_blur => 0,
    :shadow_offset_x => 0,
    :shadow_offset_y => 0,
    :line_cap => 'butt',
    :line_join => 'miter',
    :line_width => '1',
    :miter_limit => '10',
    :font => '10px sans-serif',
    :text_align => 'start',
    :text_baseline => 'alphabetic',
    :global_alpha => 1.0,
    :global_composite_operation => 'source-over'
    
    }
  yield self if block_given?    
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fontaine/canvas.rb', line 173

def method_missing(method_sym, *arguments, &block)
  if canvas_array.include? method_sym.to_s.sub(/trigger_on_/, "")
    variable_name = method_sym.to_s.sub(/trigger_/, "@")
    instance_variable_get(variable_name).call(arguments) if instance_variable_defined?(variable_name)
  elsif canvas_array.include? method_sym.to_s.sub(/on_/, "")
    instance_variable_set("@#{method_sym}", block)
  elsif draw_method_array.include? method_sym.to_s
    send_msg "#{ruby_to_js_command(method_sym)} #{arguments.join(" ")}"
  elsif return_method_array.include? method_sym.to_s
    send_msg "#{ruby_to_js_command(method_sym)} #{arguments.join(" ")}"
    @attributes[method_sym] = arguments[0] if (!arguments[0].nil? && !@attributes[method_sym].nil?)
    return @attributes[method_sym]
  else
    super(method_sym, *arguments, &block)
  end
end

Instance Attribute Details

#altObject

Returns the value of attribute alt.



6
7
8
# File 'lib/fontaine/canvas.rb', line 6

def alt
  @alt
end

#attributesObject

Returns the value of attribute attributes.



11
12
13
# File 'lib/fontaine/canvas.rb', line 11

def attributes
  @attributes
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/fontaine/canvas.rb', line 5

def height
  @height
end

#html_optionsObject

Returns the value of attribute html_options.



7
8
9
# File 'lib/fontaine/canvas.rb', line 7

def html_options
  @html_options
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/fontaine/canvas.rb', line 3

def id
  @id
end

#last_responseObject

Returns the value of attribute last_response.



10
11
12
# File 'lib/fontaine/canvas.rb', line 10

def last_response
  @last_response
end

#settingsObject

Returns the value of attribute settings.



8
9
10
# File 'lib/fontaine/canvas.rb', line 8

def settings
  @settings
end

#widthObject

Returns the value of attribute width.



4
5
6
# File 'lib/fontaine/canvas.rb', line 4

def width
  @width
end

#wsObject

Returns the value of attribute ws.



9
10
11
# File 'lib/fontaine/canvas.rb', line 9

def ws
  @ws
end

Instance Method Details

#canvas_arrayObject



190
191
192
193
# File 'lib/fontaine/canvas.rb', line 190

def canvas_array
  return["response", "mousedown", "mouseup", "keyup", "keydown", "keypress", "click", "mouseover", 
    "mouseout", "mousemove", "touchstart", "touchmove", "touchend", "open"]
end

#create_image_data(id, *args) ⇒ Object



154
155
156
# File 'lib/fontaine/canvas.rb', line 154

def create_image_data(id, *args)
  return image_data.new(id, self, args)
end

#create_linear_gradient(x0, y0, x1, y1, id) ⇒ Object



139
140
141
142
# File 'lib/fontaine/canvas.rb', line 139

def create_linear_gradient(x0, y0, x1, y1, id)
  send_msg("createLinearGradient #{x0} #{y0} #{x1} #{y1} #{id}")
  return Gradient.new(id, self)
end

#create_pattern(image, pattern) ⇒ Object



144
145
146
147
# File 'lib/fontaine/canvas.rb', line 144

def create_pattern(image, pattern)
  send_msg("createPattern #{image.id} #{pattern}")
  return Pattern.new(id)
end

#create_radial_gradient(x0, y0, r0, x1, y1, r1, id) ⇒ Object



149
150
151
152
# File 'lib/fontaine/canvas.rb', line 149

def create_radial_gradient(x0, y0, r0, x1, y1, r1, id)
  send_msg("createRadialGradient #{x0} #{y0} #{r0} #{x1} #{y1} #{r1} #{id}")
  return Gradient.new(id, self)
end

#displayObject



107
108
109
110
111
112
113
# File 'lib/fontaine/canvas.rb', line 107

def display    
  options = ""
  @html_options.each_pair do  |key, value| 
    options << "#{key}=\"#{value}\" "
  end
  return "<canvas id=\"#{@id}\" width=\"#{@width}\" height=\"#{@height}\" #{options}>#{@alt}</canvas>"
end

#draw_method_arrayObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fontaine/canvas.rb', line 212

def draw_method_array
  return [
    #Rectangles
    "rect", "fill_rect", "stroke_rect", "clear_rect", 
    #Paths
    "fill", "stroke", "begin_path", "move_to", "close_path", "line_to", "clip", 
    "quadratic_curve_to", "bezier_curve_to", "arc", "arc_to",
    #Transformations
    "scale", "rotate", "translate", "transform", "set_transform",
    #Text
    "fill_text", "stroke_text", "measure_text",
    #Image Drawing
    "draw_image",
    #Other
    "save", "restore"
    ]
end

#fill_style(style = "") ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fontaine/canvas.rb', line 115

def fill_style(style="")
   @attributes[:fill_style] = style if style != ""
  if style.is_a? String
    send_msg "fillStyle #{style}"
  else
    send_msg "fillStyleObject #{style.id}"
  end
  
  # return @last_response
  return @attributes[:fill_style]
  
end

#listen(request, settings) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fontaine/canvas.rb', line 42

def listen(request, settings)  
  @settings = settings 
  request.websocket do |ws|
    ws.onopen do
      @ws = ws
      @ws.send("register ##{id}")
      @settings.sockets << ws
      trigger_on_open
    end
    ws.onmessage do |msg|
      #puts("recieved message: #{msg}")
      #EM.next_tick { process_message(msg)}
      process_message(msg)
    end
    ws.onclose do
      @settings.sockets.delete(ws)
    end
  end
end

#process_message(s_message) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/fontaine/canvas.rb', line 62

def process_message(s_message)
  a_message = s_message.split(' ')
  command = a_message[0] #the first part of the message is the command
  params = a_message[1..(a_message.length-1)] #get the rest of the message
  params = Hash[*params] if command != "response"#convert the array to a hash if applicable
  respond_to(command, params)
end

#put_image_data(image, x, y, dirty_x = "", dirty_y = "", dirty_width = "", dirty_height = "") ⇒ Object

end



163
164
165
# File 'lib/fontaine/canvas.rb', line 163

def put_image_data(image, x, y, dirty_x="", dirty_y="", dirty_width="", dirty_height="")
  send_msg("putImageData #{image.id} #{x} #{y} #{dirty_x} #{dirty_y} #{dirty_width} #{dirty_height}")
end

#respond_to(s_command, params = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fontaine/canvas.rb', line 70

def respond_to(s_command, params = {})
  case s_command
  when "mousedown"
    trigger_on_mousedown(params["x"], params["y"], params["button"])
  when "mouseup"
    trigger_on_mouseup(params["x"], params["y"], params["button"])
  when "mouseover"
    trigger_on_mouseover(params["x"], params["y"]) 
  when "mouseout"
    trigger_on_mouseout(params["x"], params["y"]) 
  when "mousemove"
    trigger_on_mousemove(params["x"], params["y"])
    
 ##Very glitchy right now. Need to fix this some time

  # when "touchstart"
    # trigger_on_touchstart(params["x"], params["y"], params["id"])  
  # when "touchmove"
    # trigger_on_touchstart(params["x"], params["y"], params["id"])
  # when "touchend"
    # trigger_on_touchstart(params["x"], params["y"], params["id"])               
  when "keyup"
    trigger_on_keyup(params["key_code"])    
  when "keydown"
    trigger_on_keydown(params["key_code"])
  when "keypress"
    trigger_on_keypress(params["which"])
  when "click"
    trigger_on_click(params["x"], params["y"], params["button"])      
  when "response"
    @last_response =  params.flatten[0].to_s
    trigger_on_response(@last_response)
  else
    puts "unimplemented method #{s_command}"
  end
end

#return_method_arrayObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fontaine/canvas.rb', line 195

def return_method_array
  return[
     #Colors, Styles, and Shadows
    "shadow_color", "shadow_blur", "shadow_offset_x", "shadow_offset_y",
    #Line Styles
    "line_cap", "line_join", "line_width", "miter_limit",
    #Paths
    "isPointInPath",
    #Text
    "font", "text_align", "text_baseline",
    #Composting
    "global_alpha", "global_composite_operation",
    #Other
    "to_data_url"
   ]
end

#ruby_to_js_command(method_sym) ⇒ Object



230
231
232
233
234
235
236
237
238
239
# File 'lib/fontaine/canvas.rb', line 230

def ruby_to_js_command(method_sym)
  js_command = method_sym.to_s
  js_commands = js_command.split('_')
  if js_commands.size > 1
    js_commands[1..js_commands.size].each do |command|
      command.capitalize!
    end
  end
  js_commands.join
end

#send_msg(msg) ⇒ Object



167
168
169
170
171
# File 'lib/fontaine/canvas.rb', line 167

def send_msg(msg)
  #puts "Sending message: #{msg}"
  #EM.next_tick {@ws.send(msg)} if defined? @ws
  @ws.send(msg) if defined? @ws
end

#stroke_style(style = "") ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/fontaine/canvas.rb', line 128

def stroke_style(style="")
  @attributes[:stroke_style] = style if style != ""
  if style.is_a? String
    send_msg "strokeStyle #{style}"
  else
    send_msg "strokeStyleObject #{style.id}"
  end
  # return @last_response
  return @attributes[:stroke_style]
end