Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created October 19, 2011 15:19
Show Gist options
  • Save KrofDrakula/1298605 to your computer and use it in GitHub Desktop.
Save KrofDrakula/1298605 to your computer and use it in GitHub Desktop.
Extends Raphaël 2.0 with set operations to help with manipulating sets
# Returns the closest element in the given set.
# Compares the centers of the elements in the set to determine
# the closest element. Returns the Raphaël element closest
# to the point.
#
# Usage: mySet.closestTo(123,456).attr fill: 'red'
Raphael.st.closestTo = (x, y) ->
el = null
dist = Infinity
@forEach (elm) ->
box = elm.getBBox()
delta = { x : box.x + box.width / 2 - x, y : box.y + box.height / 2 - y }
d = delta.x * delta.x + delta.y * delta.y
if dist > d
el = elm
dist = d
el
# Returns the index number for the element within a set
#
# Usage: idx = mySet.indexOf el
Raphael.st.indexOf = (el) ->
Array.prototype.indexOf.call(this, el)
@KrofDrakula
Copy link
Author

Removed the square root to speed up the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment