How to calculate SVG transform matrix from rotate/translate/scale values?

前端 未结 3 774
面向向阳花
面向向阳花 2020-12-07 16:38

I have following details with me :


Need to change above line to:

相关标签:
3条回答
  • 2020-12-07 17:17

    Maybe helpful:

    1. Live demo of how to find actual coordinates of transformed points

    2. An implementation of the accepted answer:

      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]})`);
      
    0 讨论(0)
  • 2020-12-07 17:19

    Translate(tx, ty) can be written as the matrix:

    1  0  tx
    0  1  ty
    0  0  1
    

    Scale(sx, sy) can be written as the matrix:

    sx  0  0
    0  sy  0
    0   0  1
    

    Rotate(a) can be written as a matrix:

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

    Rotate(a, cx, cy) is the combination of a translation by (-cx, cy), a rotation of a degrees and a translation back to (cx, cy), which gives:

    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
    

    If you just multiply this with the translation matrix you get:

    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
    

    Which corresponds to the SVG transform matrix:

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

    In your case that is: matrix(0.866, -0.5 0.5 0.866 8.84 58.35).

    If you include the scale (sx, sy) transform, the matrix is:

    (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)

    Note that this assumes you are doing the transformations in the order you wrote them.

    0 讨论(0)
  • 2020-12-07 17:28

    First get the g element using document.getElementById if it has an id attribute or some other appropriate method, then call consolidate e.g.

    var g = document.getElementById("<whatever the id is>");
    g.transform.baseVal.consolidate();
    
    0 讨论(0)
提交回复
热议问题