@@ -59,6 +59,111 @@ function ScratchCanvas(width, height) {
59
59
return canvas ;
60
60
}
61
61
62
+ function addCtxCurrentTransform (ctx ) {
63
+ // If the context doesn't expose a `mozCurrentTransform`, add a JS based on.
64
+ if (!ctx .mozCurrentTransform ) {
65
+ // Store the original context
66
+ ctx ._originalSave = ctx .save ;
67
+ ctx ._originalRestore = ctx .restore ;
68
+ ctx ._originalRotate = ctx .rotate ;
69
+ ctx ._originalScale = ctx .scale ;
70
+ ctx ._originalTranslate = ctx .translate ;
71
+ ctx ._originalTransform = ctx .transform ;
72
+
73
+ ctx ._transformMatrix = [1 , 0 , 0 , 1 , 0 , 0 ];
74
+ ctx ._transformStack = [];
75
+
76
+ ctx .__defineGetter__ ('mozCurrentTransform' , function getCurrentTransform () {
77
+ return this ._transformMatrix ;
78
+ });
79
+
80
+ ctx .__defineGetter__ ('mozCurrentTransformInverse' ,
81
+ function getCurrentTransformInverse () {
82
+ // Calculation done using WolframAlpha:
83
+ // http://www.wolframalpha.com/input/?
84
+ // i=Inverse+{{a%2C+c%2C+e}%2C+{b%2C+d%2C+f}%2C+{0%2C+0%2C+1}}
85
+
86
+ var m = this ._transformMatrix ;
87
+ var a = m [0 ], b = m [1 ], c = m [2 ], d = m [3 ], e = m [4 ], f = m [5 ];
88
+
89
+ return [
90
+ d / (a * d - b * c ),
91
+ b / (b * c - a * d ),
92
+ c / (b * c - a * d ),
93
+ a / (a * d - b * c ),
94
+ (d * e - c * f ) / (b * c - a * d ),
95
+ (b * e - a * f ) / (a * d - b * c )
96
+ ];
97
+ }
98
+ );
99
+
100
+ ctx .save = function ctxSave () {
101
+ var old = this ._transformMatrix ;
102
+ this ._transformStack .push (old );
103
+ this ._transformMatrix = old .slice (0 , 6 );
104
+
105
+ this ._originalSave ();
106
+ };
107
+
108
+ ctx .restore = function ctxRestore () {
109
+ var prev = this ._transformStack .pop ();
110
+ if (prev ) {
111
+ this ._transformMatrix = prev ;
112
+ this ._originalRestore ();
113
+ }
114
+ }
115
+
116
+ ctx .translate = function ctxTranslate (x , y ) {
117
+ var m = this ._transformMatrix ;
118
+ m [4 ] = m [0 ] * x + m [2 ] * y + m [4 ];
119
+ m [5 ] = m [1 ] * x + m [3 ] * y + m [5 ];
120
+
121
+ this ._originalTranslate (x , y );
122
+ }
123
+
124
+ ctx .scale = function ctxScale (x , y ) {
125
+ var m = this ._transformMatrix ;
126
+ m [0 ] = m [0 ] * x ;
127
+ m [1 ] = m [1 ] * x ;
128
+ m [2 ] = m [2 ] * y ;
129
+ m [3 ] = m [3 ] * y ;
130
+
131
+ this ._originalScale (x , y );
132
+ }
133
+
134
+ ctx .transform = function ctxTransform (a , b , c , d , e , f ) {
135
+ var m = this ._transformMatrix ;
136
+ this ._transformMatrix = [
137
+ m [0 ] * a + m [2 ] * b ,
138
+ m [1 ] * a + m [3 ] * b ,
139
+ m [0 ] * c + m [2 ] * d ,
140
+ m [1 ] * c + m [3 ] * d ,
141
+ m [0 ] * e + m [2 ] * f + m [4 ],
142
+ m [1 ] * e + m [3 ] * f + m [5 ]
143
+ ];
144
+
145
+ ctx ._originalTransform (a , b , c , d , e , f );
146
+ }
147
+
148
+ ctx .rotate = function ctxRotate (angle ) {
149
+ var cosValue = Math .cos (angle );
150
+ var sinValue = Math .sin (angle );
151
+
152
+ var m = this ._transformMatrix ;
153
+ this ._transformMatrix = [
154
+ m [0 ] * cosValue + m [2 ] * sinValue ,
155
+ m [1 ] * cosValue + m [3 ] * sinValue ,
156
+ m [0 ] * (-sinValue ) + m [2 ] * cosValue ,
157
+ m [1 ] * (-sinValue ) + m [3 ] * cosValue ,
158
+ m [4 ],
159
+ m [5 ]
160
+ ];
161
+
162
+ this ._originalRotate (angle );
163
+ }
164
+ }
165
+ }
166
+
62
167
var CanvasGraphics = (function canvasGraphics () {
63
168
// Defines the time the executeIRQueue is going to be executing
64
169
// before it stops and shedules a continue of execution.
@@ -77,6 +182,10 @@ var CanvasGraphics = (function canvasGraphics() {
77
182
this .xobjs = null ;
78
183
this .ScratchCanvas = ScratchCanvas ;
79
184
this .objs = objs ;
185
+
186
+ if (canvasCtx ) {
187
+ addCtxCurrentTransform (canvasCtx );
188
+ }
80
189
}
81
190
82
191
var LINE_CAP_STYLES = ['butt' , 'round' , 'square' ];
0 commit comments