Easing / イージング

画面上でマウスを動かすと、シンボルが追従します。アニメーションの各フレームを描画する間に、プログラムはシンボルとカーソルの位置の差を計算します。距離が1ピクセルより大きい場合、シンボルはその現在の位置からカーソルに向かって距離の一部(0.05)を移動します。

easing interpolation animation
Input Motion

View Source Code

/*
 * @name Easing
 * @arialabel Pink background with a white circle that the user can move around by hovering over the circle
 * @description Move the mouse across the screen and the symbol
 * will follow. Between drawing each frame of the animation, the
 * program calculates the difference between the position of the
 * symbol and the cursor. If the distance is larger than 1 pixel,
 * the symbol moves part of the distance (0.05) from its current
 * position toward the cursor.
 */
let x = 1;
let y = 1;
let easing = 0.05;

function setup() {
  // createCanvas(720, 400);
  createCanvas(windowWidth, windowHeight);
  noStroke();
}

function draw() {
  background(237, 34, 93);
  let targetX = mouseX;
  let dx = targetX - x;
  x += dx * easing;

  let targetY = mouseY;
  let dy = targetY - y;
  y += dy * easing;

  ellipse(x, y, 66, 66);
}

License

Source code is available on GitHub p5.js website legacy.

All examples are licensed under CC BY-NC-SA 4.0.