summaryrefslogtreecommitdiff
path: root/excanvas.js
diff options
context:
space:
mode:
Diffstat (limited to 'excanvas.js')
-rw-r--r--excanvas.js58
1 files changed, 49 insertions, 9 deletions
diff --git a/excanvas.js b/excanvas.js
index 73963d8..367764b 100644
--- a/excanvas.js
+++ b/excanvas.js
@@ -797,6 +797,33 @@ if (!document.createElement('canvas').getContext) {
this.m_ = this.mStack_.pop();
};
+ function matrixIsFinite(m) {
+ for (var j = 0; j < 3; j++) {
+ for (var k = 0; k < 2; k++) {
+ if (!isFinite(m[j][k]) || isNaN(m[j][k])) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ function setM(ctx, m, updateLineScale) {
+ if (!matrixIsFinite(m)) {
+ return;
+ }
+ ctx.m_ = m;
+
+ if (updateLineScale) {
+ // Get the line scale.
+ // Determinant of this.m_ means how much the area is enlarged by the
+ // transformation. So its square root can be used as a scale factor
+ // for width.
+ var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
+ ctx.lineScale_ = sqrt(abs(det));
+ }
+ }
+
contextPrototype.translate = function(aX, aY) {
var m1 = [
[1, 0, 0],
@@ -804,7 +831,7 @@ if (!document.createElement('canvas').getContext) {
[aX, aY, 1]
];
- this.m_ = matrixMultiply(m1, this.m_);
+ setM(this, matrixMultiply(m1, this.m_), false);
};
contextPrototype.rotate = function(aRot) {
@@ -817,7 +844,7 @@ if (!document.createElement('canvas').getContext) {
[0, 0, 1]
];
- this.m_ = matrixMultiply(m1, this.m_);
+ setM(this, matrixMultiply(m1, this.m_), false);
};
contextPrototype.scale = function(aX, aY) {
@@ -829,14 +856,27 @@ if (!document.createElement('canvas').getContext) {
[0, 0, 1]
];
- var m = this.m_ = matrixMultiply(m1, this.m_);
+ setM(this, matrixMultiply(m1, this.m_), true);
+ };
+
+ contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {
+ var m1 = [
+ [m11, m12, 0],
+ [m21, m22, 0],
+ [dx, dy, 1]
+ ];
+
+ setM(this, matrixMultiply(m1, this.m_), true);
+ };
+
+ contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {
+ var m = [
+ [m11, m12, 0],
+ [m21, m22, 0],
+ [dx, dy, 1]
+ ];
- // Get the line scale.
- // Determinant of this.m_ means how much the area is enlarged by the
- // transformation. So its square root can be used as a scale factor
- // for width.
- var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
- this.lineScale_ = sqrt(abs(det));
+ setM(this, m, true);
};
/******** STUBS ********/