Skip to content

Commit a30f0ff

Browse files
committed
Implement ctx.mozCurrentTransform and ctx.mozCurrentTransformInverse shim
1 parent 5bae370 commit a30f0ff

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/canvas.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,111 @@ function ScratchCanvas(width, height) {
5959
return canvas;
6060
}
6161

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+
62167
var CanvasGraphics = (function canvasGraphics() {
63168
// Defines the time the executeIRQueue is going to be executing
64169
// before it stops and shedules a continue of execution.
@@ -77,6 +182,10 @@ var CanvasGraphics = (function canvasGraphics() {
77182
this.xobjs = null;
78183
this.ScratchCanvas = ScratchCanvas;
79184
this.objs = objs;
185+
186+
if (canvasCtx) {
187+
addCtxCurrentTransform(canvasCtx);
188+
}
80189
}
81190

82191
var LINE_CAP_STYLES = ['butt', 'round', 'square'];

src/pattern.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ var TilingPattern = (function tilingPattern() {
217217

218218
// set the new canvas element context as the graphics context
219219
var tmpCtx = tmpCanvas.getContext('2d');
220+
addCtxCurrentTransform(tmpCtx);
221+
220222
var graphics = new CanvasGraphics(tmpCtx, objs);
221223

222224
switch (paintType) {

0 commit comments

Comments
 (0)