Translate / 移動(Translate)

translate() 関数を使用すると、オブジェクトをウィンドウ内の任意の場所に移動できます。最初のパラメータはX軸のオフセットを設定し、2番目のパラメータはY軸のオフセットを設定します。

translate coordinate system
Transform Math

View Source Code

/*
 * @name Translate
 * @arialabel Two squares one white one black travel horizontally across a grey background. The black square moves faster than the white
 * @description The translate() function allows objects to be
 * moved to any location within the window. The first parameter
 * sets the x-axis offset and the second parameter sets the
 * y-axis offset. This example shows how transforms accumulate.
 */

let x = 0;
let y = 0;
let dim = 80.0;

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

function draw() {
  background(102);
  // Animate by increasing our x value
  x = x + 0.8;
  // If the shape goes off the canvas, reset the position
  if (x > width + dim) {
    x = -dim;
  }

  // Even though our rect command draws the shape with its
  // center at the origin, translate moves it to the new
  // x and y position
  translate(x, height / 2 - dim / 2);
  fill(255);
  rect(-dim / 2, -dim / 2, dim, dim);

  // Transforms accumulate. Notice how this rect moves
  // twice as fast as the other, but it has the same
  // parameter for the x-axis value
  translate(x, dim);
  fill(0);
  rect(-dim / 2, -dim / 2, dim, dim);
}

License

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

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