Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Start and End angle to circle. #1675

Merged
merged 4 commits into from
Sep 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions src/shapes/circle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

'use strict';

var fabric = global.fabric || (global.fabric = { }),
piBy2 = Math.PI * 2,
var fabric = global.fabric || (global.fabric = { }),
pi = Math.PI,
extend = fabric.util.object.extend;

if (fabric.Circle) {
Expand Down Expand Up @@ -33,6 +33,20 @@
*/
radius: 0,

/**
* Start angle of the circle, moving clockwise
* @type Number
* @default 0
*/
startAngle: 0,

/**
* End angle of the circle
* @type Number
* @default 2Pi
*/
endAngle: pi * 2,

/**
* Constructor
* @param {Object} [options] Options object
Expand All @@ -43,6 +57,8 @@

this.callSuper('initialize', options);
this.set('radius', options.radius || 0);
this.startAngle = options.startAngle || this.startAngle;
this.endAngle = options.endAngle || this.endAngle;
},

/**
Expand All @@ -68,7 +84,9 @@
*/
toObject: function(propertiesToInclude) {
return extend(this.callSuper('toObject', propertiesToInclude), {
radius: this.get('radius')
radius: this.get('radius'),
startAngle: this.startAngle,
endAngle: this.endAngle
});
},

Expand All @@ -79,20 +97,41 @@
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = this._createBaseSVGMarkup(), x = 0, y = 0;
if (this.group) {
x = this.left + this.radius;
y = this.top + this.radius;
var markup = this._createBaseSVGMarkup(), x = 0, y = 0,
angle = (this.endAngle - this.startAngle) % ( 2 * pi);

if (angle === 0) {
if (this.group && this.group.type === 'path-group') {
x = this.left + this.radius;
y = this.top + this.radius;
}
markup.push(
'<circle ',
'cx="' + x + '" cy="' + y + '" ',
'r="', this.radius,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
'"/>\n'
);
}
markup.push(
'<circle ',
'cx="' + x + '" cy="' + y + '" ',
'r="', this.radius,
else {
var startX = Math.cos(this.startAngle) * this.radius,
startY = Math.sin(this.startAngle) * this.radius,
endX = Math.cos(this.endAngle) * this.radius,
endY = Math.sin(this.endAngle) * this.radius,
largeFlag = angle > pi ? '1' : '0';

markup.push(
'<path d="M ' + startX + ' ' + startY,
' A ' + this.radius + ' ' + this.radius,
' 0 ', + largeFlag + ' 1', ' ' + endX + ' ' + endY,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
'"/>\n'
);
'"/>\n'
);
}

return reviver ? reviver(markup.join('')) : markup.join('');
},
Expand All @@ -105,7 +144,7 @@
*/
_render: function(ctx, noTransform) {
ctx.beginPath();
ctx.arc(noTransform ? this.left + this.radius : 0, noTransform ? this.top + this.radius : 0, this.radius, 0, piBy2, false);
ctx.arc(noTransform ? this.left + this.radius : 0, noTransform ? this.top + this.radius : 0, this.radius, this.startAngle, this.endAngle, false);
this._renderFill(ctx);
this._renderStroke(ctx);
},
Expand Down
4 changes: 3 additions & 1 deletion test/unit/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
'visible': true,
'backgroundColor': '',
'clipTo': null,
'radius': 0
'radius': 0,
'startAngle': 0,
'endAngle': 2 * Math.PI
};
ok(typeof circle.toObject == 'function');
deepEqual(circle.toObject(), defaultProperties);
Expand Down