fabric-rails 1.0.12.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/CHANGELOG.md +4 -0
  2. data/README.md +1 -1
  3. data/lib/fabric/rails/version.rb +2 -2
  4. data/vendor/assets/javascripts/cufon.js +1226 -0
  5. data/vendor/assets/javascripts/event.js +20 -20
  6. data/vendor/assets/javascripts/excanvas.js +1464 -0
  7. data/vendor/assets/javascripts/fabric.js +56 -33
  8. data/vendor/assets/javascripts/fabric/HEADER.js +2 -4
  9. data/vendor/assets/javascripts/fabric/brushes/base_brush.class.js +96 -0
  10. data/vendor/assets/javascripts/fabric/brushes/circle_brush.class.js +99 -0
  11. data/vendor/assets/javascripts/fabric/brushes/pattern_brush.class.js +55 -0
  12. data/vendor/assets/javascripts/fabric/{freedrawing.class.js → brushes/pencil_brush.class.js} +73 -65
  13. data/vendor/assets/javascripts/fabric/brushes/spray_brush.class.js +157 -0
  14. data/vendor/assets/javascripts/fabric/canvas.class.js +154 -135
  15. data/vendor/assets/javascripts/fabric/color.class.js +195 -29
  16. data/vendor/assets/javascripts/fabric/filters/brightness_filter.class.js +70 -0
  17. data/vendor/assets/javascripts/fabric/filters/convolute_filter.class.js +122 -0
  18. data/vendor/assets/javascripts/fabric/filters/gradienttransparency_filter.class.js +69 -0
  19. data/vendor/assets/javascripts/fabric/filters/grayscale_filter.class.js +61 -0
  20. data/vendor/assets/javascripts/fabric/filters/invert_filter.class.js +57 -0
  21. data/vendor/assets/javascripts/fabric/filters/noise_filter.class.js +73 -0
  22. data/vendor/assets/javascripts/fabric/filters/pixelate_filter.class.js +98 -0
  23. data/vendor/assets/javascripts/fabric/filters/removewhite_filter.class.js +86 -0
  24. data/vendor/assets/javascripts/fabric/filters/sepia2_filter.class.js +61 -0
  25. data/vendor/assets/javascripts/fabric/filters/sepia_filter.class.js +58 -0
  26. data/vendor/assets/javascripts/fabric/filters/tint_filter.class.js +80 -0
  27. data/vendor/assets/javascripts/fabric/gradient.class.js +232 -80
  28. data/vendor/assets/javascripts/fabric/intersection.class.js +10 -28
  29. data/vendor/assets/javascripts/fabric/log.js +0 -2
  30. data/vendor/assets/javascripts/fabric/{canvas_animation.mixin.js → mixins/canvas_animation.mixin.js} +3 -6
  31. data/vendor/assets/javascripts/fabric/mixins/canvas_dataurl_exporter.mixin.js +137 -0
  32. data/vendor/assets/javascripts/fabric/{canvas_events.mixin.js → mixins/canvas_events.mixin.js} +97 -144
  33. data/vendor/assets/javascripts/fabric/{canvas_gestures.mixin.js → mixins/canvas_gestures.mixin.js} +4 -8
  34. data/vendor/assets/javascripts/fabric/{canvas_serialization.mixin.js → mixins/canvas_serialization.mixin.js} +19 -14
  35. data/vendor/assets/javascripts/fabric/mixins/collection.mixin.js +137 -0
  36. data/vendor/assets/javascripts/fabric/{object_geometry.mixin.js → mixins/object_geometry.mixin.js} +130 -47
  37. data/vendor/assets/javascripts/fabric/{object_interactivity.mixin.js → mixins/object_interactivity.mixin.js} +21 -102
  38. data/vendor/assets/javascripts/fabric/{object_origin.mixin.js → mixins/object_origin.mixin.js} +36 -26
  39. data/vendor/assets/javascripts/fabric/{object_straightening.mixin.js → mixins/object_straightening.mixin.js} +4 -9
  40. data/vendor/assets/javascripts/fabric/{observable.mixin.js → mixins/observable.mixin.js} +27 -35
  41. data/vendor/assets/javascripts/fabric/mixins/stateful.mixin.js +45 -0
  42. data/vendor/assets/javascripts/fabric/node.js +62 -26
  43. data/vendor/assets/javascripts/fabric/parser.js +181 -58
  44. data/vendor/assets/javascripts/fabric/pattern.class.js +43 -14
  45. data/vendor/assets/javascripts/fabric/point.class.js +4 -43
  46. data/vendor/assets/javascripts/fabric/shadow.class.js +19 -19
  47. data/vendor/assets/javascripts/fabric/{circle.class.js → shapes/circle.class.js} +32 -29
  48. data/vendor/assets/javascripts/fabric/{ellipse.class.js → shapes/ellipse.class.js} +45 -27
  49. data/vendor/assets/javascripts/fabric/{group.class.js → shapes/group.class.js} +67 -169
  50. data/vendor/assets/javascripts/fabric/{image.class.js → shapes/image.class.js} +134 -72
  51. data/vendor/assets/javascripts/fabric/{line.class.js → shapes/line.class.js} +67 -36
  52. data/vendor/assets/javascripts/fabric/{object.class.js → shapes/object.class.js} +394 -252
  53. data/vendor/assets/javascripts/fabric/{path.class.js → shapes/path.class.js} +89 -174
  54. data/vendor/assets/javascripts/fabric/{path_group.class.js → shapes/path_group.class.js} +12 -18
  55. data/vendor/assets/javascripts/fabric/{polygon.class.js → shapes/polygon.class.js} +64 -38
  56. data/vendor/assets/javascripts/fabric/{polyline.class.js → shapes/polyline.class.js} +64 -39
  57. data/vendor/assets/javascripts/fabric/{rect.class.js → shapes/rect.class.js} +78 -112
  58. data/vendor/assets/javascripts/fabric/{text.class.js → shapes/text.class.js} +264 -270
  59. data/vendor/assets/javascripts/fabric/shapes/text.cufon.js +79 -0
  60. data/vendor/assets/javascripts/fabric/{triangle.class.js → shapes/triangle.class.js} +46 -26
  61. data/vendor/assets/javascripts/fabric/static_canvas.class.js +134 -358
  62. data/vendor/assets/javascripts/fabric/util/anim_ease.js +2 -31
  63. data/vendor/assets/javascripts/fabric/util/dom_event.js +21 -7
  64. data/vendor/assets/javascripts/fabric/util/dom_misc.js +49 -39
  65. data/vendor/assets/javascripts/fabric/util/dom_request.js +1 -2
  66. data/vendor/assets/javascripts/fabric/util/dom_style.js +1 -2
  67. data/vendor/assets/javascripts/fabric/util/lang_array.js +19 -13
  68. data/vendor/assets/javascripts/fabric/util/lang_class.js +1 -2
  69. data/vendor/assets/javascripts/fabric/util/lang_function.js +3 -1
  70. data/vendor/assets/javascripts/fabric/util/lang_object.js +5 -5
  71. data/vendor/assets/javascripts/fabric/util/lang_string.js +7 -5
  72. data/vendor/assets/javascripts/fabric/util/misc.js +207 -42
  73. metadata +47 -29
  74. data/vendor/assets/javascripts/fabric/image_filters.js +0 -809
  75. data/vendor/assets/javascripts/fabric/scout.js +0 -45
  76. data/vendor/assets/javascripts/fabric/stateful.js +0 -88
@@ -9,9 +9,9 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
9
9
  ----------------------------------------------------
10
10
  https://github.com/mudcube/Event.js
11
11
  ----------------------------------------------------
12
- 1 : click, dblclick, dbltap
13
- 1+ : tap, longpress, drag, swipe
14
- 2+ : pinch, rotate
12
+ 1 : click, dblclick, dbltap
13
+ 1+ : tap, longpress, drag, swipe
14
+ 2+ : pinch, rotate
15
15
  : mousewheel, devicemotion, shake
16
16
  ----------------------------------------------------
17
17
  TODO
@@ -26,7 +26,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
26
26
  ----------------------------------------------------
27
27
  REQUIREMENTS: querySelector, querySelectorAll
28
28
  ----------------------------------------------------
29
- * There are two ways to add/remove events with this library.
29
+ * There are two ways to add/remove events with this library.
30
30
  ----------------------------------------------------
31
31
  // Retains "this" attribute as target, and overrides native addEventListener.
32
32
  target.addEventListener(type, listener, useCapture);
@@ -36,12 +36,12 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
36
36
  Event.add(type, listener, configure);
37
37
  Event.remove(type, listener, configure);
38
38
 
39
- * You can turn prototyping on/off for individual features.
39
+ * You can turn prototyping on/off for individual features.
40
40
  ----------------------------------------------------
41
41
  Event.modifyEventListener = true; // add custom *EventListener commands to HTMLElements.
42
42
  Event.modifySelectors = true; // add bulk *EventListener commands on NodeLists from querySelectorAll and others.
43
43
 
44
- * Example of setting up a single listener with a custom configuration.
44
+ * Example of setting up a single listener with a custom configuration.
45
45
  ----------------------------------------------------
46
46
  // optional configuration.
