The normal way is to use the pythagoras theorem of a²+b²=c² where each ball is checked against every other ball. if the distance is smaller than the sum of their radii, you know that they have hit each other.

That code’s a little more complex because they’re using squares rather than circles.

With circles, it’s pretty simple: Take the centerpoint of the circle (which is where SVG circles would be easier, because they define themselves as a centerpoint and radius, but the ones in your first fiddle can be found fairly simply too as x+width/2, y+height/2). Compare that to every other circle’s current centerpoint via a^2+b^2=c^2 (IE: dx^2+dy^2 = d^2, dx = x2-x1, dy = y2-y1, solve for d). If d <= radius of the circle * 2, they’ve collided.