javascript functions
url "vrmlscript:
function initialize() {
setWindowRect(Browser.windowAspect);
pos = orgPos;
correctPosition();
}
function aspectRatio(value,time) {
setWindowRect(value);
correctPosition();
}
function setWindowRect(aspect) {
if(aspect>1) {
// if aspect>1, Y is constant, so X is relative to AspectRatio
node = Browser.boundViewpoint;
//print (' 1: ratio == ' + aspect);
//yfovx = node.fieldOfView;
//print (' 1: yfovx == ' + yfovx);
if (yfov==0) { yfov = 0.785398; }
//xfov = Math.atan(aspect*(Math.tan(yfov/2)))*2;
right = (aspect*(Math.tan(yfov/2))) * 2.0 + 0.1;
top = 0.95;
} else {
// if aspect<1, X is constant, so Y is relative to AspectRatio
//xfov = Browser.boundViewpoint.fieldOfView;
if(xfov==0) { xfov = 0.785398; }
//yfov = Math.atan((Math.tan(yfov/2))/aspect)*2;
right = 0.95;
top = ((Math.tan(xfov/2))/aspect) * 2.0 + 0.1;
}
//print ('ratio == ' + aspect);
//print ('yfov == ' + yfov);
//print ('xfov == ' + xfov);
//print ('angleRatio == ' + angleRatio);
left = -right;
bottom = -top;
}
function setRotateTrans (value,time) {
rotA = new SFRotation(0,1,0, value.x*10);
rotB = new SFRotation(1,0,0,-value.y*10);
rotation_changed = rotA.multiply(rotB);
}
function doDrag(value,time) {
if(!value) {
orgPos = pos;
} else {
pos = orgPos;
correctionRot = orgRot;
}
}
function correctPosition() {
// Snap position to edge of screen
dx = 0;
dy = 0;
if(pos.x > xCenter) { dx = right-pos.x; }
else { dx = pos.x-left; }
if(pos.y < yCenter) { dy = pos.y-bottom; }
else { dy = top-pos.y; }
if(dx<dy) {
if(pos.x > xCenter) {
armRotation = new SFRotation(0,0,1,0);
correctionRotation = new SFRotation(0,0,1,0);
orgRot = new SFRotation(0,0,1,0);
pos.x = right;
} else {
armRotation = new SFRotation(0,0,1, 3.14);
correctionRotation = new SFRotation(0,0,1,-3.14);
orgRot = new SFRotation(0,0,1, 3.14);
pos.x = left;
}
} else /* dy<dx */ {
if(pos.y < yCenter) {
armRotation = new SFRotation(0,0,1,-1.57);
correctionRotation = new SFRotation(0,0,1, 1.57);
orgRot = new SFRotation(0,0,1,-1.57);
pos.y = bottom;
} else {
armRotation = new SFRotation(0,0,1, 1.57);
correctionRotation = new SFRotation(0,0,1,-1.57);
orgRot = new SFRotation(0,0,1, 1.57);
pos.y = top;
}
}
// Limit position to screen
if(pos.y < bottom) { pos.y = bottom; }
if(pos.y > top) { pos.y = top; }
if(pos.x < left) { pos.x = left; }
if(pos.x > right) { pos.x = right; }
newPos = pos;
}
function setDragTrans (value,time) {
rotVal = correctionRot.multVec(value);
pos = new SFVec3f();
pos.x = orgPos.x + rotVal.x;
pos.y = orgPos.y + rotVal.y;
pos.z = orgPos.z;
correctPosition();
}
"
}