47
47
  var configure = {
@@ -60,7 +60,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
60
60
  console.log(self.velocity, self.angle, self.fingers);
61
61
  }, configure);
62
62
 
63
- * Multiple listeners glued together.
63
+ * Multiple listeners glued together.
64
64
  ----------------------------------------------------
65
65
  // adding with addEventListener()
66
66
  target.addEventListener("click swipe", function(event) { });
@@ -68,7 +68,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
68
68
  // adding with Event.add()
69
69
  Event.add(target, "click swipe", function(event, self) { });
70
70
 
71
- * Use query selectors to create an event (querySelectorAll)
71
+ * Use query selectors to create an event (querySelectorAll)
72
72
  ----------------------------------------------------
73
73
  // adding events to NodeList from querySelectorAll()
74
74
  document.querySelectorAll("#element a.link").addEventListener("click", callback);
@@ -76,7 +76,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
76
76
  // adding with Event.add()
77
77
  Event.add("#element a.link", "click", callback);
78
78
 
79
- * Listen for selector to become available (querySelector)
79
+ * Listen for selector to become available (querySelector)
80
80
  ----------------------------------------------------
81
81
  Event.add("body", "ready", callback);
82
82
  // or...
@@ -88,7 +88,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
88
88
  listener: callback
89
89
  });
90
90
 
91
- * Multiple listeners bound to one callback w/ single configuration.
91
+ * Multiple listeners bound to one callback w/ single configuration.
92
92
  ----------------------------------------------------
93
93
  var bindings = Event.add({
94
94
  target: target,
@@ -109,7 +109,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
109
109
  }
110
110
  });
111
111
 
112
- * Multiple listeners bound to multiple callbacks w/ single configuration.
112
+ * Multiple listeners bound to multiple callbacks w/ single configuration.
113
113
  ----------------------------------------------------
114
114
  var bindings = Event.add({
115
115
  target: target,
@@ -125,7 +125,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
125
125
  }
126
126
  });
127
127
 
128
- * Multiple listeners bound to multiple callbacks w/ multiple configurations.
128
+ * Multiple listeners bound to multiple callbacks w/ multiple configurations.
129
129
  ----------------------------------------------------
130
130
  var binding = Event.add({
131
131
  target: target,
@@ -148,7 +148,7 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
148
148
  }
149
149
  });
150
150
 
151
- * Capturing an event and manually forwarding it to a proxy (tiered events).
151
+ * Capturing an event and manually forwarding it to a proxy (tiered events).
152
152
  ----------------------------------------------------
153
153
  Event.add(target, "down", function(event, self) {
154
154
  var x = event.pageX; // local variables that wont change.
@@ -164,9 +164,9 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
164
164
  });
165
165
  ----------------------------------------------------
166
166
 
167
- * Event proxies.
168
- * type, fingers, state, start, x, y, position, bbox
169
- * rotation, scale, velocity, angle, delay, timeout
167
+ * Event proxies.
168
+ * type, fingers, state, start, x, y, position, bbox
169
+ * rotation, scale, velocity, angle, delay, timeout
170
170
  ----------------------------------------------------
171
171
  // "Click" :: fingers, minFingers, maxFingers.
