ctx.prototype.__applyTransformation

This commit is contained in:
Zeno Zeng 2021-06-04 23:49:08 +08:00
parent d4c59a9c49
commit 3703af4632
3 changed files with 21 additions and 6 deletions

View File

@ -276,6 +276,9 @@
//also add a group child. the svg element can't use the transform attribute //also add a group child. the svg element can't use the transform attribute
this.__currentElement = this.__document.createElementNS("http://www.w3.org/2000/svg", "g"); this.__currentElement = this.__document.createElementNS("http://www.w3.org/2000/svg", "g");
this.__root.appendChild(this.__currentElement); this.__root.appendChild(this.__currentElement);
// init transformation matrix
this.resetTransform();
}; };
@ -342,6 +345,14 @@
return styleState; return styleState;
}; };
/**
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
*/
ctx.prototype.__applyTransformation = function (element) {
const {a, b, c, d, e, f} = this.getTransform();
element.setAttribute('transform', `matrix(${a} ${b} ${c} ${d} ${e} ${f})`)
}
/** /**
* Apples the current styles to the current SVG element. On "ctx.fill" or "ctx.stroke" * Apples the current styles to the current SVG element. On "ctx.fill" or "ctx.stroke"
* @param type * @param type
@ -753,6 +764,7 @@
parent = this.__closestGroupOrSvg(); parent = this.__closestGroupOrSvg();
parent.appendChild(rect); parent.appendChild(rect);
this.__currentElement = rect; this.__currentElement = rect;
this.__applyTransformation(rect);
this.__applyStyleToCurrentElement("fill"); this.__applyStyleToCurrentElement("fill");
}; };
@ -774,6 +786,7 @@
parent = this.__closestGroupOrSvg(); parent = this.__closestGroupOrSvg();
parent.appendChild(rect); parent.appendChild(rect);
this.__currentElement = rect; this.__currentElement = rect;
this.__applyTransformation(rect);
this.__applyStyleToCurrentElement("stroke"); this.__applyStyleToCurrentElement("stroke");
}; };
@ -784,8 +797,6 @@
* 2. remove all the childNodes of the root g element * 2. remove all the childNodes of the root g element
*/ */
ctx.prototype.__clearCanvas = function () { ctx.prototype.__clearCanvas = function () {
var current = this.__closestGroupOrSvg(),
transform = current.getAttribute("transform");
var rootGroup = this.__root.childNodes[1]; var rootGroup = this.__root.childNodes[1];
var childNodes = rootGroup.childNodes; var childNodes = rootGroup.childNodes;
for (var i = childNodes.length - 1; i >= 0; i--) { for (var i = childNodes.length - 1; i >= 0; i--) {
@ -815,6 +826,7 @@
height : height, height : height,
fill : "#FFFFFF" fill : "#FFFFFF"
}, true); }, true);
this.__applyTransformation(rect)
parent.appendChild(rect); parent.appendChild(rect);
}; };
@ -925,6 +937,7 @@
textElement.appendChild(this.__document.createTextNode(text)); textElement.appendChild(this.__document.createTextNode(text));
this.__currentElement = textElement; this.__currentElement = textElement;
this.__applyTransformation(textElement);
this.__applyStyleToCurrentElement(action); this.__applyStyleToCurrentElement(action);
parent.appendChild(this.__wrapTextLink(font,textElement)); parent.appendChild(this.__wrapTextLink(font,textElement));
}; };
@ -1218,7 +1231,7 @@
*/ */
ctx.prototype.rotate = function (angle) { ctx.prototype.rotate = function (angle) {
let matrix = this.getTransform().multiply(new DOMMatrix([ let matrix = this.getTransform().multiply(new DOMMatrix([
Maht.cos(angle), Math.cos(angle),
Math.sin(angle), Math.sin(angle),
-Math.sin(angle), -Math.sin(angle),
Math.cos(angle), Math.cos(angle),

View File

@ -11,4 +11,6 @@ window.C2S_EXAMPLES['rotate'] = function(ctx) {
// Rotated rectangle // Rotated rectangle
ctx.fillStyle = 'red'; ctx.fillStyle = 'red';
ctx.fillRect(80, 60, 140, 30); ctx.fillRect(80, 60, 140, 30);
ctx.resetTransform();
} }

View File

@ -1,17 +1,16 @@
window.C2S_EXAMPLES['transform'] = function(ctx) { window.C2S_EXAMPLES['transform'] = function(ctx) {
// case 1 // case 1
ctx.resetTransform();
ctx.fillStyle = "rgba(255, 0, 0, 0.5)"; ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
ctx.setTransform(1,1,0,1,0,0); ctx.setTransform(1,1,0,1,0,0);
ctx.fillRect(0,0,100,100); ctx.fillRect(0,0,100,100);
ctx.resetTransform();
// case 2 // case 2
ctx.resetTransform();
ctx.fillStyle = "red"; ctx.fillStyle = "red";
ctx.fillRect(0, 0, 50, 50); ctx.fillRect(0, 0, 50, 50);
ctx.resetTransform();
// case 3 // case 3
ctx.resetTransform();
ctx.fillStyle = "rgba(0, 0, 255, 0.5)"; ctx.fillStyle = "rgba(0, 0, 255, 0.5)";
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(0, 0); ctx.moveTo(0, 0);
@ -22,4 +21,5 @@ window.C2S_EXAMPLES['transform'] = function(ctx) {
ctx.lineTo(100, 100) ctx.lineTo(100, 100)
ctx.closePath() ctx.closePath()
ctx.fill() ctx.fill()
ctx.resetTransform();
} }