mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-16 17:51:07 +00:00
Fix wrong positioned bounding box of fabric.Polygon and fabric.Polyline objects
- Substract minX and minY from points.x/points.y (_calcDimensions) - Same in fromElement - but only if minX or minY is negative
This commit is contained in:
parent
5e10c0011b
commit
c03c556177
4 changed files with 40 additions and 28 deletions
|
|
@ -31,7 +31,7 @@
|
|||
* Constructor
|
||||
* @param {Array} points Array of points
|
||||
* @param {Object} [options] Options object
|
||||
* @param {Boolean} Whether points offsetting should be skipped
|
||||
* @param {Boolean} [skipOffset] Whether points offsetting should be skipped
|
||||
* @return {fabric.Polygon} thisArg
|
||||
*/
|
||||
initialize: function(points, options, skipOffset) {
|
||||
|
|
@ -43,6 +43,7 @@
|
|||
|
||||
/**
|
||||
* @private
|
||||
* @param {Boolean} [skipOffset] Whether points offsetting should be skipped
|
||||
*/
|
||||
_calcDimensions: function(skipOffset) {
|
||||
|
||||
|
|
@ -60,8 +61,8 @@
|
|||
|
||||
if (skipOffset) return;
|
||||
|
||||
var halfWidth = this.width / 2,
|
||||
halfHeight = this.height / 2;
|
||||
var halfWidth = this.width / 2 + this.minX,
|
||||
halfHeight = this.height / 2 + this.minY;
|
||||
|
||||
// change points to offset polygon into a bounding box
|
||||
this.points.forEach(function(p) {
|
||||
|
|
@ -72,8 +73,8 @@
|
|||
|
||||
/**
|
||||
* Returns object representation of an instance
|
||||
* @param {Array} propertiesToInclude
|
||||
* @return {Object} object representation of an instance
|
||||
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
|
||||
* @return {Object} Object representation of an instance
|
||||
*/
|
||||
toObject: function(propertiesToInclude) {
|
||||
return extend(this.callSuper('toObject', propertiesToInclude), {
|
||||
|
|
@ -115,7 +116,7 @@
|
|||
|
||||
/**
|
||||
* @private
|
||||
* @param ctx {CanvasRenderingContext2D} context to render on
|
||||
* @param {CanvasRenderingContext2D} ctx Context to render on
|
||||
*/
|
||||
_render: function(ctx) {
|
||||
var point;
|
||||
|
|
@ -134,7 +135,7 @@
|
|||
|
||||
/**
|
||||
* @private
|
||||
* @param ctx {CanvasRenderingContext2D} context to render on
|
||||
* @param {CanvasRenderingContext2D} ctx Context to render on
|
||||
*/
|
||||
_renderDashedStroke: function(ctx) {
|
||||
var p1, p2;
|
||||
|
|
@ -169,7 +170,7 @@
|
|||
* @static
|
||||
* @param {SVGElement} element Element to parse
|
||||
* @param {Object} [options] Options object
|
||||
* @return {fabric.Polygon}
|
||||
* @return {fabric.Polygon} Instance of fabric.Polygon
|
||||
*/
|
||||
fabric.Polygon.fromElement = function(element, options) {
|
||||
if (!element) {
|
||||
|
|
@ -178,12 +179,17 @@
|
|||
options || (options = { });
|
||||
|
||||
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
|
||||
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES);
|
||||
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES),
|
||||
minX = min(points, 'x'),
|
||||
minY = min(points, 'y');
|
||||
|
||||
minX = minX < 0 ? minX : 0;
|
||||
minY = minX < 0 ? minY : 0;
|
||||
|
||||
for (var i = 0, len = points.length; i < len; i++) {
|
||||
// normalize coordinates, according to containing box (dimensions of which are passed via `options`)
|
||||
points[i].x -= (options.width / 2) || 0;
|
||||
points[i].y -= (options.height / 2) || 0;
|
||||
points[i].x -= (options.width / 2 + minX) || 0;
|
||||
points[i].y -= (options.height / 2 + minY) || 0;
|
||||
}
|
||||
|
||||
return new fabric.Polygon(points, extend(parsedAttributes, options), true);
|
||||
|
|
@ -192,8 +198,8 @@
|
|||
/**
|
||||
* Returns fabric.Polygon instance from an object representation
|
||||
* @static
|
||||
* @param {Object} object Object to create an instance from
|
||||
* @return {fabric.Polygon}
|
||||
* @param object {Object} object Object to create an instance from
|
||||
* @return {fabric.Polygon} Instance of fabric.Polygon
|
||||
*/
|
||||
fabric.Polygon.fromObject = function(object) {
|
||||
return new fabric.Polygon(object.points, object, true);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
"use strict";
|
||||
|
||||
var fabric = global.fabric || (global.fabric = { }),
|
||||
toFixed = fabric.util.toFixed;
|
||||
toFixed = fabric.util.toFixed,
|
||||
min = fabric.util.array.min;
|
||||
|
||||
if (fabric.Polyline) {
|
||||
fabric.warn('fabric.Polyline is already defined');
|
||||
|
|
@ -26,9 +27,9 @@
|
|||
|
||||
/**
|
||||
* Constructor
|
||||
* @param {Array} points array of points
|
||||
* @param {Array} points Array of points
|
||||
* @param {Object} [options] Options object
|
||||
* @param {Boolean} skipOffset Whether points offsetting should be skipped
|
||||
* @param {Boolean} [skipOffset] Whether points offsetting should be skipped
|
||||
* @return {fabric.Polyline} thisArg
|
||||
*/
|
||||
initialize: function(points, options, skipOffset) {
|
||||
|
|
@ -40,7 +41,7 @@
|
|||
|
||||
/**
|
||||
* @private
|
||||
* @param {Boolean} skipOffset Whether points offsetting should be skipped
|
||||
* @param {Boolean} [skipOffset] Whether points offsetting should be skipped
|
||||
*/
|
||||
_calcDimensions: function(skipOffset) {
|
||||
return fabric.Polygon.prototype._calcDimensions.call(this, skipOffset);
|
||||
|
|
@ -48,8 +49,8 @@
|
|||
|
||||
/**
|
||||
* Returns object representation of an instance
|
||||
* @param {Array} propertiesToInclude
|
||||
* @return {Object} object representation of an instance
|
||||
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
|
||||
* @return {Object} Object representation of an instance
|
||||
*/
|
||||
toObject: function(propertiesToInclude) {
|
||||
return fabric.Polygon.prototype.toObject.call(this, propertiesToInclude);
|
||||
|
|
@ -121,7 +122,7 @@
|
|||
|
||||
/**
|
||||
* Returns complexity of an instance
|
||||
* @return {Number} complexity
|
||||
* @return {Number} complexity of this instance
|
||||
*/
|
||||
complexity: function() {
|
||||
return this.get('points').length;
|
||||
|
|
@ -140,7 +141,7 @@
|
|||
* @static
|
||||
* @param {SVGElement} element Element to parse
|
||||
* @param {Object} [options] Options object
|
||||
* @return {Object} instance of fabric.Polyline
|
||||
* @return {fabric.Polyline} Instance of fabric.Polyline
|
||||
*/
|
||||
fabric.Polyline.fromElement = function(element, options) {
|
||||
if (!element) {
|
||||
|
|
@ -149,12 +150,17 @@
|
|||
options || (options = { });
|
||||
|
||||
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
|
||||
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES);
|
||||
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES),
|
||||
minX = min(points, 'x'),
|
||||
minY = min(points, 'y');
|
||||
|
||||
minX = minX < 0 ? minX : 0;
|
||||
minY = minX < 0 ? minY : 0;
|
||||
|
||||
for (var i = 0, len = points.length; i < len; i++) {
|
||||
// normalize coordinates, according to containing box (dimensions of which are passed via `options`)
|
||||
points[i].x -= (options.width / 2) || 0;
|
||||
points[i].y -= (options.height / 2) || 0;
|
||||
points[i].x -= (options.width / 2 + minX) || 0;
|
||||
points[i].y -= (options.height / 2 + minY) || 0;
|
||||
}
|
||||
|
||||
return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options), true);
|
||||
|
|
@ -163,8 +169,8 @@
|
|||
/**
|
||||
* Returns fabric.Polyline instance from an object representation
|
||||
* @static
|
||||
* @param {Object} [object] Object to create an instance from
|
||||
* @return {fabric.Polyline}
|
||||
* @param object {Object} object Object to create an instance from
|
||||
* @return {fabric.Polyline} Instance of fabric.Polyline
|
||||
*/
|
||||
fabric.Polyline.fromObject = function(object) {
|
||||
var points = object.points;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@
|
|||
ok(polygon instanceof fabric.Object);
|
||||
|
||||
equal(polygon.type, 'polygon');
|
||||
deepEqual([ { x: 5, y: 7 }, { x: 15, y: 17 } ], polygon.get('points'));
|
||||
deepEqual([ { x: -5, y: -5 }, { x: 5, y: 5 } ], polygon.get('points'));
|
||||
});
|
||||
|
||||
test('complexity', function() {
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@
|
|||
ok(polyline instanceof fabric.Object);
|
||||
|
||||
equal(polyline.type, 'polyline');
|
||||
deepEqual(polyline.get('points'), [ { x: 5, y: 7 }, { x: 15, y: 17 } ]);
|
||||
deepEqual(polyline.get('points'), [ { x: -5, y: -5 }, { x: 5, y: 5 } ]);
|
||||
});
|
||||
|
||||
test('complexity', function() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue