Tintin
Tintin

Reputation: 2973

find the x and y co-ordinates of the center and middle point inside a div

I am using svg and d3 and I need to draw a circle right at the center and middle of a div. How do I calculate the coordinates of the same?

Thanks

Upvotes: 0

Views: 1457

Answers (1)

You can get center of div like this:

var $this = $(this); // targeted div
var offset = $this.offset();
var width = $this.width();
var height = $this.height();

var centerX = offset.left + width / 2;
var centerY = offset.top + height / 2;

Where centerX and centerY are the coordinates of center.

Upvotes: 3

Related Questions