Earlier, I was trying to find out how to rotate a line around a pivot using p5.js, and I played with the code a little bit to make the line point towards the mouse using the atan2
function. It worked fine, and I decided to see what it would look like if I used linear interpolation (lerp
) to make the animation look smoother. What was weird is that it seemed once the line passed a certain point, it jumped to the other side instead of just moving to that point.
以下是我遇到问题的代码:
let angle = 0;
let rot = 0;
function setup() {
createCanvas(600, 300);
}
function draw() {
let v1 = createVector(width / 2 - 50, height / 2);
let v2 = createVector(width / 2 + 50, height / 2);
background(255);
stroke(0);
strokeWeight(4);
rot = lerp(rot, atan2(mouseY - v1.y, mouseX - v1.x), 0.1);
push();
translate(v1.x, v1.y);
rotate(rot);
translate(-v1.x, -v1.y);
let r0 = line(v1.x, v1.y, v2.x, v2.y);
strokeWeight(10);
let p1 = point(v1.x, v1.y);
let p2 = point(v2.x, v2.y);
pop();
}
我如何才能使这个动画看起来流畅,而不是奇怪的 skip ?