xxxxxxxxxx
52
// https://discourse.processing.org/t/rotation-based-on-mouse/1766
// https://p5js.org/reference/#/p5/atan2
//'use strict';
let targetAngle = 0.0;
let currentAngle = 0.0;
let x = 0.0;
let y = 0.0;
let smoothSpeed = 0.05;
let scl = 25.0;
const count = 3;
let iToTheta;
function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
x = lerp(x, mouseX, smoothSpeed);
y = lerp(y, mouseY, smoothSpeed);
targetAngle = atan2(mouseY - y, mouseX - x);
currentAngle = lerpAngle(currentAngle, targetAngle, smoothSpeed);
background(0);
push();
const theta = currentAngle;
translate(x, y)
rotate(theta);
rotate(PI/2);
rect(0, 0, 20, 40);
pop();
}
// Linear interpolation of an angle.
function lerpAngle(a, b, step) {
// Prefer shortest distance,
const delta = b - a;
if (delta == 0.0) {
return a;
} else if (delta < -PI) {
b += TWO_PI;
} else if (delta > PI) {
a += TWO_PI;
}
return (1.0 - step) * a + step * b;
}