38

私は私と一緒に次の詳細を持っています:

<g transform="translate(20, 50) scale(1, 1) rotate(-30 10 25)">

上記の行を次のように変更する必要があります。

<g transform="matrix(?,?,?,?,?,?)">

誰かが私がこれを達成するのを手伝ってくれる?

4

3 に答える 3

69

Translate(tx、ty)は、マトリックスとして記述できます。

1  0  tx
0  1  ty
0  0  1

Scale(sx、sy)は、行列として記述できます。

sx  0  0
0  sy  0
0   0  1

Rotate(a)は行列として書くことができます:

cos(a)  -sin(a)  0
sin(a)   cos(a)  0
0        0       1

Rotate(a、cx、cy)は、(-cx、cy)による平行移動、度の回転、および(cx、​​cy)への平行移動の組み合わせであり、次のようになります。

cos(a)  -sin(a)  -cx × cos(a) + cy × sin(a) + cx
sin(a)   cos(a)  -cx × sin(a) - cy × cos(a) + cy
0        0       1

これに変換行列を掛けると、次のようになります。

cos(a)  -sin(a)  -cx × cos(a) + cy × sin(a) + cx + tx
sin(a)   cos(a)  -cx × sin(a) - cy × cos(a) + cy + ty
0        0       1

これはSVG変換行列に対応します:

(cos(a), sin(a), -sin(a), cos(a), -cx × cos(a) + cy × sin(a) + cx + tx, -cx × sin(a) - cy × cos(a) + cy + ty)

あなたの場合、それは:matrix(0.866, -0.5 0.5 0.866 8.84 58.35)

scale(sx、sy)変換を含めると、行列は次のようになります。

(sx × cos(a), sy × sin(a), -sx × sin(a), sy × cos(a), (-cx × cos(a) + cy × sin(a) + cx) × sx + tx, (-cx × sin(a) - cy × cos(a) + cy) × sy + ty)

これは、作成した順序で変換を実行していることを前提としていることに注意してください。

于 2013-02-28T11:59:18.870 に答える
12

最初に、id属性またはその他の適切なメソッドがある場合はdocument.getElementByIdを使用してg要素を取得し、次にconsolidateを呼び出します。

var g = document.getElementById("<whatever the id is>");
g.transform.baseVal.consolidate();
于 2013-02-28T11:21:53.747 に答える
3

多分役立つ:

  1. 変換されたポイントの実際の座標を見つける方法のライブデモ

  2. 受け入れられた答えの実装:

    function multiplyMatrices(matrixA, matrixB) {
        let aNumRows = matrixA.length;
        let aNumCols = matrixA[0].length;
        let bNumRows = matrixB.length;
        let bNumCols = matrixB[0].length;
        let newMatrix = new Array(aNumRows);
    
        for (let r = 0; r < aNumRows; ++r) {
            newMatrix[r] = new Array(bNumCols);
    
            for (let c = 0; c < bNumCols; ++c) {
                newMatrix[r][c] = 0;
    
                for (let i = 0; i < aNumCols; ++i) {
                    newMatrix[r][c] += matrixA[r][i] * matrixB[i][c];
                }
            }
        }
    
        return newMatrix;
    }
    
    let translation = {
        x: 200,
        y: 50
    };
    let scaling = {
        x: 1.5,
        y: 1.5
    };
    let angleInDegrees = 25;
    let angleInRadians = angleInDegrees * (Math.PI / 180);
    let translationMatrix = [
        [1, 0, translation.x],
        [0, 1, translation.y],
        [0, 0, 1],
    ];
    let scalingMatrix = [
        [scaling.x, 0, 0],
        [0, scaling.y, 0],
        [0, 0, 1],
    ];
    let rotationMatrix = [
        [Math.cos(angleInRadians), -Math.sin(angleInRadians), 0],
        [Math.sin(angleInRadians), Math.cos(angleInRadians), 0],
        [0, 0, 1],
    ];
    let transformMatrix = multiplyMatrices(multiplyMatrices(translationMatrix, scalingMatrix), rotationMatrix);
    
    console.log(`matrix(${transformMatrix[0][0]}, ${transformMatrix[1][0]}, ${transformMatrix[0][1]}, ${transformMatrix[1][1]}, ${transformMatrix[0][2]}, ${transformMatrix[1][2]})`);
    
于 2018-05-20T09:28:32.780 に答える