/
canvas.rb
336 lines (240 loc) · 5.48 KB
/
canvas.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# backtick_javascript: true
require 'browser/canvas/style'
require 'browser/canvas/text'
require 'browser/canvas/data'
require 'browser/canvas/gradient'
module Browser
class Canvas
include Native::Wrapper
attr_reader :element, :style, :text
def initialize(*args)
if DOM::Element === args.first
element = args.shift
if DOM::Element::Image === element
@image = element
else
@element = element
end
elsif Canvas === args.first
@image = args.first
end
unless @element
@element = $document.create_element('canvas')
if @image
@element[:width] = @image.width
@element[:height] = @image.height
else
@element[:width] = args.shift
@element[:height] = args.shift
end
end
if @element.node_name != 'CANVAS'
raise ArgumentError, "the element isn't a <canvas> element"
end
super(`#{@element.to_n}.getContext('2d')`)
@style = Style.new(self)
@text = Text.new(self)
if @image
draw_image(@image)
end
end
def width
@element[:width].to_i
end
def height
@element[:height].to_i
end
def width=(new_width)
@element[:width] = new_width.to_i
end
def height=(new_height)
@element[:height] = new_height.to_i
end
def append_to(parent)
@element.append_to(parent)
end
def load(path)
promise = Promise.new
image = $document.create_element('img')
image.on :load do
promise.resolve(image)
end
image[:src] = path
promise
end
def data(x = nil, y = nil, width = nil, height = nil)
x ||= 0
y ||= 0
width ||= self.width
height ||= self.height
Data.new(self, x, y, width, height)
end
def pattern(image, type = :repeat)
`#@native.createPattern(#{DOM(image).to_n}, type)`
end
def gradient(*args, &block)
Gradient.new(self, *args, &block)
end
def clear(x = nil, y = nil, width = nil, height = nil)
x ||= 0
y ||= 0
width ||= self.width
height ||= self.height
`#@native.clearRect(x, y, width, height)`
end
def begin
`#@native.beginPath()`
self
end
def close
`#@native.closePath()`
self
end
def save
`#@native.save()`
self
end
def restore
`#@native.restore()`
self
end
def move_to(x, y)
`#@native.moveTo(x, y)`
self
end
alias move move_to
def line_to(x, y)
`#@native.lineTo(x, y)`
self
end
def line(x1, y1, x2, y2)
move_to x1, y1
line_to x2, y2
end
def rect(x, y, width, height)
`#@native.rect(x, y, width, height)`
self
end
def arc(x, y, radius, angle, clockwise = false)
`#@native.arc(x, y, radius, #{angle[:start]}, #{angle[:end]}, !clockwise)`
self
end
def quadratic_curve_to(cp1x, cp1y, x, y)
`#@native.quadraticCurveTo(cp1x, cp1y, x, y)`
self
end
def bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
`#@native.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)`
self
end
def curve_to(*args)
case args.length
when 4 then quadratic_curve_to(*args)
when 6 then bezier_curve_to(*args)
else raise ArgumentError, "don't know where to dispatch"
end
self
end
def draw_image(image, *args)
if Canvas === image
image = image.element
else
image = DOM(image)
end
if args.first.is_a?(Hash)
source, destination = args
`#@native.drawImage(#{image.to_n}, #{source[:x]}, #{source[:y]}, #{source[:width]}, #{source[:height]}, #{destination[:x]}, #{destination[:y]}, #{destination[:width]}, #{destination[:height]})`
else
case args.length
when 0
`#@native.drawImage(#{image.to_n}, 0, 0)`
when 2
`#@native.drawImage(#{image.to_n}, #{args[0]}, #{args[1]})`
when 4
`#@native.drawImage(#{image.to_n}, #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]})`
end
end
self
end
def translate(x, y, &block)
if block
save
`#@native.translate(x, y)`
instance_eval(&block)
restore
else
`#@native.translate(x, y)`
end
self
end
def rotate(angle, &block)
if block
save
`#@native.rotate(angle)`
instance_eval(&block)
restore
else
`#@native.rotate(angle)`
end
self
end
def scale(x, y, &block)
if block
save
`#@native.scale(x, y)`
instance_eval(&block)
restore
else
`#@native.scale(x, y)`
end
self
end
def transform(m11, m12, m21, m22, dx, dy, &block)
if block
save
`#@native.transform(m11, m12, m21, m22, dx, dy)`
instance_eval(&block)
restore
else
`#@native.transform(m11, m12, m21, m22, dx, dy)`
end
self
end
def path(&block)
`#@native.beginPath()`
instance_eval(&block)
`#@native.closePath()`
self
end
def fill(&block)
path(&block) if block
`#@native.fill()`
self
end
def stroke(&block)
path(&block) if block
`#@native.stroke()`
self
end
def clip(&block)
path(&block) if block
`#@native.clip()`
self
end
def point_in_path?(x, y)
`#@native.isPointInPath(x, y)`
end
def to_data(type = undefined)
`#{@element.to_n}.toDataUrl(type)`
end
def to_dom(*)
@element
end
def on(*args, &block); @element.on(*args, &block); end
def one(*args, &block); @element.one(*args, &block); end
def off(*args, &block); @element.off(*args, &block); end
end
Browser::DOM::Builder.for Canvas do |b, item|
item.element
end
end