whichTriangle.js
A small jQuery plugin that helps you divide an element into four triangles and test co-ordinates on them.
It helps you add direction sensitivity to all the elements it is called on.
Here, have a demo.
More demos coming soon.
Installation
- Download either
whichTriangle.jsorwhichTriangle.min.jsfromlib - Add it after you include
jQuery - You're ready to go!
Usage
Setup
$( selector ).setupTriangles( {'midDiameter': 100} )element.setupTriangles() sets up the triangles for the element its called on.
It returns the element so it can be chained.
It accepts one setting via the settings object.
- The
midDiametersettings enables the creation of a circular mid tolerance region.
If the coordinates fall in this region then4is returned and not the usual0-3or-1.midDiameterdefaults to0
Test co-ordinates
$( selector ).whichTriangle( event )element.whichTriangle( event ) is passed one parameter:
eventis the event object that is available to any event handler. Internally, it extracts the client co-ordinates from the event object and returns which triangle those co-ordinates fall in. If they don't fall in any triangle then it returns-1.- It returns
0fortoptriangle - It returns
1forrighttriangle - It returns
2forbottomtriangle - It returns
3forlefttriangle - It returns
4ifmidDiameter > 0and the coordinates are in the mid tolerance region - It returns
-1when co-ordinates outside triangles
Update triangles
$( selector ).updateTriangles()element.updateTriangles() updates the co-ordinates that describe the triangles for the element. This is required when the dimensions and/or position of the element changes. This is also required when the window is resized, etc as the relative coordinates of the triangles change.
With this you can easily build things like direction sensitive hovers, direction sensitive interactions, etc.
See the example in the examples directory for more clarity.
Support
Chrome, Firefox 3.0+, IE6+, Safari 4.0+, Opera 10.0+
Note
.whichTriangle( event )doesn't support chaining as it returns anumbervalue.- To use with IE < 9 use jQuery <= 1.7.0