Okay, so I've been creating a UI framework and I added animations to it. I wanted to allow users to control the timing functions of their animations using cubicBeziers, so I decided to use p5.js' bezierPoint()
function. I spent hours making it work, and finally got it working in a way that I thought was perfect..... Until now I find out that it's (somehow) not working as expected.
When I pass in a cubic bezier with start/end points at 0,1
and 1,0
and control points at 0.5,0.5
and 0.5,0.5
(this is a linear cubic bezier), I get an eased animation for no reason... This should be yielding a linear animation (which can be demonstrated here) but somehow it's not.
So I looked into it further just to prove that I wasn't just seeing things, and sure enough, it is in fact giving non-linear numbers. I wrote this tiny code snippet:
<script src = "https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.min.js"></script>
<script>
function setup(){
createCanvas(100,100);
}
function draw(){
background(255,255,0);
noFill();
stroke(0,0,0);
strokeWeight(3);
bezier(0,100,50,50,50,50,100,0);
ellipse(bezierPoint(0,50,50,100,frameCount / 100),bezierPoint(100,50,50,0,frameCount / 100),5,5);
ellipse(50,bezierPoint(100,50,50,0,frameCount / 100),5,5);
ellipse(60,100 - frameCount,5,5);
}
</script>
And you can see very clearly that the dot following bezierPoint()
(the one on the left) is moving at a non-linear speed when compared to the linear one (the one on the right)
Why is this happening?
Just thought I'd include this here for convenience. This is p5.js' bezierPoint()
function:
function bezierPoint(e,t,r,i,n){
var a = 1 - n;
return(Math.pow(a,3) * e + 3 * Math.pow(a,2) * n * t + 3 * a * Math.pow(n,2) * r + Math.pow(n,3) * i);
}