[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/WebGL_API/Matrix_math_for_the_web [Back]  [Original]

- Web API | MDN

MDN Web Docs

View in English Always switch to English

CSS matrix3d transform

CSS WebGLWebXRVR ARAPIGLSL MDN MDN

3D 44 16 JavaScript

(identity matrix) 1 n * 1 = n

JavaScript

js
let identityMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];

3D 3 (xyz) 44 4 (perspective) w w 1

w

js
[1, 0, 0, 0,
 0, 1, 0, 0,
 0, 0, 1, 0,
 0, 0, 0, 1]

[4, 3, 2, 1] // Point at [x, y, z, w]

w WebGL

multiplyMatrixAndPoint()

js
//   
function multiplyMatrixAndPoint(matrix, point) {
  //  c r 
  let c0r0 = matrix[0],
    c1r0 = matrix[1],
    c2r0 = matrix[2],
    c3r0 = matrix[3];
  let c0r1 = matrix[4],
    c1r1 = matrix[5],
    c2r1 = matrix[6],
    c3r1 = matrix[7];
  let c0r2 = matrix[8],
    c1r2 = matrix[9],
    c2r2 = matrix[10],
    c3r2 = matrix[11];
  let c0r3 = matrix[12],
    c1r3 = matrix[13],
    c2r3 = matrix[14],
    c3r3 = matrix[15];

  // 
  let x = point[0];
  let y = point[1];
  let z = point[2];
  let w = point[3];

  // 1
  let resultX = x * c0r0 + y * c0r1 + z * c0r2 + w * c0r3;

  // 2
  let resultY = x * c1r0 + y * c1r1 + z * c1r2 + w * c1r3;

  // 3
  let resultZ = x * c2r0 + y * c2r1 + z * c2r2 + w * c2r3;

  // 4
  let resultW = x * c3r0 + y * c3r1 + z * c3r2 + w * c3r3;

  return [resultX, resultY, resultZ, resultW];
}

js
// identityResult  [4,3,2,1] 
let identityResult = multiplyMatrixAndPoint(identityMatrix, [4, 3, 2, 1]);

2

2

js
// B  A
function multiplyMatrices(matrixA, matrixB) {
  // 2
  let row0 = [matrixB[0], matrixB[1], matrixB[2], matrixB[3]];
  let row1 = [matrixB[4], matrixB[5], matrixB[6], matrixB[7]];
  let row2 = [matrixB[8], matrixB[9], matrixB[10], matrixB[11]];
  let row3 = [matrixB[12], matrixB[13], matrixB[14], matrixB[15]];

  // A
  let result0 = multiplyMatrixAndPoint(matrixA, row0);
  let result1 = multiplyMatrixAndPoint(matrixA, row1);
  let result2 = multiplyMatrixAndPoint(matrixA, row2);
  let result3 = multiplyMatrixAndPoint(matrixA, row3);

  // 
  return [
    result0[0],
    result0[1],
    result0[2],
    result0[3],
    result1[0],
    result1[1],
    result1[2],
    result1[3],
    result2[0],
    result2[1],
    result2[2],
    result2[3],
    result3[0],
    result3[1],
    result3[2],
    result3[3],
  ];
}

js
let someMatrix = [4, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 4, 8, 4, 1];

let identityMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];

// someMatrix 
let someMatrixResult = multiplyMatrices(identityMatrix, someMatrix);

: glMatrix glMatrix

(translation matrix) 3D 3 xyz 1

****

js
let x = 50;
let y = 100;
let z = 0;

let translationMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1];

3 3D

DOM

CSS matrix3d() transform <div> <div> transform

html
<div id="move-me" class="transformable">
  <h2>Move me with a matrix</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
</div>

44 <div> transform matrix3d 4 4 16 1 JavaScript 1

js
//  matrix3d 
function matrixArrayToCssMatrix(array) {
  return `matrix3d(${array.join(",")})`;
}

// DOM 
let moveMe = document.getElementById("move-me");

// : "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 50, 100, 0, 1);"
let matrix3dRule = matrixArrayToCssMatrix(translationMatrix);

// transform 
moveMe.style.transform = matrix3dRule;

JSFiddle

[]

(scale matrix) 3 1

js
let w = 1.5; // width  (x)
let h = 0.7; // height (y)
let d = 1; // depth  (z)

let scaleMatrix = [w, 0, 0, 0, 0, h, 0, 0, 0, 0, d, 0, 0, 0, 0, 1];

JSFiddle

[]

(rotation matrix) Wolfram MathWorld

js
// 
let point = [10, 2];

// 
let distance = Math.sqrt(point[0] * point[0] + point[1] * point[1]);

//  60 
let rotationInRadians = Math.PI / 3;

let transformedPoint = [
  Math.cos(rotationInRadians) * distance,
  Math.sin(rotationInRadians) * distance,
];

xyz Z

js
let sin = Math.sin;
let cos = Math.cos;

// : 
//      div 

let a = Math.PI * 0.3; // 

// Z 
let rotateZMatrix = [
  cos(a),
  -sin(a),
  0,
  0,
  sin(a),
  cos(a),
  0,
  0,
  0,
  0,
  1,
  0,
  0,
  0,
  0,
  1,
];

JSFiddle

[]

3 1 3D (flatness) (sense of perspective)

js
function rotateAroundXAxis(a) {
  return [1, 0, 0, 0, 0, cos(a), -sin(a), 0, 0, sin(a), cos(a), 0, 0, 0, 0, 1];
}

function rotateAroundYAxis(a) {
  return [cos(a), 0, sin(a), 0, 0, 1, 0, 0, -sin(a), 0, cos(a), 0, 0, 0, 0, 1];
}

function rotateAroundZAxis(a) {
  return [cos(a), -sin(a), 0, 0, sin(a), cos(a), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
}

JSFiddle

(matrix composition)

a * b = c b * a = c 3 * 4 = 12 4 * 3 = 12 (commutative) (non-commutative)

1 WebGL CSS 80% 200 90

transformation = rotate * translate * scale

multiplyArrayOfMatrices() WebGL * scale() translate()

js
let transformMatrix = MDN.multiplyArrayOfMatrices([
  rotateAroundZAxis(Math.PI * 0.5), //  3: 90
  translate(0, 200, 0), //  2: 100
  scale(0.8, 0.8, 0.8), //  1: 
]);

JSFiddle

[]

js
let transformMatrix = MDN.multiplyArrayOfMatrices([
  scale(1.25, 1.25, 1.25), //  6: 
  translate(0, -200, 0), //  5: 
  rotateAroundZAxis(-Math.PI * 0.5), //  4: 
  rotateAroundZAxis(Math.PI * 0.5), //  3: 90
  translate(0, 200, 0), //  2: 100
  scale(0.8, 0.8, 0.8), //  1: 
]);

WebGL


Web Proxy Viewer  |  New URL  |  Original Page