I am new to JavaScript and wanted to make a simple script in which an object (a square in this case) moves towards the cursor. The idea is that the square traces the cursor, slowing down as it gets closer to it. So far, my code looks like this:
var xspeed;
var yspeed;
var x;
var y;
function setup() {
createCanvas(500,500);
}
function update(){
xspeed = (mouseX - x)/2;
yspeed = (mouseY - y)/2;
x += xspeed;
y += yspeed;
}
function draw() {
background(255);
x = width/2;
y = height/2;
while (!(x == mouseX && y == mouseY)){
update();
rect(x,y,10,10);
}
}
The problem is, that this code just pops up a load of squares that are static and are placed in a diagonal line from the centre to the left upper corner. It seems the code completely ignores the location of the cursor.
What am I doing wrong? Thanks in advance!