I'm trying to simulate gravitational pull/acceleration in the following animation. earth
's angular velocity should increase as it gets closer to sol
and decrease as it gets far. I think I'll need an easing function to modify the earth.angularVelocity
but no idea how.
I don't know if already defined easing functions or a custom one would work. The easing function I need should work like in this graph:
earth
's perihelion is at 180°, and aphelion is at 0/360°. How can I create such a function and make this work?
function pullRelease(angularPosition, begin, change, maxVelocity) {
// ?
}
earth.angularVelocity = pullRelease(earth.angularPosition, 0, 360, 3);
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var sol = {
x: 125,
y: 150,
r: 30,
fill: "gold",
};
var orbit = {
x: 200,
y: 150,
semiMajor: 150,
semiMinor: 75,
};
var earth = {
r: 15,
fill: "dodgerblue",
angularPosition: 0,
angularVelocity: 1,
};
// draw sun
context.beginPath();
context.arc(sol.x, sol.y, sol.r, 0, 360 * Math.PI / 180);
context.fillStyle = sol.fill;
context.fill();
// draw fake sun to mark the orbit center
context.beginPath();
context.arc(orbit.x, orbit.y, sol.r, 0, 360 * Math.PI / 180);
context.fillStyle = "rgba(255,215,0,.1)";
context.fill();
// draw earth's orbit path
context.beginPath();
context.ellipse(orbit.x, orbit.y, orbit.semiMajor, orbit.semiMinor, 0, 0, 2 * Math.PI);
context.stroke();
// these are fixed, so save them as background-image
canvas.style.backgroundImage = "url(" + canvas.toDataURL() + ")";
function draw() {
context.resetTransform();
context.clearRect(0, 0, canvas.width, canvas.height);
var newPosition = rotate(-earth.angularPosition, orbit.semiMajor, orbit.semiMinor, orbit.x, orbit.y);
earth.x = newPosition.x;
earth.y = newPosition.y;
// earth.angularVelocity = pullRelease(earth.angularPosition, 0, 360, 3);
earth.angularPosition += earth.angularVelocity;
if (earth.angularPosition >= 360) {
earth.angularPosition = 0;
}
position.innerHTML = earth.angularPosition + "°";
context.translate(earth.x, earth.y);
context.beginPath();
context.arc(0, 0, earth.r, 0, 360 * Math.PI / 180);
context.closePath();
context.fillStyle = earth.fill;
context.fill();
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
function rotate(angle, distanceX, distanceY, originX, originY) {
return {
x: originX + Math.cos(angle * Math.PI / 180) * distanceX,
y: originY + Math.sin(angle * Math.PI / 180) * distanceY,
}
}
body {
background: gainsboro;
}
canvas {
background: white;
box-shadow: 1px 1px 1px rgba(0, 0, 0, .1);
}
#position {
display: inline-block;
width: 35px;
text-align: right;
}
<canvas id="canvas" class="box" width="400" height="300"></canvas>
<p>Position: <span id="position">0</span></p>