172
172
  Event.add(window, "click", function(event, self) {
@@ -209,18 +209,18 @@ if (typeof document == 'undefined' && typeof navigator == 'undefined') {
209
209
  console.log(self.gesture, self.state, self.wheelDelta);
210
210
  });
211
211
 
212
- * Stop, prevent and cancel.
212
+ * Stop, prevent and cancel.
213
213
  ----------------------------------------------------
214
214
  Event.stop(event); // stop bubble.
215
215
  Event.prevent(event); // prevent default.
216
216
  Event.cancel(event); // stop and prevent.
217
217
 
218
- * Track for proper command/control-key for Mac/PC.
218
+ * Track for proper command/control-key for Mac/PC.
219
219
  ----------------------------------------------------
220
220
  Event.add(window, "keyup keydown", Event.proxy.metaTracker);
221
221
  console.log(Event.proxy.metaKey);
222
222
 
223
- * Test for event features, in this example Drag & Drop file support.
223
+ * Test for event features, in this example Drag & Drop file support.
224
224
  ----------------------------------------------------
225
225
  console.log(Event.supports('dragstart') && Event.supports('drop') && !!window.FileReader);
226
226
 
@@ -813,7 +813,7 @@ Event.proxy = (function(root) {
813
813
  track.up = true;
814
814
  conf.fingers--;
815
815
  }
816
- /* // This should work but fails in Safari on iOS4 so not using it.
816
+ /* // This should work but fails in Safari on iOS4 so not using it.
817
817
  var touches = event.changedTouches || root.getCoords(event);
818
818
  var length = touches.length;
819
819
  // Record changed touches have ended (this should work).
@@ -0,0 +1,1464 @@
1
+ // Copyright 2006 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+
16
+ // Known Issues:
17
+ //
18
+ // * Patterns only support repeat.
19
+ // * Radial gradient are not implemented. The VML version of these look very
20
+ // different from the canvas one.
21
+ // * Clipping paths are not implemented.
22
+ // * Coordsize. The width and height attribute have higher priority than the
23
+ // width and height style values which isn't correct.
24
+ // * Painting mode isn't implemented.
25
+ // * Canvas width/height should is using content-box by default. IE in
26
+ // Quirks mode will draw the canvas using border-box. Either change your
27
+ // doctype to HTML5
28
+ // (http://www.whatwg.org/specs/web-apps/current-work/#the-doctype)
29
+ // or use Box Sizing Behavior from WebFX
30
+ // (http://webfx.eae.net/dhtml/boxsizing/boxsizing.html)
31
+ // * Non uniform scaling does not correctly scale strokes.
32
+ // * Optimize. There is always room for speed improvements.
33
+
34
+ // Only add this code if we do not already have a canvas implementation
35
+ if (!document.createElement('canvas').getContext) {
36
+
37
+ (function() {
38
+
39
+ // alias some functions to make (compiled) code shorter
40
+ var m = Math;
41
+ var mr = m.round;
42
+ var ms = m.sin;
43
+ var mc = m.cos;
44
+ var abs = m.abs;
45
+ var sqrt = m.sqrt;
46
+
47
+ // this is used for sub pixel precision
48
+ var Z = 10;
49
+ var Z2 = Z / 2;
50
+
51
+ var IE_VERSION = +navigator.userAgent.match(/MSIE ([\d.]+)?/)[1];
52
+
53
+ /**
54
+ * This funtion is assigned to the <canvas> elements as element.getContext().
55
+ * @this {HTMLElement}
56
+ * @return {CanvasRenderingContext2D_}
57
+ */
58
+ function getContext() {
59
+ return this.context_ ||
60
+ (this.context_ = new CanvasRenderingContext2D_(this));
61
+ }
62
+
63
+ var slice = Array.prototype.slice;
64
+
65
+ /**
66
+ * Binds a function to an object. The returned function will always use the
67
+ * passed in {@code obj} as {@code this}.
68
+ *
69
+ * Example:
70
+ *
71
+ * g = bind(f, obj, a, b)
72
+ * g(c, d) // will do f.call(obj, a, b, c, d)
73
+ *
74
+ * @param {Function} f The function to bind the object to
75
+ * @param {Object} obj The object that should act as this when the function
76
+ * is called
77
+ * @param {*} var_args Rest arguments that will be used as the initial
78
+ * arguments when the function is called
79
+ * @return {Function} A new function that has bound this
80
+ */
81
+ function bind(f, obj, var_args) {
82
+ var a = slice.call(arguments, 2);
83
+ return function() {
84
+ return f.apply(obj, a.concat(slice.call(arguments)));
85
+ };
86
+ }
87
+
88
+ function encodeHtmlAttribute(s) {
89
+ return String(s).replace(/&/g, '&amp;').replace(/"/g, '&quot;');
90
+ }
91
+
92
+ function addNamespace(doc, prefix, urn) {
93
+ if (!doc.namespaces[prefix]) {
94
+ doc.namespaces.add(prefix, urn, '#default#VML');
95
+ }
96
+ }
97
+
98
+ function addNamespacesAndStylesheet(doc) {
99
+ addNamespace(doc, 'g_vml_', 'urn:schemas-microsoft-com:vml');
100
+ addNamespace(doc, 'g_o_', 'urn:schemas-microsoft-com:office:office');
101
+
102
+ // Setup default CSS. Only add one style sheet per document
103
+ if (!doc.styleSheets['ex_canvas_']) {
104
+ var ss = doc.createStyleSheet();
105
+ ss.owningElement.id = 'ex_canvas_';
106
+ ss.cssText = 'canvas{display:inline-block;overflow:hidden;' +
107
+ // default size is 300x150 in Gecko and Opera
108
+ 'text-align:left;width:300px;height:150px}';
109
+ }
110
+ }
111
+
112
+ // Add namespaces and stylesheet at startup.
113
+ addNamespacesAndStylesheet(document);
114
+
115
+ var G_vmlCanvasManager_ = {
116
+ init: function(opt_doc) {
117
+ var doc = opt_doc || document;
118
+ // Create a dummy element so that IE will allow canvas elements to be
119
+ // recognized.
120
+ doc.createElement('canvas');
121
+ doc.attachEvent('onreadystatechange', bind(this.init_, this, doc));
122
+ },
123
+
124
+ init_: function(doc) {
125
+ // find all canvas elements
126
+ var els = doc.getElementsByTagName('canvas');
127
+ for (var i = 0; i < els.length; i++) {
128
+ this.initElement(els[i]);
129
+ }
130
+ },
131
+
132
+ /**
133
+ * Public initializes a canvas element so that it can be used as canvas
134
+ * element from now on. This is called automatically before the page is
135
+ * loaded but if you are creating elements using createElement you need to
136
+ * make sure this is called on the element.
137
+ * @param {HTMLElement} el The canvas element to initialize.
138
+ * @return {HTMLElement} the element that was created.
139
+ */
140
+ initElement: function(el) {
141
+ if (!el.getContext) {
142
+ el.getContext = getContext;
143
+
144
+ // Add namespaces and stylesheet to document of the element.
145
+ addNamespacesAndStylesheet(el.ownerDocument);
146
+
147
+ // Remove fallback content. There is no way to hide text nodes so we
148
+ // just remove all childNodes. We could hide all elements and remove
149
+ // text nodes but who really cares about the fallback content.
150
+ el.innerHTML = '';
151
+
152
+ // do not use inline function because that will leak memory
153
+ el.attachEvent('onpropertychange', onPropertyChange);
154
+ el.attachEvent('onresize', onResize);
155
+
156
+ var attrs = el.attributes;
157
+ if (attrs.width && attrs.width.specified) {
158
+ // TODO: use runtimeStyle and coordsize
159
+ // el.getContext().setWidth_(attrs.width.nodeValue);
160
+ el.style.width = attrs.width.nodeValue + 'px';
161
+ } else {
162
+ el.width = el.clientWidth;
163
+ }
164
+ if (attrs.height && attrs.height.specified) {
165
+ // TODO: use runtimeStyle and coordsize
166
+ // el.getContext().setHeight_(attrs.height.nodeValue);
167
+ el.style.height = attrs.height.nodeValue + 'px';
168
+ } else {
169
+ el.height = el.clientHeight;
170
+ }
171
+ //el.getContext().setCoordsize_()
172
+ }
173
+ return el;
174
+ }
175
+ };
176
+
177
+ function onPropertyChange(e) {
178
+ var el = e.srcElement;
179
+
180
+ switch (e.propertyName) {
181
+ case 'width':
182
+ el.getContext().clearRect();
183
+ el.style.width = el.attributes.width.nodeValue + 'px';
184
+ // In IE8 this does not trigger onresize.
185
+ el.firstChild.style.width = el.clientWidth + 'px';
186
+ break;
187
+ case 'height':
188
+ el.getContext().clearRect();
189
+ el.style.height = el.attributes.height.nodeValue + 'px';
190
+ el.firstChild.style.height = el.clientHeight + 'px';
191
+ break;
192
+ }
193
+ }
194
+
195
+ function onResize(e) {
196
+ var el = e.srcElement;
197
+ if (el.firstChild) {
198
+ el.firstChild.style.width = el.clientWidth + 'px';
199
+ el.firstChild.style.height = el.clientHeight + 'px';
200
+ }
201
+ }
202
+
203
+ G_vmlCanvasManager_.init();
204
+
205
+ // precompute "00" to "FF"
206
+ var decToHex = [];
207
+ for (var i = 0; i < 16; i++) {
208
+ for (var j = 0; j < 16; j++) {
209
+ decToHex[i * 16 + j] = i.toString(16) + j.toString(16);
210
+ }
211
+ }
212
+
213
+ function createMatrixIdentity() {
214
+ return [
215
+ [1, 0, 0],
216
+ [0, 1, 0],
217
+ [0, 0, 1]
218
+ ];
219
+ }
220
+
221
+ function matrixMultiply(m1, m2) {
222
+ var result = createMatrixIdentity();
223
+
224
+ for (var x = 0; x < 3; x++) {
225
+ for (var y = 0; y < 3; y++) {
226
+ var sum = 0;
227
+
228
+ for (var z = 0; z < 3; z++) {
229
+ sum += m1[x][z] * m2[z][y];
230
+ }
231
+
232
+ result[x][y] = sum;
233
+ }
234
+ }
235
+ return result;
236
+ }
237
+
238
+ function copyState(o1, o2) {
239
+ o2.fillStyle = o1.fillStyle;
240
+ o2.lineCap = o1.lineCap;
241
+ o2.lineJoin = o1.lineJoin;
242
+ o2.lineWidth = o1.lineWidth;
243
+ o2.miterLimit = o1.miterLimit;
244
+ o2.shadowBlur = o1.shadowBlur;
245
+ o2.shadowColor = o1.shadowColor;
246
+ o2.shadowOffsetX = o1.shadowOffsetX;
247
+ o2.shadowOffsetY = o1.shadowOffsetY;
248
+ o2.strokeStyle = o1.strokeStyle;
249
+ o2.globalAlpha = o1.globalAlpha;
250
+ o2.font = o1.font;
251
+ o2.textAlign = o1.textAlign;
252
+ o2.textBaseline = o1.textBaseline;
253
+ o2.arcScaleX_ = o1.arcScaleX_;
254
+ o2.arcScaleY_ = o1.arcScaleY_;
255
+ o2.lineScale_ = o1.lineScale_;
256
+ o2.rotation_ = o1.rotation_; // used for images
257
+ }
258
+
259
+ var colorData = {
260
+ aliceblue: '#F0F8FF',
261
+ antiquewhite: '#FAEBD7',
262
+ aquamarine: '#7FFFD4',
263
+ azure: '#F0FFFF',
264
+ beige: '#F5F5DC',
265
+ bisque: '#FFE4C4',
266
+ black: '#000000',
267
+ blanchedalmond: '#FFEBCD',
268
+ blueviolet: '#8A2BE2',
269
+ brown: '#A52A2A',
270
+ burlywood: '#DEB887',
271
+ cadetblue: '#5F9EA0',
272
+ chartreuse: '#7FFF00',
273
+ chocolate: '#D2691E',
274
+ coral: '#FF7F50',
275
+ cornflowerblue: '#6495ED',
276
+ cornsilk: '#FFF8DC',
277
+ crimson: '#DC143C',
278
+ cyan: '#00FFFF',
279
+ darkblue: '#00008B',
280
+ darkcyan: '#008B8B',
281
+ darkgoldenrod: '#B8860B',
282
+ darkgray: '#A9A9A9',
283
+ darkgreen: '#006400',
284
+ darkgrey: '#A9A9A9',
285
+ darkkhaki: '#BDB76B',
286
+ darkmagenta: '#8B008B',
287
+ darkolivegreen: '#556B2F',
288
+ darkorange: '#FF8C00',
289
+ darkorchid: '#9932CC',
290
+ darkred: '#8B0000',
291
+ darksalmon: '#E9967A',
292
+ darkseagreen: '#8FBC8F',
293
+ darkslateblue: '#483D8B',
294
+ darkslategray: '#2F4F4F',
295
+ darkslategrey: '#2F4F4F',
296
+ darkturquoise: '#00CED1',
297
+ darkviolet: '#9400D3',
298
+ deeppink: '#FF1493',
299
+ deepskyblue: '#00BFFF',
300
+ dimgray: '#696969',
301
+ dimgrey: '#696969',
302
+ dodgerblue: '#1E90FF',
303
+ firebrick: '#B22222',
304
+ floralwhite: '#FFFAF0',
305
+ forestgreen: '#228B22',
306
+ gainsboro: '#DCDCDC',
307
+ ghostwhite: '#F8F8FF',
308
+ gold: '#FFD700',
309
+ goldenrod: '#DAA520',
310
+ grey: '#808080',
311
+ greenyellow: '#ADFF2F',
312
+ honeydew: '#F0FFF0',
313
+ hotpink: '#FF69B4',
314
+ indianred: '#CD5C5C',
315
+ indigo: '#4B0082',
316
+ ivory: '#FFFFF0',
317
+ khaki: '#F0E68C',
318
+ lavender: '#E6E6FA',
319
+ lavenderblush: '#FFF0F5',
320
+ lawngreen: '#7CFC00',
321
+ lemonchiffon: '#FFFACD',
322
+ lightblue: '#ADD8E6',
323
+ lightcoral: '#F08080',
324
+ lightcyan: '#E0FFFF',
325
+ lightgoldenrodyellow: '#FAFAD2',
326
+ lightgreen: '#90EE90',
327
+ lightgrey: '#D3D3D3',
328
+ lightpink: '#FFB6C1',
329
+ lightsalmon: '#FFA07A',
330
+ lightseagreen: '#20B2AA',
331
+ lightskyblue: '#87CEFA',
332
+ lightslategray: '#778899',
333
+ lightslategrey: '#778899',
334
+ lightsteelblue: '#B0C4DE',
335
+ lightyellow: '#FFFFE0',
336
+ limegreen: '#32CD32',
337
+ linen: '#FAF0E6',
338
+ magenta: '#FF00FF',
339
+ mediumaquamarine: '#66CDAA',
340
+ mediumblue: '#0000CD',
341
+ mediumorchid: '#BA55D3',
342
+ mediumpurple: '#9370DB',
343
+ mediumseagreen: '#3CB371',
344
+ mediumslateblue: '#7B68EE',
345
+ mediumspringgreen: '#00FA9A',
346
+ mediumturquoise: '#48D1CC',
347
+ mediumvioletred: '#C71585',
348
+ midnightblue: '#191970',
349
+ mintcream: '#F5FFFA',
350
+ mistyrose: '#FFE4E1',
351
+ moccasin: '#FFE4B5',
352
+ navajowhite: '#FFDEAD',
353
+ oldlace: '#FDF5E6',
354
+ olivedrab: '#6B8E23',
355
+ orange: '#FFA500',
356
+ orangered: '#FF4500',
357
+ orchid: '#DA70D6',
358
+ palegoldenrod: '#EEE8AA',
359
+ palegreen: '#98FB98',
360
+ paleturquoise: '#AFEEEE',
361
+ palevioletred: '#DB7093',
362
+ papayawhip: '#FFEFD5',
363
+ peachpuff: '#FFDAB9',
364
+ peru: '#CD853F',
365
+ pink: '#FFC0CB',
366
+ plum: '#DDA0DD',
367
+ powderblue: '#B0E0E6',
368
+ rosybrown: '#BC8F8F',
369
+ royalblue: '#4169E1',
370
+ saddlebrown: '#8B4513',
371
+ salmon: '#FA8072',
372
+ sandybrown: '#F4A460',
373
+ seagreen: '#2E8B57',
374
+ seashell: '#FFF5EE',
375
+ sienna: '#A0522D',
376
+ skyblue: '#87CEEB',
377
+ slateblue: '#6A5ACD',
378
+ slategray: '#708090',
379
+ slategrey: '#708090',
380
+ snow: '#FFFAFA',
381
+ springgreen: '#00FF7F',
382
+ steelblue: '#4682B4',
383
+ tan: '#D2B48C',
384
+ thistle: '#D8BFD8',
385
+ tomato: '#FF6347',
386
+ turquoise: '#40E0D0',
387
+ violet: '#EE82EE',
388
+ wheat: '#F5DEB3',
389
+ whitesmoke: '#F5F5F5',
390
+ yellowgreen: '#9ACD32'
391
+ };
392
+
393
+
394
+ function getRgbHslContent(styleString) {
395
+ var start = styleString.indexOf('(', 3);
396
+ var end = styleString.indexOf(')', start + 1);
397
+ var parts = styleString.substring(start + 1, end).split(',');
398
+ // add alpha if needed
399
+ if (parts.length != 4 || styleString.charAt(3) != 'a') {
400
+ parts[3] = 1;
401
+ }
402
+ return parts;
403
+ }
404
+
405
+ function percent(s) {
406
+ return parseFloat(s) / 100;
407
+ }
408
+
409
+ function clamp(v, min, max) {
410
+ return Math.min(max, Math.max(min, v));
411
+ }
412
+
413
+ function hslToRgb(parts){
414
+ var r, g, b, h, s, l;
415
+ h = parseFloat(parts[0]) / 360 % 360;
416
+ if (h < 0)
417
+ h++;
418
+ s = clamp(percent(parts[1]), 0, 1);
419
+ l = clamp(percent(parts[2]), 0, 1);
420
+ if (s == 0) {
421
+ r = g = b = l; // achromatic
422
+ } else {
423
+ var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
424
+ var p = 2 * l - q;
425
+ r = hueToRgb(p, q, h + 1 / 3);
426
+ g = hueToRgb(p, q, h);
427
+ b = hueToRgb(p, q, h - 1 / 3);
428
+ }
429
+
430
+ return '#' + decToHex[~~(r * 255)] +
431
+ decToHex[~~(g * 255)] +
432
+ decToHex[~~(b * 255)];
433
+ }
434
+
435
+ function hueToRgb(m1, m2, h) {
436
+ if (h < 0)
437
+ h++;
438
+ if (h > 1)
439
+ h--;
440
+
441
+ if (6 * h < 1)
442
+ return m1 + (m2 - m1) * 6 * h;
443
+ else if (2 * h < 1)
444
+ return m2;
445
+ else if (3 * h < 2)
446
+ return m1 + (m2 - m1) * (2 / 3 - h) * 6;
447
+ else
448
+ return m1;
449
+ }
450
+
451
+ var processStyleCache = {};
452
+
453
+ function processStyle(styleString) {
454
+ if (styleString in processStyleCache) {
455
+ return processStyleCache[styleString];
456
+ }
457
+
458
+ var str, alpha = 1;
459
+
460
+ styleString = String(styleString);
461
+ if (styleString.charAt(0) == '#') {
462
+ str = styleString;
463
+ } else if (/^rgb/.test(styleString)) {
464
+ var parts = getRgbHslContent(styleString);
465
+ var str = '#', n;
466
+ for (var i = 0; i < 3; i++) {
467
+ if (parts[i].indexOf('%') != -1) {
468
+ n = ~~(percent(parts[i]) * 255);
469
+ } else {
470
+ n = +parts[i];
471
+ }
472
+ str += decToHex[clamp(n, 0, 255)];
473
+ }
474
+ alpha = +parts[3];
475
+ } else if (/^hsl/.test(styleString)) {
476
+ var parts = getRgbHslContent(styleString);
477
+ str = hslToRgb(parts);
478
+ alpha = parts[3];
479
+ } else {
480
+ str = colorData[styleString] || styleString;
481
+ }
482
+ return processStyleCache[styleString] = {color: str, alpha: alpha};
483
+ }
484
+
485
+ var DEFAULT_STYLE = {
486
+ style: 'normal',
487
+ variant: 'normal',
488
+ weight: 'normal',
489
+ size: 10,
490
+ family: 'sans-serif'
491
+ };
492
+
493
+ // Internal text style cache
494
+ var fontStyleCache = {};
495
+
496
+ function processFontStyle(styleString) {
497
+ if (fontStyleCache[styleString]) {
498
+ return fontStyleCache[styleString];
499
+ }
500
+
501
+ var el = document.createElement('div');
502
+ var style = el.style;
503
+ try {
504
+ style.font = styleString;
505
+ } catch (ex) {
506
+ // Ignore failures to set to invalid font.
507
+ }
508
+
509
+ return fontStyleCache[styleString] = {
510
+ style: style.fontStyle || DEFAULT_STYLE.style,
511
+ variant: style.fontVariant || DEFAULT_STYLE.variant,
512
+ weight: style.fontWeight || DEFAULT_STYLE.weight,
513
+ size: style.fontSize || DEFAULT_STYLE.size,
514
+ family: style.fontFamily || DEFAULT_STYLE.family
515
+ };
516
+ }
517
+
518
+ function getComputedStyle(style, element) {
519
+ var computedStyle = {};
520
+
521
+ for (var p in style) {
522
+ computedStyle[p] = style[p];
523
+ }
524
+
525
+ // Compute the size
526
+ var canvasFontSize = parseFloat(element.currentStyle.fontSize),
527
+ fontSize = parseFloat(style.size);
528
+
529
+ if (typeof style.size == 'number') {
530
+ computedStyle.size = style.size;
531
+ } else if (style.size.indexOf('px') != -1) {
532
+ computedStyle.size = fontSize;
533
+ } else if (style.size.indexOf('em') != -1) {
534
+ computedStyle.size = canvasFontSize * fontSize;
535
+ } else if(style.size.indexOf('%') != -1) {
536
+ computedStyle.size = (canvasFontSize / 100) * fontSize;
537
+ } else if (style.size.indexOf('pt') != -1) {
538
+ computedStyle.size = fontSize / .75;
539
+ } else {
540
+ computedStyle.size = canvasFontSize;
541
+ }
542
+
543
+ // Different scaling between normal text and VML text. This was found using
544
+ // trial and error to get the same size as non VML text.
545
+ computedStyle.size *= 0.981;
546
+
547
+ return computedStyle;
548
+ }
549
+
550
+ function buildStyle(style) {
551
+ return style.style + ' ' + style.variant + ' ' + style.weight + ' ' +
552
+ style.size + 'px ' + style.family;
553
+ }
554
+
555
+ var lineCapMap = {
556
+ 'butt': 'flat',
557
+ 'round': 'round'
558
+ };
559
+
560
+ function processLineCap(lineCap) {
561
+ return lineCapMap[lineCap] || 'square';
562
+ }
563
+
564
+ /**
565
+ * This class implements CanvasRenderingContext2D interface as described by
566
+ * the WHATWG.
567
+ * @param {HTMLElement} canvasElement The element that the 2D context should
568
+ * be associated with
569
+ */
570
+ function CanvasRenderingContext2D_(canvasElement) {
571
+ this.m_ = createMatrixIdentity();
572
+
573
+ this.mStack_ = [];
574
+ this.aStack_ = [];
575
+ this.currentPath_ = [];
576
+
577
+ // Canvas context properties
578
+ this.strokeStyle = '#000';
579
+ this.fillStyle = '#000';
580
+
581
+ this.lineWidth = 1;
582
+ this.lineJoin = 'miter';
583
+ this.lineCap = 'butt';
584
+ this.miterLimit = Z * 1;
585
+ this.globalAlpha = 1;
586
+ this.font = '10px sans-serif';
587
+ this.textAlign = 'left';
588
+ this.textBaseline = 'alphabetic';
589
+ this.canvas = canvasElement;
590
+
591
+ var cssText = 'width:' + canvasElement.clientWidth + 'px;height:' +
592
+ canvasElement.clientHeight + 'px;overflow:hidden;position:absolute';
593
+ var el = canvasElement.ownerDocument.createElement('div');
594
+ el.style.cssText = cssText;
595
+ canvasElement.appendChild(el);
596
+
597
+ var overlayEl = el.cloneNode(false);
598
+ // Use a non transparent background.
599
+ overlayEl.style.backgroundColor = 'red';
600
+ overlayEl.style.filter = 'alpha(opacity=0)';
601
+ canvasElement.appendChild(overlayEl);
602
+
603
+ this.element_ = el;
604
+ this.arcScaleX_ = 1;
605
+ this.arcScaleY_ = 1;
606
+ this.lineScale_ = 1;
607
+ this.rotation_ = 0;
608
+ }
609
+
610
+ var contextPrototype = CanvasRenderingContext2D_.prototype;
611
+ contextPrototype.clearRect = function() {
612
+ if (this.textMeasureEl_) {
613
+ this.textMeasureEl_.removeNode(true);
614
+ this.textMeasureEl_ = null;
615
+ }
616
+ this.element_.innerHTML = '';
617
+ };
618
+
619
+ contextPrototype.beginPath = function() {
620
+ // TODO: Branch current matrix so that save/restore has no effect
621
+ // as per safari docs.
622
+ this.currentPath_ = [];
623
+ };
624
+
625
+ contextPrototype.moveTo = function(aX, aY) {
626
+ var p = getCoords(this, aX, aY);
627
+ this.currentPath_.push({type: 'moveTo', x: p.x, y: p.y});
628
+ this.currentX_ = p.x;
629
+ this.currentY_ = p.y;
630
+ };
631
+
632
+ contextPrototype.lineTo = function(aX, aY) {
633
+ var p = getCoords(this, aX, aY);
634
+ this.currentPath_.push({type: 'lineTo', x: p.x, y: p.y});
635
+
636
+ this.currentX_ = p.x;
637
+ this.currentY_ = p.y;
638
+ };
639
+
640
+ contextPrototype.bezierCurveTo = function(aCP1x, aCP1y,
641
+ aCP2x, aCP2y,
642
+ aX, aY) {
643
+ var p = getCoords(this, aX, aY);
644
+ var cp1 = getCoords(this, aCP1x, aCP1y);
645
+ var cp2 = getCoords(this, aCP2x, aCP2y);
646
+ bezierCurveTo(this, cp1, cp2, p);
647
+ };
648
+
649
+ // Helper function that takes the already fixed cordinates.
650
+ function bezierCurveTo(self, cp1, cp2, p) {
651
+ self.currentPath_.push({
652
+ type: 'bezierCurveTo',
653
+ cp1x: cp1.x,
654
+ cp1y: cp1.y,
655
+ cp2x: cp2.x,
656
+ cp2y: cp2.y,
657
+ x: p.x,
658
+ y: p.y
659
+ });
660
+ self.currentX_ = p.x;
661
+ self.currentY_ = p.y;
662
+ }
663
+
664
+ contextPrototype.quadraticCurveTo = function(aCPx, aCPy, aX, aY) {
665
+ // the following is lifted almost directly from
666
+ // http://developer.mozilla.org/en/docs/Canvas_tutorial:Drawing_shapes
667
+
668
+ var cp = getCoords(this, aCPx, aCPy);
669
+ var p = getCoords(this, aX, aY);
670
+
671
+ var cp1 = {
672
+ x: this.currentX_ + 2.0 / 3.0 * (cp.x - this.currentX_),
673
+ y: this.currentY_ + 2.0 / 3.0 * (cp.y - this.currentY_)
674
+ };
675
+ var cp2 = {
676
+ x: cp1.x + (p.x - this.currentX_) / 3.0,
677
+ y: cp1.y + (p.y - this.currentY_) / 3.0
678
+ };
679
+
680
+ bezierCurveTo(this, cp1, cp2, p);
681
+ };
682
+
683
+ contextPrototype.arc = function(aX, aY, aRadius,
684
+ aStartAngle, aEndAngle, aClockwise) {
685
+ aRadius *= Z;
686
+ var arcType = aClockwise ? 'at' : 'wa';
687
+
688
+ var xStart = aX + mc(aStartAngle) * aRadius - Z2;
689
+ var yStart = aY + ms(aStartAngle) * aRadius - Z2;
690
+
691
+ var xEnd = aX + mc(aEndAngle) * aRadius - Z2;
692
+ var yEnd = aY + ms(aEndAngle) * aRadius - Z2;
693
+
694
+ // IE won't render arches drawn counter clockwise if xStart == xEnd.
695
+ if (xStart == xEnd && !aClockwise) {
696
+ xStart += 0.125; // Offset xStart by 1/80 of a pixel. Use something
697
+ // that can be represented in binary
698
+ }
699
+
700
+ var p = getCoords(this, aX, aY);
701
+ var pStart = getCoords(this, xStart, yStart);
702
+ var pEnd = getCoords(this, xEnd, yEnd);
703
+
704
+ this.currentPath_.push({type: arcType,
705
+ x: p.x,
706
+ y: p.y,
707
+ radius: aRadius,
708
+ xStart: pStart.x,
709
+ yStart: pStart.y,
710
+ xEnd: pEnd.x,
711
+ yEnd: pEnd.y});
712
+
713
+ };
714
+
715
+ contextPrototype.rect = function(aX, aY, aWidth, aHeight) {
716
+ this.moveTo(aX, aY);
717
+ this.lineTo(aX + aWidth, aY);
718
+ this.lineTo(aX + aWidth, aY + aHeight);
719
+ this.lineTo(aX, aY + aHeight);
720
+ this.closePath();
721
+ };
722
+
723
+ contextPrototype.strokeRect = function(aX, aY, aWidth, aHeight) {
724
+ var oldPath = this.currentPath_;
725
+ this.beginPath();
726
+
727
+ this.moveTo(aX, aY);
728
+ this.lineTo(aX + aWidth, aY);
729
+ this.lineTo(aX + aWidth, aY + aHeight);
730
+ this.lineTo(aX, aY + aHeight);
731
+ this.closePath();
732
+ this.stroke();
733
+
734
+ this.currentPath_ = oldPath;
735
+ };
736
+
737
+ contextPrototype.fillRect = function(aX, aY, aWidth, aHeight) {
738
+ var oldPath = this.currentPath_;
739
+ this.beginPath();
740
+
741
+ this.moveTo(aX, aY);
742
+ this.lineTo(aX + aWidth, aY);
743
+ this.lineTo(aX + aWidth, aY + aHeight);
744
+ this.lineTo(aX, aY + aHeight);
745
+ this.closePath();
746
+ this.fill();
747
+
748
+ this.currentPath_ = oldPath;
749
+ };
750
+
751
+ contextPrototype.createLinearGradient = function(aX0, aY0, aX1, aY1) {
752
+ var gradient = new CanvasGradient_('gradient');
753
+ gradient.x0_ = aX0;
754
+ gradient.y0_ = aY0;
755
+ gradient.x1_ = aX1;
756
+ gradient.y1_ = aY1;
757
+ return gradient;
758
+ };
759
+
760
+ contextPrototype.createRadialGradient = function(aX0, aY0, aR0,
761
+ aX1, aY1, aR1) {
762
+ var gradient = new CanvasGradient_('gradientradial');
763
+ gradient.x0_ = aX0;
764
+ gradient.y0_ = aY0;
765
+ gradient.r0_ = aR0;
766
+ gradient.x1_ = aX1;
767
+ gradient.y1_ = aY1;
768
+ gradient.r1_ = aR1;
769
+ return gradient;
770
+ };
771
+
772
+ contextPrototype.drawImage = function(image) {
773
+ var dx, dy, dw, dh, sx, sy, sw, sh;
774
+
775
+ // to fix new Image() we check the existance of runtimeStyle
776
+ var rts = image.runtimeStyle.width;
777
+
778
+ // to find the original width we overide the width and height
779
+ if(rts) {
780
+ var oldRuntimeWidth = image.runtimeStyle.width;
781
+ var oldRuntimeHeight = image.runtimeStyle.height;
782
+
783
+ image.runtimeStyle.width = 'auto';
784
+ image.runtimeStyle.height = 'auto';
785
+ }
786
+
787
+ // get the original size
788
+ var w = image.width;
789
+ var h = image.height;
790
+
791
+ // and remove overides
792
+ if(rts) {
793
+ image.runtimeStyle.width = oldRuntimeWidth;
794
+ image.runtimeStyle.height = oldRuntimeHeight;
795
+ }
796
+
797
+ if (arguments.length == 3) {
798
+ dx = arguments[1];
799
+ dy = arguments[2];
800
+ sx = sy = 0;
801
+ sw = dw = w;
802
+ sh = dh = h;
803
+ } else if (arguments.length == 5) {
804
+ dx = arguments[1];
805
+ dy = arguments[2];
806
+ dw = arguments[3];
807
+ dh = arguments[4];
808
+ sx = sy = 0;
809
+ sw = w;
810
+ sh = h;
811
+ } else if (arguments.length == 9) {
812
+ sx = arguments[1];
813
+ sy = arguments[2];
814
+ sw = arguments[3];
815
+ sh = arguments[4];
816
+ dx = arguments[5];
817
+ dy = arguments[6];
818
+ dw = arguments[7];
819
+ dh = arguments[8];
820
+ } else {
821
+ throw Error('Invalid number of arguments');
822
+ }
823
+
824
+ var d = getCoords(this, dx, dy);
825
+
826
+ var w2 = sw / 2;
827
+ var h2 = sh / 2;
828
+
829
+ var vmlStr = [];
830
+
831
+ var W = 10;
832
+ var H = 10;
833
+
834
+ var scaleX = scaleY = 1;
835
+
836
+ // FIX: divs give better quality then vml image and also fixes transparent PNG's
837
+ vmlStr.push(' <div style="position:absolute;');
838
+
839
+ // If filters are necessary (rotation exists), create them
840
+ // filters are bog-slow, so only create them if abbsolutely necessary
841
+ // The following check doesn't account for skews (which don't exist
842
+ // in the canvas spec (yet) anyway.
843
+ if (this.m_[0][0] != 1 || this.m_[0][1] ||
844
+ this.m_[1][1] != 1 || this.m_[1][0]) {
845
+ var filter = [];
846
+
847
+ // Scaling images using width & height instead of Transform Matrix
848
+ // because of quality loss
849
+ var c = mc(this.rotation_);
850
+ var s = ms(this.rotation_);
851
+
852
+ // Inverse rotation matrix
853
+ var irm = [
854
+ [c, -s, 0],
855
+ [s, c, 0],
856
+ [0, 0, 1]
857
+ ];
858
+
859
+ // Get unrotated matrix to get only scaling values
860
+ var urm = matrixMultiply(irm, this.m_);
861
+ scaleX = urm[0][0];
862
+ scaleY = urm[1][1];
863
+
864
+ // Apply only rotation and translation to Matrix
865
+ filter.push('M11=', c, ',',
866
+ 'M12=', -s, ',',
867
+ 'M21=', s, ',',
868
+ 'M22=', c, ',',
869
+ 'Dx=', d.x / Z, ',',
870
+ 'Dy=', d.y / Z);
871
+
872
+ // Bounding box calculation (need to minimize displayed area so that
873
+ // filters don't waste time on unused pixels.
874
+ var max = d;
875
+ var c2 = getCoords(this, dx + dw, dy);
876
+ var c3 = getCoords(this, dx, dy + dh);
877
+ var c4 = getCoords(this, dx + dw, dy + dh);
878
+
879
+ max.x = m.max(max.x, c2.x, c3.x, c4.x);
880
+ max.y = m.max(max.y, c2.y, c3.y, c4.y);
881
+
882
+ vmlStr.push('padding:0 ', mr(max.x / Z), 'px ', mr(max.y / Z),
883
+ 'px 0;filter:progid:DXImageTransform.Microsoft.Matrix(',
884
+ filter.join(''), ", sizingmethod='clip');");
885
+ } else {
886
+ vmlStr.push('top:', mr(d.y / Z), 'px;left:', mr(d.x / Z), 'px;');
887
+ }
888
+
889
+ vmlStr.push(' ">');
890
+
891
+ // Draw a special cropping div if needed
892
+ if (sx || sy) {
893
+ // Apply scales to width and height
894
+ vmlStr.push('<div style="overflow: hidden; width:', Math.ceil((dw + sx * dw / sw) * scaleX), 'px;',
895
+ ' height:', Math.ceil((dh + sy * dh / sh) * scaleY), 'px;',
896
+ ' filter:progid:DxImageTransform.Microsoft.Matrix(Dx=',
897
+ -sx * dw / sw * scaleX, ',Dy=', -sy * dh / sh * scaleY, ');">');
898
+ }
899
+
900
+
901
+ // Apply scales to width and height
902
+ vmlStr.push('<div style="width:', Math.round(scaleX * w * dw / sw), 'px;',
903
+ ' height:', Math.round(scaleY * h * dh / sh), 'px;',
904
+ ' filter:');
905
+
906
+ // If there is a globalAlpha, apply it to image
907
+ if(this.globalAlpha < 1) {
908
+ vmlStr.push(' progid:DXImageTransform.Microsoft.Alpha(opacity=' + (this.globalAlpha * 100) + ')');
909
+ }
910
+
911
+ vmlStr.push(' progid:DXImageTransform.Microsoft.AlphaImageLoader(src=', image.src, ',sizingMethod=scale)">');
912
+
913
+ // Close the crop div if necessary
914
+ if (sx || sy) vmlStr.push('</div>');
915
+
916
+ vmlStr.push('</div></div>');
917
+
918
+ this.element_.insertAdjacentHTML('beforeEnd', vmlStr.join(''));
919
+ };
920
+
921
+ contextPrototype.stroke = function(aFill) {
922
+ var lineStr = [];
923
+ var lineOpen = false;
924
+
925
+ var W = 10;
926
+ var H = 10;
927
+
928
+ lineStr.push('<g_vml_:shape',
929
+ ' filled="', !!aFill, '"',
930
+ ' style="position:absolute;width:', W, 'px;height:', H, 'px;"',
931
+ ' coordorigin="0,0"',
932
+ ' coordsize="', Z * W, ',', Z * H, '"',
933
+ ' stroked="', !aFill, '"',
934
+ ' path="');
935
+
936
+ var newSeq = false;
937
+ var min = {x: null, y: null};
938
+ var max = {x: null, y: null};
939
+
940
+ for (var i = 0; i < this.currentPath_.length; i++) {
941
+ var p = this.currentPath_[i];
942
+ var c;
943
+
944
+ switch (p.type) {
945
+ case 'moveTo':
946
+ c = p;
947
+ lineStr.push(' m ', mr(p.x), ',', mr(p.y));
948
+ break;
949
+ case 'lineTo':
950
+ lineStr.push(' l ', mr(p.x), ',', mr(p.y));
951
+ break;
952
+ case 'close':
953
+ lineStr.push(' x ');
954
+ p = null;
955
+ break;
956
+ case 'bezierCurveTo':
957
+ lineStr.push(' c ',
958
+ mr(p.cp1x), ',', mr(p.cp1y), ',',
959
+ mr(p.cp2x), ',', mr(p.cp2y), ',',
960
+ mr(p.x), ',', mr(p.y));
961
+ break;
962
+ case 'at':
963
+ case 'wa':
964
+ lineStr.push(' ', p.type, ' ',
965
+ mr(p.x - this.arcScaleX_ * p.radius), ',',
966
+ mr(p.y - this.arcScaleY_ * p.radius), ' ',
967
+ mr(p.x + this.arcScaleX_ * p.radius), ',',
968
+ mr(p.y + this.arcScaleY_ * p.radius), ' ',
969
+ mr(p.xStart), ',', mr(p.yStart), ' ',
970
+ mr(p.xEnd), ',', mr(p.yEnd));
971
+ break;
972
+ }
973
+
974
+
975
+ // TODO: Following is broken for curves due to
976
+ // move to proper paths.
977
+
978
+ // Figure out dimensions so we can do gradient fills
979
+ // properly
980
+ if (p) {
981
+ if (min.x == null || p.x < min.x) {
982
+ min.x = p.x;
983
+ }
984
+ if (max.x == null || p.x > max.x) {
985
+ max.x = p.x;
986
+ }
987
+ if (min.y == null || p.y < min.y) {
988
+ min.y = p.y;
989
+ }
990
+ if (max.y == null || p.y > max.y) {
991
+ max.y = p.y;
992
+ }
993
+ }
994
+ }
995
+ lineStr.push(' ">');
996
+
997
+ if (!aFill) {
998
+ appendStroke(this, lineStr);
999
+ } else {
1000
+ appendFill(this, lineStr, min, max);
1001
+ }
1002
+
1003
+ lineStr.push('</g_vml_:shape>');
1004
+
1005
+ this.element_.insertAdjacentHTML('beforeEnd', lineStr.join(''));
1006
+ };
1007
+
1008
+ function appendStroke(ctx, lineStr) {
1009
+ var a = processStyle(ctx.strokeStyle);
1010
+ var color = a.color;
1011
+ var opacity = a.alpha * ctx.globalAlpha;
1012
+ var lineWidth = ctx.lineScale_ * ctx.lineWidth;
1013
+
1014
+ // VML cannot correctly render a line if the width is less than 1px.
1015
+ // In that case, we dilute the color to make the line look thinner.
1016
+ if (lineWidth < 1) {
1017
+ opacity *= lineWidth;
1018
+ }
1019
+
1020
+ lineStr.push(
1021
+ '<g_vml_:stroke',
1022
+ ' opacity="', opacity, '"',
1023
+ ' joinstyle="', ctx.lineJoin, '"',
1024
+ ' miterlimit="', ctx.miterLimit, '"',
1025
+ ' endcap="', processLineCap(ctx.lineCap), '"',
1026
+ ' weight="', lineWidth, 'px"',
1027
+ ' color="', color, '" />'
1028
+ );
1029
+ }
1030
+
1031
+ function appendFill(ctx, lineStr, min, max) {
1032
+ var fillStyle = ctx.fillStyle;
1033
+ var arcScaleX = ctx.arcScaleX_;
1034
+ var arcScaleY = ctx.arcScaleY_;
1035
+ var width = max.x - min.x;
1036
+ var height = max.y - min.y;
1037
+ if (fillStyle instanceof CanvasGradient_) {
1038
+ // TODO: Gradients transformed with the transformation matrix.
1039
+ var angle = 0;
1040
+ var focus = {x: 0, y: 0};
1041
+
1042
+ // additional offset
1043
+ var shift = 0;
1044
+ // scale factor for offset
1045
+ var expansion = 1;
1046
+
1047
+ if (fillStyle.type_ == 'gradient') {
1048
+ var x0 = fillStyle.x0_ / arcScaleX;
1049
+ var y0 = fillStyle.y0_ / arcScaleY;
1050
+ var x1 = fillStyle.x1_ / arcScaleX;
1051
+ var y1 = fillStyle.y1_ / arcScaleY;
1052
+ var p0 = getCoords(ctx, x0, y0);
1053
+ var p1 = getCoords(ctx, x1, y1);
1054
+ var dx = p1.x - p0.x;
1055
+ var dy = p1.y - p0.y;
1056
+ angle = Math.atan2(dx, dy) * 180 / Math.PI;
1057
+
1058
+ // The angle should be a non-negative number.
1059
+ if (angle < 0) {
1060
+ angle += 360;
1061
+ }
1062
+
1063
+ // Very small angles produce an unexpected result because they are
1064
+ // converted to a scientific notation string.
1065
+ if (angle < 1e-6) {
1066
+ angle = 0;
1067
+ }
1068
+ } else {
1069
+ var p0 = getCoords(ctx, fillStyle.x0_, fillStyle.y0_);
1070
+ focus = {
1071
+ x: (p0.x - min.x) / width,
1072
+ y: (p0.y - min.y) / height
1073
+ };
1074
+
1075
+ width /= arcScaleX * Z;
1076
+ height /= arcScaleY * Z;
1077
+ var dimension = m.max(width, height);
1078
+ shift = 2 * fillStyle.r0_ / dimension;
1079
+ expansion = 2 * fillStyle.r1_ / dimension - shift;
1080
+ }
1081
+
1082
+ // We need to sort the color stops in ascending order by offset,
1083
+ // otherwise IE won't interpret it correctly.
1084
+ var stops = fillStyle.colors_;
1085
+ stops.sort(function(cs1, cs2) {
1086
+ return cs1.offset - cs2.offset;
1087
+ });
1088
+
1089
+ var length = stops.length;
1090
+ var color1 = stops[0].color;
1091
+ var color2 = stops[length - 1].color;
1092
+ var opacity1 = stops[0].alpha * ctx.globalAlpha;
1093
+ var opacity2 = stops[length - 1].alpha * ctx.globalAlpha;
1094
+
1095
+ var colors = [];
1096
+ for (var i = 0; i < length; i++) {
1097
+ var stop = stops[i];
1098
+ colors.push(stop.offset * expansion + shift + ' ' + stop.color);
1099
+ }
1100
+
1101
+ // When colors attribute is used, the meanings of opacity and o:opacity2
1102
+ // are reversed.
1103
+ lineStr.push('<g_vml_:fill type="', fillStyle.type_, '"',
1104
+ ' method="none" focus="100%"',
1105
+ ' color="', color1, '"',
1106
+ ' color2="', color2, '"',
1107
+ ' colors="', colors.join(','), '"',
1108
+ ' opacity="', opacity2, '"',
1109
+ ' g_o_:opacity2="', opacity1, '"',
1110
+ ' angle="', angle, '"',
1111
+ ' focusposition="', focus.x, ',', focus.y, '" />');
1112
+ } else if (fillStyle instanceof CanvasPattern_) {
1113
+ if (width && height) {
1114
+ var deltaLeft = -min.x;
1115
+ var deltaTop = -min.y;
1116
+ lineStr.push('<g_vml_:fill',
1117
+ ' position="',
1118
+ deltaLeft / width * arcScaleX * arcScaleX, ',',
1119
+ deltaTop / height * arcScaleY * arcScaleY, '"',
1120
+ ' type="tile"',
1121
+ // TODO: Figure out the correct size to fit the scale.
1122
+ //' size="', w, 'px ', h, 'px"',
1123
+ ' src="', fillStyle.src_, '" />');
1124
+ }
1125
+ } else {
1126
+ var a = processStyle(ctx.fillStyle);
1127
+ var color = a.color;
1128
+ var opacity = a.alpha * ctx.globalAlpha;
1129
+ lineStr.push('<g_vml_:fill color="', color, '" opacity="', opacity,
1130
+ '" />');
1131
+ }
1132
+ }
1133
+
1134
+ contextPrototype.fill = function() {
1135
+ this.stroke(true);
1136
+ };
1137
+
1138
+ contextPrototype.closePath = function() {
1139
+ this.currentPath_.push({type: 'close'});
1140
+ };
1141
+
1142
+ function getCoords(ctx, aX, aY) {
1143
+ var m = ctx.m_;
1144
+ return {
1145
+ x: Z * (aX * m[0][0] + aY * m[1][0] + m[2][0]) - Z2,
1146
+ y: Z * (aX * m[0][1] + aY * m[1][1] + m[2][1]) - Z2
1147
+ };
1148
+ };
1149
+
1150
+ contextPrototype.save = function() {
1151
+ var o = {};
1152
+ copyState(this, o);
1153
+ this.aStack_.push(o);
1154
+ this.mStack_.push(this.m_);
1155
+ this.m_ = matrixMultiply(createMatrixIdentity(), this.m_);
1156
+ };
1157
+
1158
+ contextPrototype.restore = function() {
1159
+ if (this.aStack_.length) {
1160
+ copyState(this.aStack_.pop(), this);
1161
+ this.m_ = this.mStack_.pop();
1162
+ }
1163
+ };
1164
+
1165
+ function matrixIsFinite(m) {
1166
+ return isFinite(m[0][0]) && isFinite(m[0][1]) &&
1167
+ isFinite(m[1][0]) && isFinite(m[1][1]) &&
1168
+ isFinite(m[2][0]) && isFinite(m[2][1]);
1169
+ }
1170
+
1171
+ function setM(ctx, m, updateLineScale) {
1172
+ if (!matrixIsFinite(m)) {
1173
+ return;
1174
+ }
1175
+ ctx.m_ = m;
1176
+
1177
+ if (updateLineScale) {
1178
+ // Get the line scale.
1179
+ // Determinant of this.m_ means how much the area is enlarged by the
1180
+ // transformation. So its square root can be used as a scale factor
1181
+ // for width.
1182
+ var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
1183
+ ctx.lineScale_ = sqrt(abs(det));
1184
+ }
1185
+ }
1186
+
1187
+ contextPrototype.translate = function(aX, aY) {
1188
+ var m1 = [
1189
+ [1, 0, 0],
1190
+ [0, 1, 0],
1191
+ [aX, aY, 1]
1192
+ ];
1193
+
1194
+ setM(this, matrixMultiply(m1, this.m_), false);
1195
+ };
1196
+
1197
+ contextPrototype.rotate = function(aRot) {
1198
+ var c = mc(aRot);
1199
+ var s = ms(aRot);
1200
+
1201
+ this.rotation_ += aRot;
1202
+
1203
+ var m1 = [
1204
+ [c, s, 0],
1205
+ [-s, c, 0],
1206
+ [0, 0, 1]
1207
+ ];
1208
+
1209
+ setM(this, matrixMultiply(m1, this.m_), false);
1210
+ };
1211
+
1212
+ contextPrototype.scale = function(aX, aY) {
1213
+ this.arcScaleX_ *= aX;
1214
+ this.arcScaleY_ *= aY;
1215
+ var m1 = [
1216
+ [aX, 0, 0],
1217
+ [0, aY, 0],
1218
+ [0, 0, 1]
1219
+ ];
1220
+
1221
+ setM(this, matrixMultiply(m1, this.m_), true);
1222
+ };
1223
+
1224
+ contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {
1225
+ var m1 = [
1226
+ [m11, m12, 0],
1227
+ [m21, m22, 0],
1228
+ [dx, dy, 1]
1229
+ ];
1230
+
1231
+ setM(this, matrixMultiply(m1, this.m_), true);
1232
+ };
1233
+
1234
+ contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {
1235
+ var m = [
1236
+ [m11, m12, 0],
1237
+ [m21, m22, 0],
1238
+ [dx, dy, 1]
1239
+ ];
1240
+
1241
+ setM(this, m, true);
1242
+ };
1243
+
1244
+ /**
1245
+ * The text drawing function.
1246
+ * The maxWidth argument isn't taken in account, since no browser supports
1247
+ * it yet.
1248
+ */
1249
+ contextPrototype.drawText_ = function(text, x, y, maxWidth, stroke) {
1250
+ var m = this.m_,
1251
+ delta = 1000,
1252
+ left = 0,
1253
+ right = delta,
1254
+ offset = {x: 0, y: 0},
1255
+ lineStr = [];
1256
+
1257
+ var fontStyle = getComputedStyle(processFontStyle(this.font),
1258
+ this.element_);
1259
+
1260
+ var fontStyleString = buildStyle(fontStyle);
1261
+
1262
+ var elementStyle = this.element_.currentStyle;
1263
+ var textAlign = this.textAlign.toLowerCase();
1264
+ switch (textAlign) {
1265
+ case 'left':
1266
+ case 'center':
1267
+ case 'right':
1268
+ break;
1269
+ case 'end':
1270
+ textAlign = elementStyle.direction == 'ltr' ? 'right' : 'left';
1271
+ break;
1272
+ case 'start':
1273
+ textAlign = elementStyle.direction == 'rtl' ? 'right' : 'left';
1274
+ break;
1275
+ default:
1276
+ textAlign = 'left';
1277
+ }
1278
+
1279
+ // 1.75 is an arbitrary number, as there is no info about the text baseline
1280
+ switch (this.textBaseline) {
1281
+ case 'hanging':
1282
+ case 'top':
1283
+ offset.y = fontStyle.size / 1.75;
1284
+ break;
1285
+ case 'middle':
1286
+ break;
1287
+ default:
1288
+ case null:
1289
+ case 'alphabetic':
1290
+ case 'ideographic':
1291
+ case 'bottom':
1292
+ offset.y = -fontStyle.size / 2.25;
1293
+ break;
1294
+ }
1295
+
1296
+ switch(textAlign) {
1297
+ case 'right':
1298
+ left = delta;
1299
+ right = 0.05;
1300
+ break;
1301
+ case 'center':
1302
+ left = right = delta / 2;
1303
+ break;
1304
+ }
1305
+
1306
+ var d = getCoords(this, x + offset.x, y + offset.y);
1307
+
1308
+ lineStr.push('<g_vml_:line from="', -left ,' 0" to="', right ,' 0.05" ',
1309
+ ' coordsize="100 100" coordorigin="0 0"',
1310
+ ' filled="', !stroke, '" stroked="', !!stroke,
1311
+ '" style="position:absolute;width:1px;height:1px;">');
1312
+
1313
+ if (stroke) {
1314
+ appendStroke(this, lineStr);
1315
+ } else {
1316
+ // TODO: Fix the min and max params.
1317
+ appendFill(this, lineStr, {x: -left, y: 0},
1318
+ {x: right, y: fontStyle.size});
1319
+ }
1320
+
1321
+ var skewM = m[0][0].toFixed(3) + ',' + m[1][0].toFixed(3) + ',' +
1322
+ m[0][1].toFixed(3) + ',' + m[1][1].toFixed(3) + ',0,0';
1323
+
1324
+ var skewOffset = mr(d.x / Z) + ',' + mr(d.y / Z);
1325
+
1326
+ lineStr.push('<g_vml_:skew on="t" matrix="', skewM ,'" ',
1327
+ ' offset="', skewOffset, '" origin="', left ,' 0" />',
1328
+ '<g_vml_:path textpathok="true" />',
1329
+ '<g_vml_:textpath on="true" string="',
1330
+ encodeHtmlAttribute(text),
1331
+ '" style="v-text-align:', textAlign,
1332
+ ';font:', encodeHtmlAttribute(fontStyleString),
1333
+ '" /></g_vml_:line>');
1334
+
1335
+ this.element_.insertAdjacentHTML('beforeEnd', lineStr.join(''));
1336
+ };
1337
+
1338
+ contextPrototype.fillText = function(text, x, y, maxWidth) {
1339
+ this.drawText_(text, x, y, maxWidth, false);
1340
+ };
1341
+
1342
+ contextPrototype.strokeText = function(text, x, y, maxWidth) {
1343
+ this.drawText_(text, x, y, maxWidth, true);
1344
+ };
1345
+
1346
+ contextPrototype.measureText = function(text) {
1347
+ if (!this.textMeasureEl_) {
1348
+ var s = '<span style="position:absolute;' +
1349
+ 'top:-20000px;left:0;padding:0;margin:0;border:none;' +
1350
+ 'white-space:pre;"></span>';
1351
+ this.element_.insertAdjacentHTML('beforeEnd', s);
1352
+ this.textMeasureEl_ = this.element_.lastChild;
1353
+ }
1354
+ var doc = this.element_.ownerDocument;
1355
+ this.textMeasureEl_.innerHTML = '';
1356
+ // FIX: Apply current font style to textMeasureEl to get correct size
1357
+ var fontStyle = getComputedStyle(processFontStyle(this.font), this.element_),
1358
+ fontStyleString = buildStyle(fontStyle);
1359
+ this.textMeasureEl_.style.font = fontStyleString;
1360
+
1361
+ // Don't use innerHTML or innerText because they allow markup/whitespace.
1362
+ this.textMeasureEl_.appendChild(doc.createTextNode(text));
1363
+ return {width: this.textMeasureEl_.offsetWidth};
1364
+ };
1365
+
1366
+ /******** STUBS ********/
1367
+ contextPrototype.clip = function() {
1368
+ // TODO: Implement
1369
+ };
1370
+
1371
+ contextPrototype.arcTo = function() {
1372
+ // TODO: Implement
1373
+ };
1374
+
1375
+ contextPrototype.createPattern = function(image, repetition) {
1376
+ return new CanvasPattern_(image, repetition);
1377
+ };
1378
+
1379
+ // Gradient / Pattern Stubs
1380
+ function CanvasGradient_(aType) {
1381
+ this.type_ = aType;
1382
+ this.x0_ = 0;
1383
+ this.y0_ = 0;
1384
+ this.r0_ = 0;
1385
+ this.x1_ = 0;
1386
+ this.y1_ = 0;
1387
+ this.r1_ = 0;
1388
+ this.colors_ = [];
1389
+ }
1390
+
1391
+ CanvasGradient_.prototype.addColorStop = function(aOffset, aColor) {
1392
+ aColor = processStyle(aColor);
1393
+ this.colors_.push({offset: aOffset,
1394
+ color: aColor.color,
1395
+ alpha: aColor.alpha});
1396
+ };
1397
+
1398
+ function CanvasPattern_(image, repetition) {
1399
+ assertImageIsValid(image);
1400
+ switch (repetition) {
1401
+ case 'repeat':
1402
+ case null:
1403
+ case '':
1404
+ this.repetition_ = 'repeat';
1405
+ break
1406
+ case 'repeat-x':
1407
+ case 'repeat-y':
1408
+ case 'no-repeat':
1409
+ this.repetition_ = repetition;
1410
+ break;
1411
+ default:
1412
+ throwException('SYNTAX_ERR');
1413
+ }
1414
+
1415
+ this.src_ = image.src;
1416
+ this.width_ = image.width;
1417
+ this.height_ = image.height;
1418
+ }
1419
+
1420
+ function throwException(s) {
1421
+ throw new DOMException_(s);
1422
+ }
1423
+
1424
+ function assertImageIsValid(img) {
1425
+ if (!img || img.nodeType != 1 || img.tagName != 'IMG') {
1426
+ throwException('TYPE_MISMATCH_ERR');
1427
+ }
1428
+ if (img.readyState != 'complete') {
1429
+ throwException('INVALID_STATE_ERR');
1430
+ }
1431
+ }
1432
+
1433
+ function DOMException_(s) {
1434
+ this.code = this[s];
1435
+ this.message = s +': DOM Exception ' + this.code;
1436
+ }
1437
+ var p = DOMException_.prototype = new Error;
1438
+ p.INDEX_SIZE_ERR = 1;
1439
+ p.DOMSTRING_SIZE_ERR = 2;
1440
+ p.HIERARCHY_REQUEST_ERR = 3;
1441
+ p.WRONG_DOCUMENT_ERR = 4;
1442
+ p.INVALID_CHARACTER_ERR = 5;
1443
+ p.NO_DATA_ALLOWED_ERR = 6;
1444
+ p.NO_MODIFICATION_ALLOWED_ERR = 7;
1445
+ p.NOT_FOUND_ERR = 8;
1446
+ p.NOT_SUPPORTED_ERR = 9;
1447
+ p.INUSE_ATTRIBUTE_ERR = 10;
1448
+ p.INVALID_STATE_ERR = 11;
1449
+ p.SYNTAX_ERR = 12;
1450
+ p.INVALID_MODIFICATION_ERR = 13;
1451
+ p.NAMESPACE_ERR = 14;
1452
+ p.INVALID_ACCESS_ERR = 15;
1453
+ p.VALIDATION_ERR = 16;
1454
+ p.TYPE_MISMATCH_ERR = 17;
1455
+
1456
+ // set up externs
1457
+ G_vmlCanvasManager = G_vmlCanvasManager_;
1458
+ CanvasRenderingContext2D = CanvasRenderingContext2D_;
1459
+ CanvasGradient = CanvasGradient_;
1460
+ CanvasPattern = CanvasPattern_;
1461
+ DOMException = DOMException_;
1462
+ })();
1463
+
1464
+ } // if