I would like to calculate the position of $x to give an acceleration / deceleration effect to a square in this animated gif.
Currently, the only thing I can do is a constant displacement of this square.
How to find the value of $x to give this acceleration / deceleration effect for each animated GIF frame?
$fps = 25;
$initialVelocity = 5;
$finalVelocity = 0;
$duration = 2000;
$distance = 300;
$nbFrames = $duration / 1000 * $fps;
$imFrames = new Imagick();
for ($i = 0; $i < $nbFrames; $i++)
{
$currentTime = $duration / ($nbFrames - 1) * $i;
$x = $distance / $duration * $currentTime; // How i can apply velocity on the $x coordinate ?
$draw = new ImagickDraw();
$draw->setFillColor('#ff0000');
$draw->rectangle(
$x,
10,
$x + 10,
20
);
$imFrame = new Imagick();
$imFrame->newImage(400, 40, '#5b5b5b');
$imFrame->drawImage($draw);
$imFrame->setImageDelay(100 / $fps);
$imFrame->setImageFormat('gif');
$imFrame->setImageDispose(3); // clean previous frame
$imFrames->addImage($imFrame);
}
header('Content-type: image/gif');
$imFrames->setFormat('gif');
echo $imFrames->getImagesBlob();
exit();
EDIT :
I do not happen.
I would like it to move from 0 to x but my square makes back trips.
$fps = 25;
$initialVelocity = 6;
$finalVelocity = 0;
$duration = 2000;
$distance = 100;
$acceleration = ($finalVelocity - $initialVelocity) / $duration;
$nbFrames = $duration / 1000 * $fps;
$imFrames = new imagick();
for ($i = 0; $i < $nbFrames; $i++)
{
$timePerFrame = $duration / ($nbFrames - 1);
$currentTime = $timePerFrame * $i;
$velocity = $initialVelocity + ($acceleration * $currentTime);
$x = $distance / $duration * $currentTime;
$x *= $velocity;
$draw = new ImagickDraw();
$draw->setFillColor('#ff0000');
$draw->rectangle(
$x,
10,
$x + 10,
20
);
$imFrame = new Imagick();
$imFrame->newImage(400, 40, '#5b5b5b');
$imFrame->drawImage($draw);
$imFrame->setImageDelay(100 / $fps);
$imFrame->setImageFormat('gif');
$imFrame->setImageDispose(3); // clean previous
$imFrames->addImage($imFrame);
}
header('Content-type: image/gif');
$imFrames->setFormat('gif');
echo $imFrames->getImagesBlob();
exit();