Skip to content

Commit fd01dc2

Browse files
committed
allow passing in vectors as props
1 parent 1c61f7f commit fd01dc2

File tree

8 files changed

+366
-89
lines changed

8 files changed

+366
-89
lines changed

examples/3d-cubes/package-lock.json

Lines changed: 40 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/3d-cubes/src/components/Cube.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FlatMaterial } from './FlatMaterial';
55
import { halfPi, yAxis, xAxis } from '../constants';
66

77
export function Cube(props) {
8-
const { faceSize, color, shadowColor, ...otherProps } = props;
8+
const { faceSize, color, ...otherProps } = props;
99

1010
const halfFaceSize = faceSize / 2;
1111

examples/3d-cubes/src/components/FlatMaterial.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
//import { glMatrixType, vec3Shape } from 'react-css-transform';
3+
import { glMatrixType } from 'react-css-transform';
44
import { vec3, mat3 } from 'gl-matrix';
55

66
function hexStringToRgb(hexString) {
@@ -88,7 +88,7 @@ export class FlatMaterial extends React.Component {
8888

8989
FlatMaterial.propTypes = {
9090
color: PropTypes.string.isRequired,
91-
parentMatrixWorld: PropTypes.oneOfType([PropTypes.instanceOf(Float32Array), PropTypes.array]),
91+
parentMatrixWorld: glMatrixType,
9292
style: PropTypes.object,
9393
children: PropTypes.node.isRequired,
9494
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-css-transform",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "A React component to help handle complex nested 2d and 3d css transformations ",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/Transform2d.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import PropTypes from 'prop-types';
33
import { mat2d, vec2 } from 'gl-matrix';
44

55
import { MULTIPLICATION_ORDER, vec2Shape, glMatrixType } from './constants';
6+
import { setVec2FromProp } from './utils';
67

78
export default class Transform2d extends React.Component {
89
static propTypes = {
910
parentMatrixWorld: glMatrixType,
1011
multiplicationOrder: PropTypes.oneOf([MULTIPLICATION_ORDER.PRE, MULTIPLICATION_ORDER.POST]),
11-
translate: vec2Shape,
12-
scale: PropTypes.oneOfType([vec2Shape, glMatrixType, PropTypes.number]),
12+
translate: PropTypes.oneOfType([glMatrixType, vec2Shape]),
13+
scale: PropTypes.oneOfType([glMatrixType, vec2Shape, PropTypes.number]),
1314
rotate: PropTypes.number,
1415
children: PropTypes.node.isRequired,
1516
};
@@ -28,24 +29,11 @@ export default class Transform2d extends React.Component {
2829
const { children, parentMatrixWorld, multiplicationOrder, translate, scale, rotate } = this.props;
2930
const { matrix, matrixWorld, vTranslation, vScale } = this;
3031

31-
const tx = translate && translate.x ? translate.x : 0;
32-
const ty = translate && translate.y ? translate.y : 0;
33-
34-
let sx, sy;
35-
36-
if (typeof scale === 'number') {
37-
sx = scale;
38-
sy = scale;
39-
} else {
40-
sx = scale && scale.x ? scale.x : 1;
41-
sy = scale && scale.y ? scale.y : 1;
42-
}
43-
44-
vec2.set(vTranslation, tx, ty);
45-
vec2.set(vScale, sx, sy);
46-
4732
mat2d.identity(matrix);
4833

34+
setVec2FromProp(vTranslation, translate);
35+
setVec2FromProp(vScale, scale, 1);
36+
4937
// T * R * S
5038
mat2d.translate(matrix, matrix, vTranslation);
5139
mat2d.rotate(matrix, matrix, rotate || 0);

src/Transform3d.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import PropTypes from 'prop-types';
33
import { mat4, vec3 } from 'gl-matrix';
44

55
import { MULTIPLICATION_ORDER, vec3Shape, glMatrixType } from './constants';
6+
import { setVec3FromProp } from './utils';
67

78
export default class Transform3d extends React.Component {
89
static propTypes = {
910
parentMatrixWorld: glMatrixType,
1011
multiplicationOrder: PropTypes.oneOf([MULTIPLICATION_ORDER.PRE, MULTIPLICATION_ORDER.POST]),
11-
translate: vec3Shape,
12-
scale: PropTypes.oneOfType([vec3Shape, PropTypes.number]),
12+
translate: PropTypes.oneOfType([glMatrixType, vec3Shape]),
13+
scale: PropTypes.oneOfType([glMatrixType, vec3Shape, PropTypes.number]),
1314
rotate: PropTypes.number,
14-
rotateAxis: vec3Shape,
15+
rotateAxis: PropTypes.oneOfType([glMatrixType, vec3Shape]),
1516
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
1617
};
1718

@@ -35,33 +36,15 @@ export default class Transform3d extends React.Component {
3536
const { children, parentMatrixWorld, multiplicationOrder, translate, rotate, rotateAxis, scale } = this.props;
3637
const { matrix, matrixWorld, vTranslation, vScale, vRotationAxis } = this;
3738

38-
const tx = translate && translate.x ? translate.x : 0;
39-
const ty = translate && translate.y ? translate.y : 0;
40-
const tz = translate && translate.z ? translate.z : 0;
41-
42-
const theta = typeof rotate === 'number' ? rotate : 0;
43-
44-
let sx, sy, sz;
45-
46-
if (typeof scale === 'number') {
47-
sx = scale;
48-
sy = scale;
49-
sz = scale;
50-
} else {
51-
sx = scale && scale.x ? scale.x : 1;
52-
sy = scale && scale.y ? scale.y : 1;
53-
sz = scale && scale.z ? scale.z : 1;
54-
}
55-
56-
vec3.set(vTranslation, tx, ty, tz);
57-
vec3.set(vScale, sx, sy, sz);
58-
vec3.set(vRotationAxis, rotateAxis.x, rotateAxis.y, rotateAxis.z);
59-
6039
mat4.identity(matrix);
6140

41+
setVec3FromProp(vTranslation, translate);
42+
setVec3FromProp(vScale, scale, 1, 1, 1);
43+
setVec3FromProp(vRotationAxis, rotateAxis, 0, 0, 1);
44+
6245
// T * R * S
6346
mat4.translate(matrix, matrix, vTranslation);
64-
mat4.rotate(matrix, matrix, theta, vRotationAxis);
47+
mat4.rotate(matrix, matrix, rotate || 0, vRotationAxis);
6548
mat4.scale(matrix, matrix, vScale);
6649

6750
if (multiplicationOrder === MULTIPLICATION_ORDER.PRE) {

src/utils.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { vec2, vec3, glMatrix } from 'gl-matrix';
2+
3+
export function setVec2FromProp(v, prop, defaultValue = 0) {
4+
let x = defaultValue;
5+
let y = defaultValue;
6+
7+
if (!prop) {
8+
return vec2.set(v, x, y);
9+
}
10+
11+
if (typeof prop === 'number') {
12+
x = prop;
13+
y = prop;
14+
} else {
15+
const usingTypedArray = glMatrix.ARRAY_TYPE === Float32Array;
16+
const isArray = usingTypedArray ? prop.byteLength !== undefined : Array.isArray(prop);
17+
18+
if (isArray) {
19+
[x, y] = prop;
20+
} else if (typeof prop === 'object') {
21+
x = prop.x || defaultValue;
22+
y = prop.y || defaultValue;
23+
}
24+
}
25+
26+
return vec2.set(v, x, y);
27+
}
28+
29+
export function setVec3FromProp(v, prop, defaultX = 0, defaultY = 0, defaultZ = 0) {
30+
let x = defaultX;
31+
let y = defaultY;
32+
let z = defaultZ;
33+
34+
if (!prop) {
35+
return vec3.set(v, x, y, z);
36+
}
37+
38+
if (typeof prop === 'number') {
39+
x = prop;
40+
y = prop;
41+
z = prop;
42+
} else {
43+
const usingTypedArray = glMatrix.ARRAY_TYPE === Float32Array;
44+
const isArray = usingTypedArray ? prop.byteLength !== undefined : Array.isArray(prop);
45+
46+
if (isArray) {
47+
[x, y, z] = prop;
48+
} else if (typeof prop === 'object') {
49+
x = prop.x || defaultX;
50+
y = prop.y || defaultY;
51+
z = prop.z || defaultZ;
52+
}
53+
}
54+
55+
return vec3.set(v, x, y, z);
56+
}

0 commit comments

Comments
 (0)