Get/Set the radius of Path.Circle items

1,875 views
Skip to first unread message

Endash

unread,
Jan 28, 2012, 7:48:30 PM1/28/12
to pap...@googlegroups.com
Hi,

How can the radius of a Path.Circle item be "get" and "set" ?

Thanks,

Christopher Best

unread,
Jan 29, 2012, 6:44:15 PM1/29/12
to pap...@googlegroups.com
Once you create a circle path, it's just a path, and there is no inherent circle structure.
However, the following would be equivalent to getting and setting the radius:

var circlePath = new Path.Circle(center, radius);

// get
var radius = circlePath.bounds.width / 2;

// set
var circlePath.scale(newRadius / radius);

Endash

unread,
Jan 29, 2012, 7:59:47 PM1/29/12
to pap...@googlegroups.com
The problem with the solution you propose is that it gets messed up with the strokeWidth.

For instance:

var circlePath = new Path.Circle(center, 100);

circlePath.strokeWidth = 20;

// get
var radius = circlePath.bounds.width / 2;

>>> radius = 60 = 50 + 10 (because of the strokeWidth);

// set
var scale = 2;
var circlePath.scale(2);


radius = circlePath.bounds.width / 2;

>>> radius = (2*100 + 10 + 10) / 2 = 110, where it would have been more logical to be 120 = 2 * 60.

But by writing the "logical" word, I realize the paperjs team may disagree.

I guess I will have to build my own function to return what I consider the "logical" radius. :-)


Alex Blackwood

unread,
Jan 29, 2012, 8:37:47 PM1/29/12
to Paper.js
Since it's a circle, why not just use the vector length between any
segment and the center?

var circlePath = new Path.Circle(view.center,100);
circlePath.strokeColor='black';
circlePath.strokeWidth = 50;
function radGet(circle){
rad = (circle.firstSegment.point-circle.bounds.center).length;
return rad;
}
var test = new PointText(new Point(view.center));
test.content= radGet(circlePath);
test.paragraphStyle.justification= 'center';

Christopher Best

unread,
Jan 30, 2012, 11:08:37 AM1/30/12
to pap...@googlegroups.com
Hi Endash,

If you want to include stroke in the calculations, Path has a strokeBounds property which will let you find the radius. Setting the radius is slightly more complicated, but not too much. Below are two functions which should get and set the radius the way you want.

var getRadius = function(path) {
return path.bounds.width / 2 + path.strokeWidth / 2;
// or return path.strokeBounds.width / 2; 
}
var setRadius = function(path, radius) {
// figure out what the new radius should be without the stroke
var newRadiusWithoutStroke = radius - path.strokeWidth / 2;
// figure out what the current radius is without the stroke 
var oldRadiusWithoutStroke = path.bounds.width / 2;
path.scale(newRadiusWithoutStroke / oldRadiusWithoutStroke);
}
Reply all
Reply to author
Forward
0 new messages