shank 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +0 -0
- data/Rakefile +2 -0
- data/lib/assets/javascripts/engine.gamepads.coffee +46 -0
- data/lib/assets/javascripts/engine_stats.coffee +10 -0
- data/lib/assets/javascripts/error_handler.coffee +11 -0
- data/lib/assets/javascripts/game_keys.coffee +6 -0
- data/lib/assets/javascripts/gamepads.coffee +21 -0
- data/lib/assets/javascripts/gamepads.controller.coffee +138 -0
- data/lib/assets/javascripts/init.coffee +6 -0
- data/lib/assets/javascripts/joysticks.coffee +238 -0
- data/lib/assets/javascripts/jquery.hotkeys.coffee +161 -0
- data/lib/assets/javascripts/jquery.reverse_merge.coffee +21 -0
- data/lib/assets/javascripts/keydown.coffee +73 -0
- data/lib/assets/javascripts/mouse.coffee +66 -0
- data/lib/assets/javascripts/music.coffee +78 -0
- data/lib/assets/javascripts/pixie_canvas.coffee +739 -0
- data/lib/assets/javascripts/request_animation_frame.coffee +22 -0
- data/lib/assets/javascripts/shank.coffee +18 -0
- data/lib/assets/javascripts/sound.coffee +131 -0
- data/lib/assets/javascripts/storage.coffee +88 -0
- data/lib/shank/version.rb +3 -0
- data/lib/shank.rb +10 -0
- data/shank.gemspec +17 -0
- data/test/jquery.reverse_merge.coffee +43 -0
- data/test/keydown.coffee +19 -0
- data/test/pixie_canvas.coffee +18 -0
- data/test/request_animation_frame.coffee +7 -0
- data/test/sound.coffee +7 -0
- data/test/storage.coffee +47 -0
- data/test/xstats.coffee +7 -0
- data/vendor/assets/javascripts/xstats.js +767 -0
- metadata +113 -0
@@ -0,0 +1,739 @@
|
|
1
|
+
( ($) ->
|
2
|
+
$.fn.pixieCanvas = (options={}) ->
|
3
|
+
canvas = this.get(0)
|
4
|
+
context = undefined
|
5
|
+
|
6
|
+
###*
|
7
|
+
PixieCanvas provides a convenient wrapper for working with Context2d.
|
8
|
+
|
9
|
+
Methods try to be as flexible as possible as to what arguments they take.
|
10
|
+
|
11
|
+
Non-getter methods return `this` for method chaining.
|
12
|
+
|
13
|
+
@name PixieCanvas
|
14
|
+
@constructor
|
15
|
+
###
|
16
|
+
$canvas = $(canvas).extend
|
17
|
+
###*
|
18
|
+
Passes this canvas to the block with the given matrix transformation
|
19
|
+
applied. All drawing methods called within the block will draw
|
20
|
+
into the canvas with the transformation applied. The transformation
|
21
|
+
is removed at the end of the block, even if the block throws an error.
|
22
|
+
|
23
|
+
@name withTransform
|
24
|
+
@methodOf PixieCanvas#
|
25
|
+
|
26
|
+
@param {Matrix} matrix
|
27
|
+
@param {Function} block
|
28
|
+
|
29
|
+
@returns {PixieCanvas} this
|
30
|
+
###
|
31
|
+
withTransform: (matrix, block) ->
|
32
|
+
context.save()
|
33
|
+
|
34
|
+
context.transform(
|
35
|
+
matrix.a,
|
36
|
+
matrix.b,
|
37
|
+
matrix.c,
|
38
|
+
matrix.d,
|
39
|
+
matrix.tx,
|
40
|
+
matrix.ty
|
41
|
+
)
|
42
|
+
|
43
|
+
try
|
44
|
+
block(@)
|
45
|
+
finally
|
46
|
+
context.restore()
|
47
|
+
|
48
|
+
return @
|
49
|
+
|
50
|
+
###*
|
51
|
+
Clear the canvas (or a portion of it).
|
52
|
+
|
53
|
+
Clear the entire canvas
|
54
|
+
|
55
|
+
<code><pre>
|
56
|
+
canvas.clear()
|
57
|
+
</pre></code>
|
58
|
+
|
59
|
+
Clear a portion of the canvas
|
60
|
+
|
61
|
+
<code class="run"><pre>
|
62
|
+
# Set up: Fill canvas with blue
|
63
|
+
canvas.fill("blue")
|
64
|
+
|
65
|
+
# Clear a portion of the canvas
|
66
|
+
canvas.clear
|
67
|
+
x: 50
|
68
|
+
y: 50
|
69
|
+
width: 50
|
70
|
+
height: 50
|
71
|
+
</pre></code>
|
72
|
+
|
73
|
+
You can also clear the canvas by passing x, y, width height as
|
74
|
+
unnamed parameters:
|
75
|
+
|
76
|
+
<code><pre>
|
77
|
+
canvas.clear(25, 25, 50, 50)
|
78
|
+
</pre></code>
|
79
|
+
|
80
|
+
@name clear
|
81
|
+
@methodOf PixieCanvas#
|
82
|
+
|
83
|
+
@param {Number} [x] where to start clearing on the x axis
|
84
|
+
@param {Number} [y] where to start clearing on the y axis
|
85
|
+
@param {Number} [width] width of area to clear
|
86
|
+
@param {Number} [height] height of area to clear
|
87
|
+
|
88
|
+
@returns {PixieCanvas} this
|
89
|
+
###
|
90
|
+
clear: (x={}, y, width, height) ->
|
91
|
+
unless y?
|
92
|
+
{x, y, width, height} = x
|
93
|
+
|
94
|
+
x ||= 0
|
95
|
+
y ||= 0
|
96
|
+
width = canvas.width unless width?
|
97
|
+
height = canvas.height unless height?
|
98
|
+
|
99
|
+
context.clearRect(x, y, width, height)
|
100
|
+
|
101
|
+
return @
|
102
|
+
|
103
|
+
###*
|
104
|
+
Fills the entire canvas (or a specified section of it) with
|
105
|
+
the given color.
|
106
|
+
|
107
|
+
<code class="run"><pre>
|
108
|
+
# Paint the town (entire canvas) red
|
109
|
+
canvas.fill "red"
|
110
|
+
|
111
|
+
# Fill a section of the canvas white (#FFF)
|
112
|
+
canvas.fill
|
113
|
+
x: 50
|
114
|
+
y: 50
|
115
|
+
width: 50
|
116
|
+
height: 50
|
117
|
+
color: "#FFF"
|
118
|
+
</pre></code>
|
119
|
+
|
120
|
+
@name fill
|
121
|
+
@methodOf PixieCanvas#
|
122
|
+
|
123
|
+
@param {Number} [x=0] Optional x position to fill from
|
124
|
+
@param {Number} [y=0] Optional y position to fill from
|
125
|
+
@param {Number} [width=canvas.width] Optional width of area to fill
|
126
|
+
@param {Number} [height=canvas.height] Optional height of area to fill
|
127
|
+
@param {Bounds} [bounds] bounds object to fill
|
128
|
+
@param {String|Color} [color] color of area to fill
|
129
|
+
|
130
|
+
@returns {PixieCanvas} this
|
131
|
+
###
|
132
|
+
fill: (color={}) ->
|
133
|
+
unless color.isString?() || color.channels
|
134
|
+
{x, y, width, height, bounds, color} = color
|
135
|
+
|
136
|
+
{x, y, width, height} = bounds if bounds
|
137
|
+
|
138
|
+
x ||= 0
|
139
|
+
y ||= 0
|
140
|
+
width = canvas.width unless width?
|
141
|
+
height = canvas.height unless height?
|
142
|
+
|
143
|
+
@fillColor(color)
|
144
|
+
context.fillRect(x, y, width, height)
|
145
|
+
|
146
|
+
return @
|
147
|
+
|
148
|
+
###*
|
149
|
+
A direct map to the Context2d draw image. `GameObject`s
|
150
|
+
that implement drawable will have this wrapped up nicely,
|
151
|
+
so there is a good chance that you will not have to deal with
|
152
|
+
it directly.
|
153
|
+
|
154
|
+
@name drawImage
|
155
|
+
@methodOf PixieCanvas#
|
156
|
+
|
157
|
+
@param image
|
158
|
+
@param {Number} sx
|
159
|
+
@param {Number} sy
|
160
|
+
@param {Number} sWidth
|
161
|
+
@param {Number} sHeight
|
162
|
+
@param {Number} dx
|
163
|
+
@param {Number} dy
|
164
|
+
@param {Number} dWidth
|
165
|
+
@param {Number} dHeight
|
166
|
+
|
167
|
+
@returns {PixieCanvas} this
|
168
|
+
###
|
169
|
+
drawImage: (image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) ->
|
170
|
+
context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
|
171
|
+
|
172
|
+
return @
|
173
|
+
|
174
|
+
###*
|
175
|
+
Draws a circle at the specified position with the specified
|
176
|
+
radius and color.
|
177
|
+
|
178
|
+
<code class="run"><pre>
|
179
|
+
# Draw a large orange circle
|
180
|
+
canvas.drawCircle
|
181
|
+
radius: 30
|
182
|
+
position: Point(100, 75)
|
183
|
+
color: "orange"
|
184
|
+
|
185
|
+
# Draw a blue circle with radius 10 at (25, 50)
|
186
|
+
# and a red stroke
|
187
|
+
canvas.drawCircle
|
188
|
+
x: 25
|
189
|
+
y: 50
|
190
|
+
radius: 10
|
191
|
+
color: "blue"
|
192
|
+
stroke:
|
193
|
+
color: "red"
|
194
|
+
width: 1
|
195
|
+
|
196
|
+
# Create a circle object to set up the next examples
|
197
|
+
circle =
|
198
|
+
radius: 20
|
199
|
+
x: 50
|
200
|
+
y: 50
|
201
|
+
|
202
|
+
# Draw a given circle in yellow
|
203
|
+
canvas.drawCircle
|
204
|
+
circle: circle
|
205
|
+
color: "yellow"
|
206
|
+
|
207
|
+
# Draw the circle in green at a different position
|
208
|
+
canvas.drawCircle
|
209
|
+
circle: circle
|
210
|
+
position: Point(25, 75)
|
211
|
+
color: "green"
|
212
|
+
|
213
|
+
# Draw an outline circle in purple.
|
214
|
+
canvas.drawCircle
|
215
|
+
x: 50
|
216
|
+
y: 75
|
217
|
+
radius: 10
|
218
|
+
stroke:
|
219
|
+
color: "purple"
|
220
|
+
width: 2
|
221
|
+
</pre></code>
|
222
|
+
|
223
|
+
@name drawCircle
|
224
|
+
@methodOf PixieCanvas#
|
225
|
+
|
226
|
+
@param {Number} [x] location on the x axis to start drawing
|
227
|
+
@param {Number} [y] location on the y axis to start drawing
|
228
|
+
@param {Point} [position] position object of location to start drawing. This will override x and y values passed
|
229
|
+
@param {Number} [radius] length of the radius of the circle
|
230
|
+
@param {Color|String} [color] color of the circle
|
231
|
+
@param {Circle} [circle] circle object that contains position and radius. Overrides x, y, and radius if passed
|
232
|
+
@param {Stroke} [stroke] stroke object that specifies stroke color and stroke width
|
233
|
+
|
234
|
+
@returns {PixieCanvas} this
|
235
|
+
###
|
236
|
+
drawCircle: ({x, y, radius, position, color, stroke, circle}) ->
|
237
|
+
{x, y, radius} = circle if circle
|
238
|
+
{x, y} = position if position
|
239
|
+
|
240
|
+
context.beginPath()
|
241
|
+
context.arc(x, y, radius, 0, Math.TAU, true)
|
242
|
+
context.closePath()
|
243
|
+
|
244
|
+
if color
|
245
|
+
@fillColor(color)
|
246
|
+
context.fill()
|
247
|
+
|
248
|
+
if stroke
|
249
|
+
@strokeColor(stroke.color)
|
250
|
+
@lineWidth(stroke.width)
|
251
|
+
context.stroke()
|
252
|
+
|
253
|
+
return @
|
254
|
+
|
255
|
+
###*
|
256
|
+
Draws a rectangle at the specified position with given
|
257
|
+
width and height. Optionally takes a position, bounds
|
258
|
+
and color argument.
|
259
|
+
|
260
|
+
<code class="run"><pre>
|
261
|
+
# Draw a red rectangle using x, y, width and height
|
262
|
+
canvas.drawRect
|
263
|
+
x: 50
|
264
|
+
y: 50
|
265
|
+
width: 50
|
266
|
+
height: 50
|
267
|
+
color: "#F00"
|
268
|
+
|
269
|
+
# Draw a blue rectangle using position, width and height
|
270
|
+
# and throw in a stroke for good measure
|
271
|
+
canvas.drawRect
|
272
|
+
position: Point(0, 0)
|
273
|
+
width: 50
|
274
|
+
height: 50
|
275
|
+
color: "blue"
|
276
|
+
stroke:
|
277
|
+
color: "orange"
|
278
|
+
width: 3
|
279
|
+
|
280
|
+
# Set up a bounds object for the next examples
|
281
|
+
bounds =
|
282
|
+
x: 100
|
283
|
+
y: 0
|
284
|
+
width: 100
|
285
|
+
height: 100
|
286
|
+
|
287
|
+
# Draw a purple rectangle using bounds
|
288
|
+
canvas.drawRect
|
289
|
+
bounds: bounds
|
290
|
+
color: "green"
|
291
|
+
|
292
|
+
# Draw the outline of the same bounds, but at a different position
|
293
|
+
canvas.drawRect
|
294
|
+
bounds: bounds
|
295
|
+
position: Point(0, 50)
|
296
|
+
stroke:
|
297
|
+
color: "purple"
|
298
|
+
width: 2
|
299
|
+
</pre></code>
|
300
|
+
|
301
|
+
@name drawRect
|
302
|
+
@methodOf PixieCanvas#
|
303
|
+
|
304
|
+
@param {Number} [x] location on the x axis to start drawing
|
305
|
+
@param {Number} [y] location on the y axis to start drawing
|
306
|
+
@param {Number} [width] width of rectangle to draw
|
307
|
+
@param {Number} [height] height of rectangle to draw
|
308
|
+
@param {Point} [position] position to start drawing. Overrides x and y if passed
|
309
|
+
@param {Color|String} [color] color of rectangle
|
310
|
+
@param {Bounds} [bounds] bounds of rectangle. Overrides x, y, width, height if passed
|
311
|
+
@param {Stroke} [stroke] stroke object that specifies stroke color and stroke width
|
312
|
+
|
313
|
+
@returns {PixieCanvas} this
|
314
|
+
###
|
315
|
+
drawRect: ({x, y, width, height, position, bounds, color, stroke}) ->
|
316
|
+
{x, y, width, height} = bounds if bounds
|
317
|
+
{x, y} = position if position
|
318
|
+
|
319
|
+
if color
|
320
|
+
@fillColor(color)
|
321
|
+
context.fillRect(x, y, width, height)
|
322
|
+
|
323
|
+
if stroke
|
324
|
+
@strokeColor(stroke.color)
|
325
|
+
@lineWidth(stroke.width)
|
326
|
+
context.strokeRect(x, y, width, height)
|
327
|
+
|
328
|
+
return @
|
329
|
+
|
330
|
+
###*
|
331
|
+
Draw a line from `start` to `end`.
|
332
|
+
|
333
|
+
<code class="run"><pre>
|
334
|
+
# Draw a sweet diagonal
|
335
|
+
canvas.drawLine
|
336
|
+
start: Point(0, 0)
|
337
|
+
end: Point(200, 200)
|
338
|
+
color: "purple"
|
339
|
+
|
340
|
+
# Draw another sweet diagonal
|
341
|
+
canvas.drawLine
|
342
|
+
start: Point(200, 0)
|
343
|
+
end: Point(0, 200)
|
344
|
+
color: "red"
|
345
|
+
width: 6
|
346
|
+
|
347
|
+
# Now draw a sweet horizontal with a direction and a length
|
348
|
+
canvas.drawLine
|
349
|
+
start: Point(0, 100)
|
350
|
+
length: 200
|
351
|
+
direction: Point(1, 0)
|
352
|
+
color: "orange"
|
353
|
+
|
354
|
+
</pre></code>
|
355
|
+
|
356
|
+
@name drawLine
|
357
|
+
@methodOf PixieCanvas#
|
358
|
+
|
359
|
+
@param {Point} start position to start drawing from
|
360
|
+
@param {Point} [end] position to stop drawing
|
361
|
+
@param {Number} [width] width of the line
|
362
|
+
@param {String|Color} [color] color of the line
|
363
|
+
|
364
|
+
@returns {PixieCanvas} this
|
365
|
+
###
|
366
|
+
drawLine: ({start, end, width, color, direction, length}) ->
|
367
|
+
width ||= 3
|
368
|
+
|
369
|
+
if direction
|
370
|
+
end = direction.norm(length).add(start)
|
371
|
+
|
372
|
+
@lineWidth(width)
|
373
|
+
@strokeColor(color)
|
374
|
+
|
375
|
+
context.beginPath()
|
376
|
+
context.moveTo(start.x, start.y)
|
377
|
+
context.lineTo(end.x, end.y)
|
378
|
+
context.closePath()
|
379
|
+
context.stroke()
|
380
|
+
|
381
|
+
return @
|
382
|
+
|
383
|
+
###*
|
384
|
+
Draw a polygon.
|
385
|
+
|
386
|
+
<code class="run"><pre>
|
387
|
+
# Draw a sweet rhombus
|
388
|
+
canvas.drawPoly
|
389
|
+
points: [
|
390
|
+
Point(50, 25)
|
391
|
+
Point(75, 50)
|
392
|
+
Point(50, 75)
|
393
|
+
Point(25, 50)
|
394
|
+
]
|
395
|
+
color: "purple"
|
396
|
+
stroke:
|
397
|
+
color: "red"
|
398
|
+
width: 2
|
399
|
+
</pre></code>
|
400
|
+
|
401
|
+
@name drawPoly
|
402
|
+
@methodOf PixieCanvas#
|
403
|
+
|
404
|
+
@param {Point[]} [points] collection of points that define the vertices of the polygon
|
405
|
+
@param {String|Color} [color] color of the polygon
|
406
|
+
@param {Stroke} [stroke] stroke object that specifies stroke color and stroke width
|
407
|
+
|
408
|
+
@returns {PixieCanvas} this
|
409
|
+
###
|
410
|
+
drawPoly: ({points, color, stroke}) ->
|
411
|
+
context.beginPath()
|
412
|
+
points.each (point, i) ->
|
413
|
+
if i == 0
|
414
|
+
context.moveTo(point.x, point.y)
|
415
|
+
else
|
416
|
+
context.lineTo(point.x, point.y)
|
417
|
+
context.lineTo points[0].x, points[0].y
|
418
|
+
|
419
|
+
if color
|
420
|
+
@fillColor(color)
|
421
|
+
context.fill()
|
422
|
+
|
423
|
+
if stroke
|
424
|
+
@strokeColor(stroke.color)
|
425
|
+
@lineWidth(stroke.width)
|
426
|
+
context.stroke()
|
427
|
+
|
428
|
+
return @
|
429
|
+
|
430
|
+
###*
|
431
|
+
Draw a rounded rectangle.
|
432
|
+
|
433
|
+
Adapted from http://js-bits.blogspot.com/2010/07/canvas-rounded-corner-rectangles.html
|
434
|
+
|
435
|
+
<code class="run"><pre>
|
436
|
+
# Draw a purple rounded rectangle with a red outline
|
437
|
+
canvas.drawRoundRect
|
438
|
+
position: Point(25, 25)
|
439
|
+
radius: 10
|
440
|
+
width: 150
|
441
|
+
height: 100
|
442
|
+
color: "purple"
|
443
|
+
stroke:
|
444
|
+
color: "red"
|
445
|
+
width: 2
|
446
|
+
</pre></code>
|
447
|
+
|
448
|
+
@name drawRoundRect
|
449
|
+
@methodOf PixieCanvas#
|
450
|
+
|
451
|
+
@param {Number} [x] location on the x axis to start drawing
|
452
|
+
@param {Number} [y] location on the y axis to start drawing
|
453
|
+
@param {Number} [width] width of the rounded rectangle
|
454
|
+
@param {Number} [height] height of the rounded rectangle
|
455
|
+
@param {Number} [radius=5] radius to round the rectangle corners
|
456
|
+
@param {Point} [position] position to start drawing. Overrides x and y if passed
|
457
|
+
@param {Color|String} [color] color of the rounded rectangle
|
458
|
+
@param {Bounds} [bounds] bounds of the rounded rectangle. Overrides x, y, width, and height if passed
|
459
|
+
@param {Stroke} [stroke] stroke object that specifies stroke color and stroke width
|
460
|
+
|
461
|
+
@returns {PixieCanvas} this
|
462
|
+
###
|
463
|
+
drawRoundRect: ({x, y, width, height, radius, position, bounds, color, stroke}) ->
|
464
|
+
radius = 5 unless radius?
|
465
|
+
|
466
|
+
{x, y, width, height} = bounds if bounds
|
467
|
+
{x, y} = position if position
|
468
|
+
|
469
|
+
context.beginPath()
|
470
|
+
context.moveTo(x + radius, y)
|
471
|
+
context.lineTo(x + width - radius, y)
|
472
|
+
context.quadraticCurveTo(x + width, y, x + width, y + radius)
|
473
|
+
context.lineTo(x + width, y + height - radius)
|
474
|
+
context.quadraticCurveTo(x + width, y + height, x + width - radius, y + height)
|
475
|
+
context.lineTo(x + radius, y + height)
|
476
|
+
context.quadraticCurveTo(x, y + height, x, y + height - radius)
|
477
|
+
context.lineTo(x, y + radius)
|
478
|
+
context.quadraticCurveTo(x, y, x + radius, y)
|
479
|
+
context.closePath()
|
480
|
+
|
481
|
+
if color
|
482
|
+
@fillColor(color)
|
483
|
+
context.fill()
|
484
|
+
|
485
|
+
if stroke
|
486
|
+
@lineWidth(stroke.width)
|
487
|
+
@strokeColor(stroke.color)
|
488
|
+
context.stroke()
|
489
|
+
|
490
|
+
return @
|
491
|
+
|
492
|
+
###*
|
493
|
+
Draws text on the canvas at the given position, in the given color.
|
494
|
+
If no color is given then the previous fill color is used.
|
495
|
+
|
496
|
+
<code class="run"><pre>
|
497
|
+
# Fill canvas to indicate bounds
|
498
|
+
canvas.fill
|
499
|
+
color: '#eee'
|
500
|
+
|
501
|
+
# A line to indicate the baseline
|
502
|
+
canvas.drawLine
|
503
|
+
start: Point(25, 50)
|
504
|
+
end: Point(125, 50)
|
505
|
+
color: "#333"
|
506
|
+
width: 1
|
507
|
+
|
508
|
+
# Draw some text, note the position of the baseline
|
509
|
+
canvas.drawText
|
510
|
+
position: Point(25, 50)
|
511
|
+
color: "red"
|
512
|
+
text: "It's dangerous to go alone"
|
513
|
+
|
514
|
+
</pre></code>
|
515
|
+
|
516
|
+
@name drawText
|
517
|
+
@methodOf PixieCanvas#
|
518
|
+
|
519
|
+
@param {Number} [x] location on x axis to start printing
|
520
|
+
@param {Number} [y] location on y axis to start printing
|
521
|
+
@param {String} text text to print
|
522
|
+
@param {Point} [position] position to start printing. Overrides x and y if passed
|
523
|
+
@param {String|Color} [color] color of text to start printing
|
524
|
+
@param {String} [font] font of text to print
|
525
|
+
|
526
|
+
@returns {PixieCanvas} this
|
527
|
+
###
|
528
|
+
drawText: ({x, y, text, position, color, font}) ->
|
529
|
+
{x, y} = position if position
|
530
|
+
|
531
|
+
@fillColor(color)
|
532
|
+
@font(font) if font
|
533
|
+
context.fillText(text, x, y)
|
534
|
+
|
535
|
+
return @
|
536
|
+
|
537
|
+
###*
|
538
|
+
Centers the given text on the canvas at the given y position. An x position
|
539
|
+
or point position can also be given in which case the text is centered at the
|
540
|
+
x, y or position value specified.
|
541
|
+
|
542
|
+
<code class="run"><pre>
|
543
|
+
# Fill canvas to indicate bounds
|
544
|
+
canvas.fill
|
545
|
+
color: "#eee"
|
546
|
+
|
547
|
+
# A line to indicate the baseline
|
548
|
+
canvas.drawLine
|
549
|
+
start: Point(25, 25)
|
550
|
+
end: Point(125, 25)
|
551
|
+
color: "#333"
|
552
|
+
width: 1
|
553
|
+
|
554
|
+
# Center text on the screen at y value 25
|
555
|
+
canvas.centerText
|
556
|
+
y: 25
|
557
|
+
color: "red"
|
558
|
+
text: "It's dangerous to go alone"
|
559
|
+
|
560
|
+
# Center text at point (75, 75)
|
561
|
+
canvas.centerText
|
562
|
+
position: Point(75, 75)
|
563
|
+
color: "green"
|
564
|
+
text: "take this"
|
565
|
+
|
566
|
+
</pre></code>
|
567
|
+
|
568
|
+
@name centerText
|
569
|
+
@methodOf PixieCanvas#
|
570
|
+
|
571
|
+
@param {String} text Text to print
|
572
|
+
@param {Number} [y] location on the y axis to start printing
|
573
|
+
@param {Number} [x] location on the x axis to start printing. Overrides the default centering behavior if passed
|
574
|
+
@param {Point} [position] position to start printing. Overrides x and y if passed
|
575
|
+
@param {String|Color} [color] color of text to print
|
576
|
+
@param {String} [font] font of text to print
|
577
|
+
|
578
|
+
@returns {PixieCanvas} this
|
579
|
+
###
|
580
|
+
centerText: ({text, x, y, position, color, font}) ->
|
581
|
+
{x, y} = position if position
|
582
|
+
|
583
|
+
x = canvas.width / 2 unless x?
|
584
|
+
|
585
|
+
textWidth = @measureText(text)
|
586
|
+
|
587
|
+
@drawText {
|
588
|
+
text
|
589
|
+
color
|
590
|
+
font
|
591
|
+
x: x - (textWidth) / 2
|
592
|
+
y
|
593
|
+
}
|
594
|
+
|
595
|
+
###*
|
596
|
+
A getter / setter method to set the canvas fillColor.
|
597
|
+
|
598
|
+
<code><pre>
|
599
|
+
# Set the fill color
|
600
|
+
canvas.fillColor('#FF0000')
|
601
|
+
|
602
|
+
# Passing no arguments returns the fillColor
|
603
|
+
canvas.fillColor()
|
604
|
+
# => '#FF0000'
|
605
|
+
|
606
|
+
# You can also pass a Color object
|
607
|
+
canvas.fillColor(Color('sky blue'))
|
608
|
+
</pre></code>
|
609
|
+
|
610
|
+
@name fillColor
|
611
|
+
@methodOf PixieCanvas#
|
612
|
+
|
613
|
+
@param {String|Color} [color] color to make the canvas fillColor
|
614
|
+
|
615
|
+
@returns {PixieCanvas} this
|
616
|
+
###
|
617
|
+
fillColor: (color) ->
|
618
|
+
if color
|
619
|
+
if color.channels
|
620
|
+
context.fillStyle = color.toString()
|
621
|
+
else
|
622
|
+
context.fillStyle = color
|
623
|
+
|
624
|
+
return @
|
625
|
+
else
|
626
|
+
return context.fillStyle
|
627
|
+
|
628
|
+
###*
|
629
|
+
A getter / setter method to set the canvas strokeColor.
|
630
|
+
|
631
|
+
<code><pre>
|
632
|
+
# Set the stroke color
|
633
|
+
canvas.strokeColor('#FF0000')
|
634
|
+
|
635
|
+
# Passing no arguments returns the strokeColor
|
636
|
+
canvas.strokeColor()
|
637
|
+
# => '#FF0000'
|
638
|
+
|
639
|
+
# You can also pass a Color object
|
640
|
+
canvas.strokeColor(Color('sky blue'))
|
641
|
+
</pre></code>
|
642
|
+
|
643
|
+
@name strokeColor
|
644
|
+
@methodOf PixieCanvas#
|
645
|
+
|
646
|
+
@param {String|Color} [color] color to make the canvas strokeColor
|
647
|
+
|
648
|
+
@returns {PixieCanvas} this
|
649
|
+
###
|
650
|
+
strokeColor: (color) ->
|
651
|
+
if color
|
652
|
+
if color.channels
|
653
|
+
context.strokeStyle = color.toString()
|
654
|
+
else
|
655
|
+
context.strokeStyle = color
|
656
|
+
|
657
|
+
return @
|
658
|
+
else
|
659
|
+
return context.strokeStyle
|
660
|
+
|
661
|
+
###*
|
662
|
+
Determine how wide some text is.
|
663
|
+
|
664
|
+
<code><pre>
|
665
|
+
canvas.measureText('Hello World!')
|
666
|
+
# => 55
|
667
|
+
</pre></code>
|
668
|
+
|
669
|
+
@name measureText
|
670
|
+
@methodOf PixieCanvas#
|
671
|
+
|
672
|
+
@param {String} [text] the text to measure
|
673
|
+
|
674
|
+
@returns {PixieCanvas} this
|
675
|
+
###
|
676
|
+
measureText: (text) ->
|
677
|
+
context.measureText(text).width
|
678
|
+
|
679
|
+
putImageData: (imageData, x, y) ->
|
680
|
+
context.putImageData(imageData, x, y)
|
681
|
+
|
682
|
+
return @
|
683
|
+
|
684
|
+
context: ->
|
685
|
+
context
|
686
|
+
|
687
|
+
element: ->
|
688
|
+
canvas
|
689
|
+
|
690
|
+
createPattern: (image, repitition) ->
|
691
|
+
context.createPattern(image, repitition)
|
692
|
+
|
693
|
+
clip: (x, y, width, height) ->
|
694
|
+
context.beginPath()
|
695
|
+
context.rect(x, y, width, height)
|
696
|
+
context.clip()
|
697
|
+
|
698
|
+
return @
|
699
|
+
|
700
|
+
contextAttrAccessor = (attrs...) ->
|
701
|
+
attrs.each (attr) ->
|
702
|
+
$canvas[attr] = (newVal) ->
|
703
|
+
if newVal?
|
704
|
+
context[attr] = newVal
|
705
|
+
return @
|
706
|
+
else
|
707
|
+
context[attr]
|
708
|
+
|
709
|
+
canvasAttrAccessor = (attrs...) ->
|
710
|
+
attrs.each (attr) ->
|
711
|
+
$canvas[attr] = (newVal) ->
|
712
|
+
if newVal?
|
713
|
+
canvas[attr] = newVal
|
714
|
+
return @
|
715
|
+
else
|
716
|
+
canvas[attr]
|
717
|
+
|
718
|
+
contextAttrAccessor(
|
719
|
+
"font",
|
720
|
+
"globalAlpha",
|
721
|
+
"globalCompositeOperation",
|
722
|
+
"lineWidth",
|
723
|
+
"textAlign",
|
724
|
+
)
|
725
|
+
|
726
|
+
canvasAttrAccessor(
|
727
|
+
"height",
|
728
|
+
"width",
|
729
|
+
)
|
730
|
+
|
731
|
+
if canvas?.getContext
|
732
|
+
context = canvas.getContext('2d')
|
733
|
+
|
734
|
+
if options.init
|
735
|
+
options.init($canvas)
|
736
|
+
|
737
|
+
return $canvas
|
738
|
+
|
739
|
+
)(jQuery)
|