dommetry
Version:
The W3C Geometry Interfaces implemented in JavaScript and polyfilled.
397 lines (353 loc) • 14.4 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x14, _x15, _x16) { var _again = true; _function: while (_again) { var object = _x14, property = _x15, receiver = _x16; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x14 = parent; _x15 = property; _x16 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _DOMMatrixReadOnly2 = require('./DOMMatrixReadOnly');
var _DOMMatrixReadOnly3 = _interopRequireDefault(_DOMMatrixReadOnly2);
var _utilities = require('./utilities');
var DOMMatrix = (function (_DOMMatrixReadOnly) {
_inherits(DOMMatrix, _DOMMatrixReadOnly);
function DOMMatrix() {
_classCallCheck(this, DOMMatrix);
if (arguments.length === 0) {
var numberSequence = [1, 0, 0, 1, 0, 0];
_get(Object.getPrototypeOf(DOMMatrix.prototype), 'constructor', this).call(this, numberSequence);
} else if (arguments.length === 1) {
if (typeof arguments[0] == 'string') {
var transformList = arguments[0];
// TODO validate that syntax of transformList matches transform-list (http://www.w3.org/TR/css-transforms-1/#typedef-transform-list).
// TODO ...
} else if (arguments[0] instanceof _DOMMatrixReadOnly3['default']) {
var other = arguments[0];
_get(Object.getPrototypeOf(DOMMatrix.prototype), 'constructor', this).call(this, (0, _utilities.matrixToArray)(other));
} else if (arguments[0] instanceof Float32Array || arguments[0] instanceof Float64Array) {
var typedArray = arguments[0];
if (typedArray.length === 6 || typedArray.length === 16) {
_get(Object.getPrototypeOf(DOMMatrix.prototype), 'constructor', this).call(this, Array.from(typedArray));
} else {
throw new TypeError('The typed array argument to the DOMMatrix constructor has an invalid length.');
}
} else if (arguments[0] instanceof Array /* TODO && all items are numbers */) {
var numberSequence = arguments[0];
if (numberSequence.length === 6 || numberSequence.length === 16) {
_get(Object.getPrototypeOf(DOMMatrix.prototype), 'constructor', this).call(this, numberSequence);
} else {
throw new TypeError('The array argument to the DOMMatrix constructor has an invalid length.');
}
}
} else {
throw new Error('Wrong number of arguments to DOMMatrix constructor.');
}
}
// Mutable transform methods
_createClass(DOMMatrix, [{
key: 'multiplySelf',
value: function multiplySelf(other) {
if (!other instanceof _DOMMatrixReadOnly3['default']) throw new Error('The argument to multiplySelf must be an instance of DOMMatrixReadOnly or DOMMatrix');
var resultArray = (0, _utilities.multiplyToArray)(this, other);
(0, _utilities.applyArrayValuesToDOMMatrix)(resultArray, this);
if (!other.is2D) this._is2D = false;
return this;
}
}, {
key: 'preMultiplySelf',
value: function preMultiplySelf(other) {
if (!other instanceof _DOMMatrixReadOnly3['default']) throw new Error('The argument to multiplySelf must be an instance of DOMMatrixReadOnly or DOMMatrix');
var resultArray = (0, _utilities.multiplyToArray)(other, this);
(0, _utilities.applyArrayValuesToDOMMatrix)(resultArray, this);
if (!other.is2D) this._is2D = false;
return this;
}
}, {
key: 'translateSelf',
value: function translateSelf(tx, ty) {
var tz = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
// TODO: check args are numbers
if (arguments.length === 1) throw new Error('The first two arguments (X and Y translation values) are required (the third, Z translation, is optional).');
// http://www.w3.org/TR/2012/WD-css3-transforms-20120911/#Translate3dDefined
var translationMatrix = new DOMMatrix([
// column-major:
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, tx, ty, tz, 1]);
this.multiplySelf(translationMatrix);
if (tz != 0) {
this._is2D = false;
}
return this;
}
}, {
key: 'scaleSelf',
value: function scaleSelf(scale) {
var originX = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
var originY = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
this.translateSelf(originX, originY);
this.multiplySelf(new DOMMatrix([
// 2D:
/*a*/scale, /*b*/0,
/*c*/0, /*d*/scale,
/*e*/0, /*f*/0]));
this.translateSelf(-originX, -originY);
return this;
}
}, {
key: 'scale3dSelf',
value: function scale3dSelf(scale) {
var originX = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
var originY = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
var originZ = arguments.length <= 3 || arguments[3] === undefined ? 0 : arguments[3];
this.translateSelf(originX, originY, originZ);
this.multiplySelf(new DOMMatrix([
// 3D
scale, 0, 0, 0, 0, scale, 0, 0, 0, 0, scale, 0, 0, 0, 0, 1]));
this.translateSelf(-originX, -originY, -originZ);
return this;
}
}, {
key: 'scaleNonUniformSelf',
value: function scaleNonUniformSelf(scaleX) {
var scaleY = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1];
var scaleZ = arguments.length <= 2 || arguments[2] === undefined ? 1 : arguments[2];
var originX = arguments.length <= 3 || arguments[3] === undefined ? 0 : arguments[3];
var originY = arguments.length <= 4 || arguments[4] === undefined ? 0 : arguments[4];
var originZ = arguments.length <= 5 || arguments[5] === undefined ? 0 : arguments[5];
this.translateSelf(originX, originY, originZ);
this.multiplySelf(new DOMMatrix([
// 3D
scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, scaleZ, 0, 0, 0, 0, 1]));
this.translateSelf(-originX, -originY, -originZ);
if (scaleZ !== 1 || originZ !== 0) this._is2D = false;
return this;
}
}, {
key: 'rotateSelf',
value: function rotateSelf(angle) {
var originX = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
var originY = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
this.translateSelf(originX, originY);
// axis of rotation
var x = 0;
var y = 0;
var z = 1;
// We're rotating around the Z axis.
this.rotateAxisAngleSelf(x, y, z, angle);
this.translateSelf(-originX, -originY);
return this;
}
// TODO
}, {
key: 'rotateFromVectorSelf',
value: function rotateFromVectorSelf(x, y) {
throw new Error('rotateFromVectorSelf is not implemented yet.');
}
}, {
key: 'rotateAxisAngleSelf',
value: function rotateAxisAngleSelf(x, y, z, angle) {
this.multiplySelf(new DOMMatrix((0, _utilities.rotateAxisAngleArray)(x, y, z, angle)));
return this;
}
}, {
key: 'skewXSelf',
value: function skewXSelf(sx) {
throw new Error('skewXSelf is not implemented yet.');
}
}, {
key: 'skewYSelf',
value: function skewYSelf(sy) {
throw new Error('skewYSelf is not implemented yet.');
}
}, {
key: 'invertSelf',
value: function invertSelf() {
throw new Error('invertSelf is not implemented yet.');
}
}, {
key: 'setMatrixValue',
value: function setMatrixValue( /*DOMString*/transformList) {
throw new Error('setMatrixValue is not implemented yet.');
}
}, {
key: 'a',
get: function get() {
return this.m11;
},
set: function set(value) {
this.m11 = value;
}
}, {
key: 'b',
get: function get() {
return this.m12;
},
set: function set(value) {
this.m12 = value;
}
}, {
key: 'c',
get: function get() {
return this.m21;
},
set: function set(value) {
this.m21 = value;
}
}, {
key: 'd',
get: function get() {
return this.m22;
},
set: function set(value) {
this.m22 = value;
}
}, {
key: 'e',
get: function get() {
return this.m41;
},
set: function set(value) {
this.m41 = value;
}
}, {
key: 'f',
get: function get() {
return this.m42;
},
set: function set(value) {
this.m42 = value;
}
}, {
key: 'm11',
get: function get() {
return this._matrix[0];
},
set: function set(value) {
this._matrix[0] = value;
}
}, {
key: 'm12',
get: function get() {
return this._matrix[4];
},
set: function set(value) {
this._matrix[4] = value;
}
}, {
key: 'm13',
get: function get() {
return this._matrix[8];
},
set: function set(value) {
this._matrix[8] = value;
}
}, {
key: 'm14',
get: function get() {
return this._matrix[12];
},
set: function set(value) {
this._matrix[12] = value;
}
}, {
key: 'm21',
get: function get() {
return this._matrix[1];
},
set: function set(value) {
this._matrix[1] = value;
}
}, {
key: 'm22',
get: function get() {
return this._matrix[5];
},
set: function set(value) {
this._matrix[5] = value;
}
}, {
key: 'm23',
get: function get() {
return this._matrix[9];
},
set: function set(value) {
this._matrix[9] = value;
}
}, {
key: 'm24',
get: function get() {
return this._matrix[13];
},
set: function set(value) {
this._matrix[13] = value;
}
}, {
key: 'm31',
get: function get() {
return this._matrix[2];
},
set: function set(value) {
this._matrix[2] = value;
}
}, {
key: 'm32',
get: function get() {
return this._matrix[6];
},
set: function set(value) {
this._matrix[6] = value;
}
}, {
key: 'm33',
get: function get() {
return this._matrix[10];
},
set: function set(value) {
this._matrix[10] = value;
}
}, {
key: 'm34',
get: function get() {
return this._matrix[14];
},
set: function set(value) {
this._matrix[14] = value;
}
}, {
key: 'm41',
get: function get() {
return this._matrix[3];
},
set: function set(value) {
this._matrix[3] = value;
}
}, {
key: 'm42',
get: function get() {
return this._matrix[7];
},
set: function set(value) {
this._matrix[7] = value;
}
}, {
key: 'm43',
get: function get() {
return this._matrix[11];
},
set: function set(value) {
this._matrix[11] = value;
}
}, {
key: 'm44',
get: function get() {
return this._matrix[15];
},
set: function set(value) {
this._matrix[15] = value;
}
}]);
return DOMMatrix;
})(_DOMMatrixReadOnly3['default']);
exports['default'] = DOMMatrix;
module.exports = exports['default'];
//# sourceMappingURL=DOMMatrix.js.map