Search code examples
javascriptpaperjs

How do I get the center of a circle in paper.js?


I am trying out paper.js and I've made a few circles:

var circle = new Shape.Circle({
   center: [100,100],
   radius: 50,
   strokeColor: 'black',
   strokeWidth: 2
});

This draws a circle with center [100,100] (which could also be a point: Point({x:100, y:100});) and radius 50. This is great, this is a circle.

If I want to get the radius of a circle, I can do this:

circle.radius; // returns 50

But I don't know how to get the center back again. Part of the reason I guess is that Shape.Circle returns a Shape object, which does not have a center parameter (documentation here), but surely I can get this point back somehow. Does anyone know how?


Solution

  • Since circles are centred on the position they're created at, you can get the position (as a Point) and the x and y values from that:

    var circle = new Shape.Circle({
        center: [100,100],
        radius: 50,
        strokeColor: 'black',
        strokeWidth: 2
    });
    
    console.log(circle.position.x, circle.position.y); //100 100
    

    From http://paperjs.org/reference/shape/#position