Skip to content

Commit c78882e

Browse files
committed
update excanvas
1 parent a2c8fff commit c78882e

File tree

1 file changed

+62
-14
lines changed

1 file changed

+62
-14
lines changed

excanvas.js

+62-14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ if (!document.createElement('canvas').getContext) {
138138

139139
el.getContext = getContext;
140140

141+
// Remove fallback content. There is no way to hide text nodes so we
142+
// just remove all childNodes. We could hide all elements and remove
143+
// text nodes but who really cares about the fallback content.
144+
el.innerHTML = '';
145+
141146
// do not use inline function because that will leak memory
142147
el.attachEvent('onpropertychange', onPropertyChange);
143148
el.attachEvent('onresize', onResize);
@@ -314,7 +319,6 @@ if (!document.createElement('canvas').getContext) {
314319
var contextPrototype = CanvasRenderingContext2D_.prototype;
315320
contextPrototype.clearRect = function() {
316321
this.element_.innerHTML = '';
317-
this.currentPath_ = [];
318322
};
319323

320324
contextPrototype.beginPath = function() {
@@ -422,27 +426,31 @@ if (!document.createElement('canvas').getContext) {
422426
};
423427

424428
contextPrototype.strokeRect = function(aX, aY, aWidth, aHeight) {
425-
// Will destroy any existing path (same as FF behaviour)
429+
var oldPath = this.currentPath_;
426430
this.beginPath();
431+
427432
this.moveTo(aX, aY);
428433
this.lineTo(aX + aWidth, aY);
429434
this.lineTo(aX + aWidth, aY + aHeight);
430435
this.lineTo(aX, aY + aHeight);
431436
this.closePath();
432437
this.stroke();
433-
this.currentPath_ = [];
438+
439+
this.currentPath_ = oldPath;
434440
};
435441

436442
contextPrototype.fillRect = function(aX, aY, aWidth, aHeight) {
437-
// Will destroy any existing path (same as FF behaviour)
443+
var oldPath = this.currentPath_;
438444
this.beginPath();
445+
439446
this.moveTo(aX, aY);
440447
this.lineTo(aX + aWidth, aY);
441448
this.lineTo(aX + aWidth, aY + aHeight);
442449
this.lineTo(aX, aY + aHeight);
443450
this.closePath();
444451
this.fill();
445-
this.currentPath_ = [];
452+
453+
this.currentPath_ = oldPath;
446454
};
447455

448456
contextPrototype.createLinearGradient = function(aX0, aY0, aX1, aY1) {
@@ -789,14 +797,41 @@ if (!document.createElement('canvas').getContext) {
789797
this.m_ = this.mStack_.pop();
790798
};
791799

800+
function matrixIsFinite(m) {
801+
for (var j = 0; j < 3; j++) {
802+
for (var k = 0; k < 2; k++) {
803+
if (!isFinite(m[j][k]) || isNaN(m[j][k])) {
804+
return false;
805+
}
806+
}
807+
}
808+
return true;
809+
}
810+
811+
function setM(ctx, m, updateLineScale) {
812+
if (!matrixIsFinite(m)) {
813+
return;
814+
}
815+
ctx.m_ = m;
816+
817+
if (updateLineScale) {
818+
// Get the line scale.
819+
// Determinant of this.m_ means how much the area is enlarged by the
820+
// transformation. So its square root can be used as a scale factor
821+
// for width.
822+
var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
823+
ctx.lineScale_ = sqrt(abs(det));
824+
}
825+
}
826+
792827
contextPrototype.translate = function(aX, aY) {
793828
var m1 = [
794829
[1, 0, 0],
795830
[0, 1, 0],
796831
[aX, aY, 1]
797832
];
798833

799-
this.m_ = matrixMultiply(m1, this.m_);
834+
setM(this, matrixMultiply(m1, this.m_), false);
800835
};
801836

802837
contextPrototype.rotate = function(aRot) {
@@ -809,7 +844,7 @@ if (!document.createElement('canvas').getContext) {
809844
[0, 0, 1]
810845
];
811846

812-
this.m_ = matrixMultiply(m1, this.m_);
847+
setM(this, matrixMultiply(m1, this.m_), false);
813848
};
814849

815850
contextPrototype.scale = function(aX, aY) {
@@ -821,14 +856,27 @@ if (!document.createElement('canvas').getContext) {
821856
[0, 0, 1]
822857
];
823858

824-
var m = this.m_ = matrixMultiply(m1, this.m_);
859+
setM(this, matrixMultiply(m1, this.m_), true);
860+
};
861+
862+
contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {
863+
var m1 = [
864+
[m11, m12, 0],
865+
[m21, m22, 0],
866+
[dx, dy, 1]
867+
];
868+
869+
setM(this, matrixMultiply(m1, this.m_), true);
870+
};
871+
872+
contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {
873+
var m = [
874+
[m11, m12, 0],
875+
[m21, m22, 0],
876+
[dx, dy, 1]
877+
];
825878

826-
// Get the line scale.
827-
// Determinant of this.m_ means how much the area is enlarged by the
828-
// transformation. So its square root can be used as a scale factor
829-
// for width.
830-
var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
831-
this.lineScale_ = sqrt(abs(det));
879+
setM(this, m, true);
832880
};
833881

834882
/******** STUBS ********/

0 commit comments

Comments
 (0)