diff --git a/src/gradient.class.js b/src/gradient.class.js index 200fced00cc..62f53d1523f 100644 --- a/src/gradient.class.js +++ b/src/gradient.class.js @@ -413,7 +413,7 @@ options[prop] = 0; } propValue = parseFloat(options[prop], 10); - if (typeof options[prop] === 'string' && /^\d+%$/.test(options[prop])) { + if (typeof options[prop] === 'string' && /^(\d+\.\d+)%|(\d+)%$/.test(options[prop])) { multFactor = 0.01; } else { diff --git a/src/shapes/circle.class.js b/src/shapes/circle.class.js index 2b5134400da..64515b5a044 100644 --- a/src/shapes/circle.class.js +++ b/src/shapes/circle.class.js @@ -3,8 +3,7 @@ 'use strict'; var fabric = global.fabric || (global.fabric = { }), - pi = Math.PI, - extend = fabric.util.object.extend; + pi = Math.PI; if (fabric.Circle) { fabric.warn('fabric.Circle is already defined.'); @@ -174,13 +173,11 @@ * @static * @memberOf fabric.Circle * @param {SVGElement} element Element to parse - * @param {Object} [options] Options object * @param {Function} [callback] Options callback invoked after parsing is finished + * @param {Object} [options] Options object * @throws {Error} If value of `r` attribute is missing or invalid */ - fabric.Circle.fromElement = function(element, callback, options) { - options || (options = { }); - + fabric.Circle.fromElement = function(element, callback) { var parsedAttributes = fabric.parseAttributes(element, fabric.Circle.ATTRIBUTE_NAMES); if (!isValidRadius(parsedAttributes)) { @@ -189,7 +186,7 @@ parsedAttributes.left = (parsedAttributes.left || 0) - parsedAttributes.radius; parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.radius; - callback(new fabric.Circle(extend(parsedAttributes, options))); + callback(new fabric.Circle(parsedAttributes)); }; /** diff --git a/src/shapes/ellipse.class.js b/src/shapes/ellipse.class.js index 5400d7a95b7..7884520959c 100644 --- a/src/shapes/ellipse.class.js +++ b/src/shapes/ellipse.class.js @@ -3,8 +3,7 @@ 'use strict'; var fabric = global.fabric || (global.fabric = { }), - piBy2 = Math.PI * 2, - extend = fabric.util.object.extend; + piBy2 = Math.PI * 2; if (fabric.Ellipse) { fabric.warn('fabric.Ellipse is already defined.'); @@ -161,18 +160,16 @@ * @static * @memberOf fabric.Ellipse * @param {SVGElement} element Element to parse - * @param {Object} [options] Options object * @param {Function} [callback] Options callback invoked after parsing is finished * @return {fabric.Ellipse} */ - fabric.Ellipse.fromElement = function(element, callback, options) { - options || (options = { }); + fabric.Ellipse.fromElement = function(element, callback) { var parsedAttributes = fabric.parseAttributes(element, fabric.Ellipse.ATTRIBUTE_NAMES); parsedAttributes.left = (parsedAttributes.left || 0) - parsedAttributes.rx; parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.ry; - callback(new fabric.Ellipse(extend(parsedAttributes, options))); + callback(new fabric.Ellipse(parsedAttributes)); }; /* _FROM_SVG_END_ */ diff --git a/test.js b/test.js index 3c423119b94..f310c58a91c 100644 --- a/test.js +++ b/test.js @@ -47,7 +47,7 @@ testrunner.run({ './test/unit/intersection.js', './test/unit/stateful.js' ], - tests: ['./test/unit/pattern.js'], + // tests: ['./test/unit/pattern.js'], }, function(err, report) { if (err) { console.log(err); diff --git a/test/unit/gradient.js b/test/unit/gradient.js index 2af46b9b40e..2559438b1a2 100644 --- a/test/unit/gradient.js +++ b/test/unit/gradient.js @@ -77,6 +77,7 @@ var SVG_INTERNALRADIUS = '\n\n\n\n'; var SVG_SWAPPED = '\n\n\n\n'; + QUnit.test('constructor linearGradient', function(assert) { assert.ok(fabric.Gradient); @@ -226,6 +227,72 @@ assert.equal(gradient.colorStops[0].opacity, 0); }); + QUnit.test('fromElement linearGradient with floats percentage - objectBoundingBox', function(assert) { + assert.ok(typeof fabric.Gradient.fromElement === 'function'); + + var element = fabric.document.createElement('linearGradient'); + element.setAttribute('gradientUnits', 'objectBoundingBox'); + element.setAttribute('x1', '10%'); + element.setAttribute('y1', '0.2%'); + element.setAttribute('x2', '200'); + element.setAttribute('y2', '20%'); + var stop1 = fabric.document.createElement('stop'); + var stop2 = fabric.document.createElement('stop'); + + stop1.setAttribute('offset', '0%'); + stop1.setAttribute('stop-color', 'white'); + + stop2.setAttribute('offset', '100%'); + stop2.setAttribute('stop-color', 'black'); + stop2.setAttribute('stop-opacity', '0'); + + element.appendChild(stop1); + element.appendChild(stop2); + + var object = new fabric.Object({ width: 200, height: 200 }); + var gradient = fabric.Gradient.fromElement(element, object); + + assert.ok(gradient instanceof fabric.Gradient); + + assert.equal(gradient.coords.x1, 20); + assert.equal(gradient.coords.y1, 0.4); + assert.equal(gradient.coords.x2, 40000); + assert.equal(gradient.coords.y2, 40); + }); + + QUnit.test('fromElement linearGradient with floats percentage - userSpaceOnUse', function(assert) { + assert.ok(typeof fabric.Gradient.fromElement === 'function'); + + var element = fabric.document.createElement('linearGradient'); + element.setAttribute('gradientUnits', 'userSpaceOnUse'); + element.setAttribute('x1', '10%'); + element.setAttribute('y1', '0.2%'); + element.setAttribute('x2', '200'); + element.setAttribute('y2', '20%'); + var stop1 = fabric.document.createElement('stop'); + var stop2 = fabric.document.createElement('stop'); + + stop1.setAttribute('offset', '0%'); + stop1.setAttribute('stop-color', 'white'); + + stop2.setAttribute('offset', '100%'); + stop2.setAttribute('stop-color', 'black'); + stop2.setAttribute('stop-opacity', '0'); + + element.appendChild(stop1); + element.appendChild(stop2); + + var object = new fabric.Object({ width: 200, height: 200 }); + var gradient = fabric.Gradient.fromElement(element, object); + + assert.ok(gradient instanceof fabric.Gradient); + + assert.equal(gradient.coords.x1, 0.1); + assert.equal(gradient.coords.y1, 0.002); + assert.equal(gradient.coords.x2, 200); + assert.equal(gradient.coords.y2, 0.2); + }); + QUnit.test('fromElement linearGradient with Infinity', function(assert) { assert.ok(typeof fabric.Gradient.fromElement === 'function');