To drag the node, utilize Jquery.UI draggable and include a drag event that returns false if the point falls outside the triangle's bounds (refer to: for the algorithm). It is essential to ascertain the coordinates of the triangle's vertices (http://jsfiddle.net/dLzWr/) .
//retrieve the top point of the triangle
function getTopPoint(t){
var pos = t.offset();
//include border dimensions in the offset calculation
var leftBorder = parseInt(t.css("border-left-width"),10);
var topBorder = parseInt(t.css("border-top-width"),10);
pos.left += leftBorder;
pos.top += topBorder;
return pos;
}
//fetch the bottom left point of the triangle
function bottomLeftPoint(t){
var top = getTopPoint(t);
top.top += parseInt(t.css("border-bottom-width"),10);
top.left -= parseInt(t.css("border-left-width"),10);
return top;
}
//obtain the bottom right point of the triangle
function bottomRightPoint(t){
var top = getTopPoint(t);
top.top += parseInt(t.css("border-bottom-width"),10);
top.left += parseInt(t.css("border-right-width"),10);
return top;
}
In examining the fiddle, you can calculate the distance between each vertex and the current point during the draw event, using this information to determine the values.