|
@@ -0,0 +1,524 @@
|
|
1
|
+/*!
|
|
2
|
+ jCanvas v2.2.1
|
|
3
|
+ Caleb Evans
|
|
4
|
+ 2.2.1 revisions by Thinkyhead
|
|
5
|
+*/
|
|
6
|
+
|
|
7
|
+(function($, document, Math, Number, undefined) {
|
|
8
|
+
|
|
9
|
+// jC global object
|
|
10
|
+var jC = {};
|
|
11
|
+jC.originals = {
|
|
12
|
+ width: 20,
|
|
13
|
+ height: 20,
|
|
14
|
+ cornerRadius: 0,
|
|
15
|
+ fillStyle: 'transparent',
|
|
16
|
+ strokeStyle: 'transparent',
|
|
17
|
+ strokeWidth: 5,
|
|
18
|
+ strokeCap: 'butt',
|
|
19
|
+ strokeJoin: 'miter',
|
|
20
|
+ shadowX: 0,
|
|
21
|
+ shadowY: 0,
|
|
22
|
+ shadowBlur: 10,
|
|
23
|
+ shadowColor: 'transparent',
|
|
24
|
+ x: 0, y: 0,
|
|
25
|
+ x1: 0, y1: 0,
|
|
26
|
+ radius: 10,
|
|
27
|
+ start: 0,
|
|
28
|
+ end: 360,
|
|
29
|
+ ccw: false,
|
|
30
|
+ inDegrees: true,
|
|
31
|
+ fromCenter: true,
|
|
32
|
+ closed: false,
|
|
33
|
+ sides: 3,
|
|
34
|
+ angle: 0,
|
|
35
|
+ text: '',
|
|
36
|
+ font: 'normal 12pt sans-serif',
|
|
37
|
+ align: 'center',
|
|
38
|
+ baseline: 'middle',
|
|
39
|
+ source: '',
|
|
40
|
+ repeat: 'repeat'
|
|
41
|
+};
|
|
42
|
+// Duplicate original defaults
|
|
43
|
+jC.defaults = $.extend({}, jC.originals);
|
|
44
|
+
|
|
45
|
+// Set global properties
|
|
46
|
+function setGlobals(context, map) {
|
|
47
|
+ context.fillStyle = map.fillStyle;
|
|
48
|
+ context.strokeStyle = map.strokeStyle;
|
|
49
|
+ context.lineWidth = map.strokeWidth;
|
|
50
|
+ context.lineCap = map.strokeCap;
|
|
51
|
+ context.lineJoin = map.strokeJoin;
|
|
52
|
+ context.shadowOffsetX = map.shadowX;
|
|
53
|
+ context.shadowOffsetY = map.shadowY;
|
|
54
|
+ context.shadowBlur = map.shadowBlur;
|
|
55
|
+ context.shadowColor = map.shadowColor;
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+// Close path if chosen
|
|
59
|
+function closePath(context, map) {
|
|
60
|
+ if (map.closed === true) {
|
|
61
|
+ context.closePath();
|
|
62
|
+ context.fill();
|
|
63
|
+ context.stroke();
|
|
64
|
+ } else {
|
|
65
|
+ context.fill();
|
|
66
|
+ context.stroke();
|
|
67
|
+ context.closePath();
|
|
68
|
+ }
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+// Measure angles in degrees if chosen
|
|
72
|
+function checkUnits(map) {
|
|
73
|
+ if (map.inDegrees === true) {
|
|
74
|
+ return Math.PI / 180;
|
|
75
|
+ } else {
|
|
76
|
+ return 1;
|
|
77
|
+ }
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+// Set canvas defaults
|
|
81
|
+$.fn.canvas = function(args) {
|
|
82
|
+ // Reset defaults if no value is passed
|
|
83
|
+ if (typeof args === 'undefined') {
|
|
84
|
+ jC.defaults = jC.originals;
|
|
85
|
+ } else {
|
|
86
|
+ jC.defaults = $.extend({}, jC.defaults, args);
|
|
87
|
+ }
|
|
88
|
+ return this;
|
|
89
|
+};
|
|
90
|
+
|
|
91
|
+// Load canvas
|
|
92
|
+$.fn.loadCanvas = function(context) {
|
|
93
|
+ if (typeof context === 'undefined') {context = '2d';}
|
|
94
|
+ return this[0].getContext(context);
|
|
95
|
+};
|
|
96
|
+
|
|
97
|
+// Create gradient
|
|
98
|
+$.fn.gradient = function(args) {
|
|
99
|
+ var ctx = this.loadCanvas(),
|
|
100
|
+ // Specify custom defaults
|
|
101
|
+ gDefaults = {
|
|
102
|
+ x1: 0, y1: 0,
|
|
103
|
+ x2: 0, y2: 0,
|
|
104
|
+ r1: 10, r2: 100
|
|
105
|
+ },
|
|
106
|
+ params = $.extend({}, gDefaults, args),
|
|
107
|
+ gradient, stops = 0, percent, i;
|
|
108
|
+
|
|
109
|
+ // Create radial gradient if chosen
|
|
110
|
+ if (typeof args.r1 === 'undefined' && typeof args.r2 === 'undefined') {
|
|
111
|
+ gradient = ctx.createLinearGradient(params.x1, params.y1, params.x2, params.y2);
|
|
112
|
+ } else {
|
|
113
|
+ gradient = ctx.createRadialGradient(params.x1, params.y1, params.r1, params.x2, params.y2, params.r2);
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ // Count number of color stops
|
|
117
|
+ for (i=1; i<=Number.MAX_VALUE; i+=1) {
|
|
118
|
+ if (params['c' + i]) {
|
|
119
|
+ stops += 1;
|
|
120
|
+ } else {
|
|
121
|
+ break;
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ // Calculate color stop percentages if absent
|
|
126
|
+ for (i=1; i<=stops; i+=1) {
|
|
127
|
+ percent = Math.round((100 / (stops-1)) * (i-1)) / 100;
|
|
128
|
+ if (typeof params['s' + i] === 'undefined') {
|
|
129
|
+ params['s' + i] = percent;
|
|
130
|
+ }
|
|
131
|
+ gradient.addColorStop(params['s' + i], params['c' + i]);
|
|
132
|
+ }
|
|
133
|
+ return gradient;
|
|
134
|
+};
|
|
135
|
+
|
|
136
|
+// Create pattern
|
|
137
|
+$.fn.pattern = function(args) {
|
|
138
|
+ var ctx = this.loadCanvas(),
|
|
139
|
+ params = $.extend({}, jC.defaults, args),
|
|
140
|
+ pattern,
|
|
141
|
+ img = document.createElement('img');
|
|
142
|
+ img.src = params.source;
|
|
143
|
+
|
|
144
|
+ // Create pattern
|
|
145
|
+ function create() {
|
|
146
|
+ if (img.complete === true) {
|
|
147
|
+ // Create pattern
|
|
148
|
+ pattern = ctx.createPattern(img, params.repeat);
|
|
149
|
+ } else {
|
|
150
|
+ throw "The pattern has not loaded yet";
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+ try {
|
|
154
|
+ create();
|
|
155
|
+ } catch(error) {
|
|
156
|
+ img.onload = create;
|
|
157
|
+ }
|
|
158
|
+ return pattern;
|
|
159
|
+};
|
|
160
|
+
|
|
161
|
+// Clear canvas
|
|
162
|
+$.fn.clearCanvas = function(args) {
|
|
163
|
+ var ctx = this.loadCanvas(),
|
|
164
|
+ params = $.extend({}, jC.defaults, args);
|
|
165
|
+
|
|
166
|
+ // Draw from center if chosen
|
|
167
|
+ if (params.fromCenter === true) {
|
|
168
|
+ params.x -= params.width / 2;
|
|
169
|
+ params.y -= params.height / 2;
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ // Clear entire canvas if chosen
|
|
173
|
+ ctx.beginPath();
|
|
174
|
+ if (typeof args === 'undefined') {
|
|
175
|
+ ctx.clearRect(0, 0, this.width(), this.height());
|
|
176
|
+ } else {
|
|
177
|
+ ctx.clearRect(params.x, params.y, params.width, params.height);
|
|
178
|
+ }
|
|
179
|
+ ctx.closePath();
|
|
180
|
+ return this;
|
|
181
|
+};
|
|
182
|
+
|
|
183
|
+// Save canvas
|
|
184
|
+$.fn.saveCanvas = function() {
|
|
185
|
+ var ctx = this.loadCanvas();
|
|
186
|
+ ctx.save();
|
|
187
|
+ return this;
|
|
188
|
+};
|
|
189
|
+
|
|
190
|
+// Restore canvas
|
|
191
|
+$.fn.restoreCanvas = function() {
|
|
192
|
+ var ctx = this.loadCanvas();
|
|
193
|
+ ctx.restore();
|
|
194
|
+ return this;
|
|
195
|
+};
|
|
196
|
+
|
|
197
|
+// Scale canvas
|
|
198
|
+$.fn.scaleCanvas = function(args) {
|
|
199
|
+ var ctx = this.loadCanvas(),
|
|
200
|
+ params = $.extend({}, jC.defaults, args);
|
|
201
|
+ ctx.save();
|
|
202
|
+ ctx.translate(params.x, params.y);
|
|
203
|
+ ctx.scale(params.width, params.height);
|
|
204
|
+ ctx.translate(-params.x, -params.y)
|
|
205
|
+ return this;
|
|
206
|
+};
|
|
207
|
+
|
|
208
|
+// Translate canvas
|
|
209
|
+$.fn.translateCanvas = function(args) {
|
|
210
|
+ var ctx = this.loadCanvas(),
|
|
211
|
+ params = $.extend({}, jC.defaults, args);
|
|
212
|
+ ctx.save();
|
|
213
|
+ ctx.translate(params.x, params.y);
|
|
214
|
+ return this;
|
|
215
|
+};
|
|
216
|
+
|
|
217
|
+// Rotate canvas
|
|
218
|
+$.fn.rotateCanvas = function(args) {
|
|
219
|
+ var ctx = this.loadCanvas(),
|
|
220
|
+ params = $.extend({}, jC.defaults, args),
|
|
221
|
+ toRad = checkUnits(params);
|
|
222
|
+
|
|
223
|
+ ctx.save();
|
|
224
|
+ ctx.translate(params.x, params.y);
|
|
225
|
+ ctx.rotate(params.angle * toRad);
|
|
226
|
+ ctx.translate(-params.x, -params.y);
|
|
227
|
+ return this;
|
|
228
|
+};
|
|
229
|
+
|
|
230
|
+// Draw rectangle
|
|
231
|
+$.fn.drawRect = function(args) {
|
|
232
|
+ var ctx = this.loadCanvas(),
|
|
233
|
+ params = $.extend({}, jC.defaults, args),
|
|
234
|
+ toRad = checkUnits(params),
|
|
235
|
+ x1, y1, x2, y2, r;
|
|
236
|
+ setGlobals(ctx, params);
|
|
237
|
+
|
|
238
|
+ // Draw from center if chosen
|
|
239
|
+ if (params.fromCenter === true) {
|
|
240
|
+ params.x -= params.width / 2;
|
|
241
|
+ params.y -= params.height / 2;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ // Draw rounded rectangle if chosen
|
|
245
|
+ if (params.cornerRadius > 0) {
|
|
246
|
+ x1 = params.x;
|
|
247
|
+ y1 = params.y;
|
|
248
|
+ x2 = params.x + params.width;
|
|
249
|
+ y2 = params.y + params.height;
|
|
250
|
+ r = params.cornerRadius;
|
|
251
|
+ if ((x2 - x1) - (2 * r) < 0) {
|
|
252
|
+ r = (x2 - x1) / 2;
|
|
253
|
+ }
|
|
254
|
+ if ((y2 - y1) - (2 * r) < 0) {
|
|
255
|
+ r = (y2 - y1) / 2;
|
|
256
|
+ }
|
|
257
|
+ ctx.beginPath();
|
|
258
|
+ ctx.moveTo(x1+r,y1);
|
|
259
|
+ ctx.lineTo(x2-r,y1);
|
|
260
|
+ ctx.arc(x2-r, y1+r, r, 270*toRad, 360*toRad, false);
|
|
261
|
+ ctx.lineTo(x2,y2-r);
|
|
262
|
+ ctx.arc(x2-r, y2-r, r, 0, 90*toRad, false);
|
|
263
|
+ ctx.lineTo(x1+r,y2);
|
|
264
|
+ ctx.arc(x1+r, y2-r, r, 90*toRad, 180*toRad, false);
|
|
265
|
+ ctx.lineTo(x1,y1+r);
|
|
266
|
+ ctx.arc(x1+r, y1+r, r, 180*toRad, 270*toRad, false);
|
|
267
|
+ ctx.fill();
|
|
268
|
+ ctx.stroke();
|
|
269
|
+ ctx.closePath();
|
|
270
|
+ } else {
|
|
271
|
+ ctx.fillRect(params.x, params.y, params.width, params.height);
|
|
272
|
+ ctx.strokeRect(params.x, params.y, params.width, params.height);
|
|
273
|
+ }
|
|
274
|
+ return this;
|
|
275
|
+};
|
|
276
|
+
|
|
277
|
+// Draw arc
|
|
278
|
+$.fn.drawArc = function(args) {
|
|
279
|
+ var ctx = this.loadCanvas(),
|
|
280
|
+ params = $.extend({}, jC.defaults, args),
|
|
281
|
+ toRad = checkUnits(params);
|
|
282
|
+ setGlobals(ctx, params);
|
|
283
|
+
|
|
284
|
+ // Draw from center if chosen
|
|
285
|
+ if (params.fromCenter === false) {
|
|
286
|
+ params.x += params.radius;
|
|
287
|
+ params.y += params.radius;
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ ctx.beginPath();
|
|
291
|
+ ctx.arc(params.x, params.y, params.radius, (params.start*toRad)-(Math.PI/2), (params.end*toRad)-(Math.PI/2), params.ccw);
|
|
292
|
+ // Close path if chosen
|
|
293
|
+ closePath(ctx, params);
|
|
294
|
+ return this;
|
|
295
|
+};
|
|
296
|
+
|
|
297
|
+// Draw ellipse
|
|
298
|
+$.fn.drawEllipse = function(args) {
|
|
299
|
+ var ctx = this.loadCanvas(),
|
|
300
|
+ params = $.extend({}, jC.defaults, args),
|
|
301
|
+ controlW = params.width * (4/3);
|
|
302
|
+ setGlobals(ctx, params);
|
|
303
|
+
|
|
304
|
+ // Draw from center if chosen
|
|
305
|
+ if (params.fromCenter === false) {
|
|
306
|
+ params.x += params.width / 2;
|
|
307
|
+ params.y += params.height / 2;
|
|
308
|
+ }
|
|
309
|
+
|
|
310
|
+ // Increment coordinates to prevent negative values
|
|
311
|
+ params.x += 1e-10;
|
|
312
|
+ params.y += 1e-10;
|
|
313
|
+
|
|
314
|
+ // Create ellipse
|
|
315
|
+ ctx.beginPath();
|
|
316
|
+ ctx.moveTo(params.x, params.y-params.height/2);
|
|
317
|
+ ctx.bezierCurveTo(params.x-controlW/2,params.y-params.height/2,
|
|
318
|
+ params.x-controlW/2,params.y+params.height/2,
|
|
319
|
+ params.x,params.y+params.height/2);
|
|
320
|
+ ctx.bezierCurveTo(params.x+controlW/2,params.y+params.height/2,
|
|
321
|
+ params.x+controlW/2,params.y-params.height/2,
|
|
322
|
+ params.x,params.y-params.height/2);
|
|
323
|
+ ctx.closePath();
|
|
324
|
+ ctx.fill();
|
|
325
|
+ ctx.stroke();
|
|
326
|
+ return this;
|
|
327
|
+};
|
|
328
|
+
|
|
329
|
+// Draw line
|
|
330
|
+$.fn.drawLine = function(args) {
|
|
331
|
+ var ctx = this.loadCanvas(),
|
|
332
|
+ params = $.extend({}, jC.defaults, args),
|
|
333
|
+ max = Number.MAX_VALUE, l,
|
|
334
|
+ lx, ly;
|
|
335
|
+ setGlobals(ctx, params);
|
|
336
|
+
|
|
337
|
+ // Draw each point
|
|
338
|
+ ctx.beginPath();
|
|
339
|
+ ctx.moveTo(params.x1, params.y1);
|
|
340
|
+ for (l=2; l<max; l+=1) {
|
|
341
|
+ lx = params['x' + l];
|
|
342
|
+ ly = params['y' + l];
|
|
343
|
+ // Stop loop when all points are drawn
|
|
344
|
+ if (typeof lx === 'undefined' || typeof ly === 'undefined') {
|
|
345
|
+ break;
|
|
346
|
+ }
|
|
347
|
+ ctx.lineTo(lx, ly);
|
|
348
|
+ }
|
|
349
|
+ // Close path if chosen
|
|
350
|
+ closePath(ctx, params);
|
|
351
|
+ return this;
|
|
352
|
+};
|
|
353
|
+
|
|
354
|
+// Draw quadratic curve
|
|
355
|
+$.fn.drawQuad = function(args) {
|
|
356
|
+ var ctx = this.loadCanvas(),
|
|
357
|
+ params = $.extend({}, jC.defaults, args),
|
|
358
|
+ max = Number.MAX_VALUE, l,
|
|
359
|
+ lx, ly, lcx, lcy;
|
|
360
|
+ setGlobals(ctx, params);
|
|
361
|
+
|
|
362
|
+ // Draw each point
|
|
363
|
+ ctx.beginPath();
|
|
364
|
+ ctx.moveTo(params.x1, params.y1);
|
|
365
|
+ for (l=2; l<max; l+=1) {
|
|
366
|
+ lx = params['x' + l];
|
|
367
|
+ if (typeof lx === 'undefined') break;
|
|
368
|
+ ly = params['y' + l];
|
|
369
|
+ if (typeof ly === 'undefined') break;
|
|
370
|
+ lcx = params['cx' + (l-1)];
|
|
371
|
+ if (typeof lcx === 'undefined') break;
|
|
372
|
+ lcy = params['cy' + (l-1)];
|
|
373
|
+ if (typeof lcy === 'undefined') break;
|
|
374
|
+ ctx.quadraticCurveTo(lcx, lcy, lx, ly);
|
|
375
|
+ }
|
|
376
|
+ // Close path if chosen
|
|
377
|
+ closePath(ctx, params);
|
|
378
|
+ return this;
|
|
379
|
+};
|
|
380
|
+
|
|
381
|
+// Draw Bezier curve
|
|
382
|
+$.fn.drawBezier = function(args) {
|
|
383
|
+ var ctx = this.loadCanvas(),
|
|
384
|
+ params = $.extend({}, jC.defaults, args),
|
|
385
|
+ max = Number.MAX_VALUE,
|
|
386
|
+ l=2, lc=1, lx, ly, lcx1, lcy1, lcx2, lcy2, i;
|
|
387
|
+ setGlobals(ctx, params);
|
|
388
|
+
|
|
389
|
+ // Draw each point
|
|
390
|
+ ctx.beginPath();
|
|
391
|
+ ctx.moveTo(params.x1, params.y1);
|
|
392
|
+ for (i=2; i<max; i+=1) {
|
|
393
|
+ lx = params['x' + l];
|
|
394
|
+ if (typeof lx === 'undefined') break;
|
|
395
|
+ ly = params['y' + l];
|
|
396
|
+ if (typeof ly === 'undefined') break;
|
|
397
|
+ lcx1 = params['cx' + lc];
|
|
398
|
+ if (typeof lcx1 === 'undefined') break;
|
|
399
|
+ lcy1 = params['cy' + lc];
|
|
400
|
+ if (typeof lcy1 === 'undefined') break;
|
|
401
|
+ lcx2 = params['cx' + (lc+1)];
|
|
402
|
+ if (typeof lcx2 === 'undefined') break;
|
|
403
|
+ lcy2 = params['cy' + (lc+1)];
|
|
404
|
+ if (typeof lcy2 === 'undefined') break;
|
|
405
|
+ ctx.bezierCurveTo(lcx1, lcy1, lcx2, lcy2, lx, ly);
|
|
406
|
+ l += 1;
|
|
407
|
+ lc += 2;
|
|
408
|
+ }
|
|
409
|
+ // Close path if chosen
|
|
410
|
+ closePath(ctx, params);
|
|
411
|
+ return this;
|
|
412
|
+};
|
|
413
|
+
|
|
414
|
+// Draw text
|
|
415
|
+$.fn.drawText = function(args) {
|
|
416
|
+ var ctx = this.loadCanvas(),
|
|
417
|
+ params = $.extend({}, jC.defaults, args);
|
|
418
|
+ setGlobals(ctx, params);
|
|
419
|
+
|
|
420
|
+ // Set text-specific properties
|
|
421
|
+ ctx.textBaseline = params.baseline;
|
|
422
|
+ ctx.textAlign = params.align;
|
|
423
|
+ ctx.font = params.font;
|
|
424
|
+
|
|
425
|
+ ctx.strokeText(params.text, params.x, params.y);
|
|
426
|
+ ctx.fillText(params.text, params.x, params.y);
|
|
427
|
+ return this;
|
|
428
|
+};
|
|
429
|
+
|
|
430
|
+// Draw image
|
|
431
|
+$.fn.drawImage = function(args) {
|
|
432
|
+ var ctx = this.loadCanvas(),
|
|
433
|
+ params = $.extend({}, jC.defaults, args),
|
|
434
|
+ // Define image source
|
|
435
|
+ img = document.createElement('img');
|
|
436
|
+ img.src = params.source;
|
|
437
|
+ setGlobals(ctx, params);
|
|
438
|
+
|
|
439
|
+ // Draw image function
|
|
440
|
+ function draw() {
|
|
441
|
+ if (img.complete) {
|
|
442
|
+
|
|
443
|
+ var scaleFac = img.width / img.height;
|
|
444
|
+
|
|
445
|
+ // If width/height are specified
|
|
446
|
+ if (typeof args.width !== 'undefined' && typeof args.height !== 'undefined') {
|
|
447
|
+ img.width = args.width;
|
|
448
|
+ img.height = args.height;
|
|
449
|
+ // If width is specified
|
|
450
|
+ } else if (typeof args.width !== 'undefined' && typeof args.height === 'undefined') {
|
|
451
|
+ img.width = args.width;
|
|
452
|
+ img.height = img.width / scaleFac;
|
|
453
|
+ // If height is specified
|
|
454
|
+ } else if (typeof args.width === 'undefined' && typeof args.height !== 'undefined') {
|
|
455
|
+ img.height = args.height;
|
|
456
|
+ img.width = img.height * scaleFac;
|
|
457
|
+ }
|
|
458
|
+
|
|
459
|
+ // Draw from center if chosen
|
|
460
|
+ if (params.fromCenter === true) {
|
|
461
|
+ params.x -= img.width / 2;
|
|
462
|
+ params.y -= img.height / 2;
|
|
463
|
+ }
|
|
464
|
+
|
|
465
|
+ // Draw image
|
|
466
|
+ ctx.drawImage(img, params.x, params.y, img.width, img.height);
|
|
467
|
+ } else {
|
|
468
|
+ throw "The image has not loaded yet.";
|
|
469
|
+ }
|
|
470
|
+ }
|
|
471
|
+
|
|
472
|
+ function dodraw() {
|
|
473
|
+ // console.log("dodraw...");
|
|
474
|
+ try {
|
|
475
|
+ // console.log("dodraw...try...");
|
|
476
|
+ draw();
|
|
477
|
+ }
|
|
478
|
+ catch(error) {
|
|
479
|
+ // console.log("dodraw...catch: " + error);
|
|
480
|
+ }
|
|
481
|
+ }
|
|
482
|
+
|
|
483
|
+ // Draw image if already loaded
|
|
484
|
+ // console.log("--------------------");
|
|
485
|
+ // console.log("drawImage " + img.src);
|
|
486
|
+ try {
|
|
487
|
+ // console.log("try...");
|
|
488
|
+ draw();
|
|
489
|
+ } catch(error) {
|
|
490
|
+ // console.log("catch: " + error);
|
|
491
|
+ img.onload = dodraw;
|
|
492
|
+ }
|
|
493
|
+ return this;
|
|
494
|
+};
|
|
495
|
+
|
|
496
|
+// Draw polygon
|
|
497
|
+$.fn.drawPolygon = function(args) {
|
|
498
|
+ var ctx = this.loadCanvas(),
|
|
499
|
+ params = $.extend({}, jC.defaults, args),
|
|
500
|
+ theta, dtheta, x, y,
|
|
501
|
+ toRad = checkUnits(params), i;
|
|
502
|
+ setGlobals(ctx, params);
|
|
503
|
+
|
|
504
|
+ if (params.sides >= 3) {
|
|
505
|
+ // Calculate points and draw
|
|
506
|
+ theta = (Math.PI/2) + (Math.PI/params.sides) + (params.angle*toRad);
|
|
507
|
+ dtheta = (Math.PI*2) / params.sides;
|
|
508
|
+ for (i=0; i<params.sides; i+=1) {
|
|
509
|
+ x = params.x + (params.radius * Math.cos(theta)) + 1e-10;
|
|
510
|
+ y = params.y + (params.radius * Math.sin(theta)) + 1e-10;
|
|
511
|
+ if (params.fromCenter === false) {
|
|
512
|
+ x += params.radius;
|
|
513
|
+ y += params.radius;
|
|
514
|
+ }
|
|
515
|
+ ctx.lineTo(x, y);
|
|
516
|
+ theta += dtheta;
|
|
517
|
+ }
|
|
518
|
+ closePath(ctx, params);
|
|
519
|
+ }
|
|
520
|
+ return this;
|
|
521
|
+};
|
|
522
|
+
|
|
523
|
+return window.jCanvas = jC;
|
|
524
|
+}(jQuery, document, Math, Number));
|