Selecting locations and objects

To determine the world coordinates of the location selected by the user we can use the following function:

var pxy = sheetengine.transforms.inverseTransformPoint(pointuv);
Parameters are:
  • pointuv: point in camera coordinates. JSON with u,v keys.
This can be combined with a click event to determine world coordinates the following way:

canvasElement.onclick = function(event) {
  var puv = {
    u:event.clientX - sheetengine.canvas.offsetLeft + pageXOffset,
    v:event.clientY - sheetengine.canvas.offsetTop + pageYOffset
  };
  var pxy = sheetengine.transforms.inverseTransformPoint({
    u:puv.u + sheetengine.scene.center.u, 
    v:puv.v + sheetengine.scene.center.v
  });
}

To determine if an object is selected in the scene simply use the getPointuv function to determine the object's u-v coordinates and compare it to the u-v coordinates of the mouse:

canvasElement.onmousemove = function(event) {
  // get the hover coordinates
  var puv = {
    u:event.clientX - sheetengine.canvas.offsetLeft + pageXOffset,
    v:event.clientY - sheetengine.canvas.offsetTop + pageYOffset
  };
			  
  // check if the object has been hovered
  var objhovered = isObjectHovered(puv);
}

function isObjectHovered(puv) {
  var ouv = sheetengine.drawing.getPointuv(obj.centerp);
  if (puv.u > ouv.u - 20 &&
    puv.u < ouv.u - 20 + 40 &&
    puv.v > ouv.v - 30 &&
    puv.v < ouv.v - 30 + 40)
    return true;
			
  return false;
}

Last edited Dec 26, 2012 at 1:48 AM by dobsonl, version 2

Comments

No comments yet.