Created
October 19, 2011 15:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Removed the square root to speed up the loop.