herbps10
05-05-2006, 02:34 AM
I am trying to make a simple script that when the html page is clicked, a div tag moves in a straight line to the place where you clicked. I'm doing so with polar coordinates and trigonometry. The problem is that when you click, the layer shoots thousands of pixels of the page in the general direction that was clicked.
I've already got the math to do the same thing in visual basic, so im pretty sure thats not the problem.
About the code,
via debugging I found that all of the code above the while loop is correct, and functions as it should. The problem arises in the while loop. I tried dividing I*stepsize*math.cos(theta) by some number to see if it was a units problem, but when I do, the layer goes to the same place about in the middle of the screen no matter where you click.
Any help would be greatly appreceated. :)
Heres the code:
function moveobj(x,y){
var theta;
var stepsize;
var deltax;
var deltay;
var n = 1;
var pi = 3.14159265357989;
var d;
var i;
var prex = document.getElementById("test").style.left;
var prey= document.getElementById("test").style.top;
var startx = prex.substring(0,prex.length-2);
var starty = prey.substring(0,prex.length-2);
d = Math.sqrt(Math.pow((x-startx),2) + Math.pow((y - starty),2));
deltax = x-startx;
deltay = y-starty;
stepsize=d/n;
if (deltax != 0) {
theta = Math.atan(deltay / deltax);
if (deltax < 0) {theta = theta + pi}
}
else {
if (deltax == 0) {
if (deltay < 0) {theta = -pi}
if (deltay > 0) {theta=pi}
}
}
i = 0;
while (i<=n){
i++;
xi = startx + i *(stepsize*Math.cos(theta));
yi = starty + i * (stepsize*Math.sin(theta));
xi= xi + "px"
yi = yi + "px"
document.getElementById("test").style.left= xi
document.getElementById("test").style.top = yi
}
}
I've already got the math to do the same thing in visual basic, so im pretty sure thats not the problem.
About the code,
via debugging I found that all of the code above the while loop is correct, and functions as it should. The problem arises in the while loop. I tried dividing I*stepsize*math.cos(theta) by some number to see if it was a units problem, but when I do, the layer goes to the same place about in the middle of the screen no matter where you click.
Any help would be greatly appreceated. :)
Heres the code:
function moveobj(x,y){
var theta;
var stepsize;
var deltax;
var deltay;
var n = 1;
var pi = 3.14159265357989;
var d;
var i;
var prex = document.getElementById("test").style.left;
var prey= document.getElementById("test").style.top;
var startx = prex.substring(0,prex.length-2);
var starty = prey.substring(0,prex.length-2);
d = Math.sqrt(Math.pow((x-startx),2) + Math.pow((y - starty),2));
deltax = x-startx;
deltay = y-starty;
stepsize=d/n;
if (deltax != 0) {
theta = Math.atan(deltay / deltax);
if (deltax < 0) {theta = theta + pi}
}
else {
if (deltax == 0) {
if (deltay < 0) {theta = -pi}
if (deltay > 0) {theta=pi}
}
}
i = 0;
while (i<=n){
i++;
xi = startx + i *(stepsize*Math.cos(theta));
yi = starty + i * (stepsize*Math.sin(theta));
xi= xi + "px"
yi = yi + "px"
document.getElementById("test").style.left= xi
document.getElementById("test").style.top = yi
}
}