xxxxxxxxxx
34
let x, y;
let xspeed = 1;
function setup() {
createCanvas(400, 400);
x = 10;
y = 50;
}
function draw() {
background(220);
let speed = map(mouseX, 0, width, 0, 0.5);
let slope = map(mouseY, 0, height, 0, 2);
// Calculate the ratio relationship between
// xspeed and yspeed
let xspeed = 1;
let yspeed = xspeed*slope;
// Calculate the new speed
let new_speed = sqrt(sq(xspeed) + sq(yspeed))
// Use the pythagorean theorem to calculate
// xspeed and yspeed for a diagonal speed of 1 pixel
xspeed /= new_speed;
yspeed /= new_speed;
// Multiple the speed to scale it out
x += xspeed*speed;
y += yspeed*speed;
line(0, 0, x, y);
}