make function just create canvas (#4172)

* make function just create canvas
* added tests
* added tests
This commit is contained in:
Andrea Bogazzi 2017-08-05 21:32:03 +02:00 committed by GitHub
parent 5a791a8291
commit b5323bd83e
3 changed files with 64 additions and 23 deletions

View file

@ -475,14 +475,14 @@
/**
* @private
*/
_createCanvasElement: function(canvasEl) {
var element = fabric.util.createCanvasElement(canvasEl);
if (!element.style) {
element.style = { };
}
_createCanvasElement: function() {
var element = fabric.util.createCanvasElement();
if (!element) {
throw CANVAS_INIT_ERROR;
}
if (!element.style) {
element.style = { };
}
if (typeof element.getContext === 'undefined') {
throw CANVAS_INIT_ERROR;
}
@ -518,7 +518,13 @@
* @param {HTMLElement} [canvasEl]
*/
_createLowerCanvas: function (canvasEl) {
this.lowerCanvasEl = fabric.util.getById(canvasEl) || this._createCanvasElement(canvasEl);
// canvasEl === 'HTMLCanvasElement' does not work on jsdom/node
if (canvasEl.getContext) {
this.lowerCanvasEl = canvasEl;
}
else {
this.lowerCanvasEl = fabric.util.getById(canvasEl) || this._createCanvasElement();
}
fabric.util.addClass(this.lowerCanvasEl, 'lower-canvas');

View file

@ -466,16 +466,13 @@
},
/**
* Creates canvas element and initializes it via excanvas if necessary
* Creates canvas element
* @static
* @memberOf fabric.util
* @param {CanvasElement} [canvasEl] optional canvas element to initialize;
* when not given, element is created implicitly
* @return {CanvasElement} initialized canvas element
*/
createCanvasElement: function(canvasEl) {
canvasEl || (canvasEl = fabric.document.createElement('canvas'));
return canvasEl;
createCanvasElement: function() {
return fabric.document.createElement('canvas');
},
/**

View file

@ -584,14 +584,6 @@
});
}
test('fabric.util.request', function() {
ok(typeof fabric.util.request === 'function', 'fabric.util.request is a function');
});
test('fabric.util.getPointer', function() {
ok(typeof fabric.util.getPointer === 'function', 'fabric.util.getPointer is a function');
});
test('fabric.util.addListener', function() {
ok(typeof fabric.util.addListener === 'function', 'fabric.util.addListener is a function');
fabric.util.addListener(null, 'mouseup');
@ -797,16 +789,37 @@
deepEqual(m3, [-2, 1, 1.5, -0.5, 1, -2]);
});
test('fabric.util.request', function() {
ok(typeof fabric.util.request === 'function', 'fabric.util.request is a function');
});
test('fabric.util.getPointer', function() {
ok(typeof fabric.util.getPointer === 'function', 'fabric.util.getPointer is a function');
});
test('rotateVector', function() {
ok(typeof fabric.util.rotateVector == 'function');
});
test('rotatePoint', function() {
ok(typeof fabric.util.rotatePoint == 'function');
var origin = new fabric.Point(3, 0);
var point = new fabric.Point(4, 0);
var rotated = fabric.util.rotatePoint(point, origin, Math.PI);
equal(Math.round(rotated.x), 2);
equal(Math.round(rotated.y), 0);
var rotated = fabric.util.rotatePoint(point, origin, Math.PI / 2);
equal(Math.round(rotated.x), 3);
equal(Math.round(rotated.y), -2);
});
test('transformPoint', function() {
ok(typeof fabric.util.transformPoint == 'function');
var point = new fabric.Point(2, 2);
var matrix = [3, 0, 0, 2, 10, 4];
var tp = fabric.util.transformPoint(point, matrix);
equal(Math.round(tp.x), 16);
equal(Math.round(tp.y), 8);
});
test('makeBoundingBoxFromPoints', function() {
@ -823,19 +836,44 @@
});
test('createCanvasElement', function() {
ok(typeof fabric.util.createCanvasElement == 'function');
ok(typeof fabric.util.createCanvasElement === 'function');
var element = fabric.util.createCanvasElement();
ok(element.getContext);
});
test('createImage', function() {
ok(typeof fabric.util.createImage == 'function');
ok(typeof fabric.util.createImage === 'function');
var element = fabric.util.createImage();
equal(element.naturalHeight, 0);
equal(element.naturalWidth, 0);
});
// test('createAccessors', function() {
// ok(typeof fabric.util.createAccessors == 'function');
// });
test('qrDecompose', function() {
test('qrDecompose with identity matrix', function() {
ok(typeof fabric.util.qrDecompose == 'function');
var options = fabric.util.qrDecompose(fabric.iMatrix);
equal(options.scaleX, 1, 'imatrix has scale 1');
equal(options.scaleY, 1, 'imatrix has scale 1');
equal(options.skewX, 0, 'imatrix has skewX 0');
equal(options.skewY, 0, 'imatrix has skewY 0');
equal(options.angle, 0, 'imatrix has angle 0');
equal(options.translateX, 0, 'imatrix has translateX 0');
equal(options.translateY, 0, 'imatrix has translateY 0');
});
test('qrDecompose with matrix', function() {
ok(typeof fabric.util.qrDecompose == 'function');
var options = fabric.util.qrDecompose([2, 0.4, 0.5, 3, 100, 200]);
equal(Math.round(options.scaleX, 4), 2, 'imatrix has scale');
equal(Math.round(options.scaleY, 4), 3, 'imatrix has scale');
equal(Math.round(options.skewX, 4), 28, 'imatrix has skewX');
equal(options.skewY, 0, 'imatrix has skewY 0');
equal(Math.round(options.angle, 4), 11, 'imatrix has angle 0');
equal(options.translateX, 100, 'imatrix has translateX 100');
equal(options.translateY, 200, 'imatrix has translateY 200');
});
test('drawArc', function() {