constrain

概要

p5.jsリファレンス(参考情報)

[p5.js] constrain

サンプル

def draw
  background(200)

  leftWall  = 25
  rightWall = 75

  # xm is just the mouseX, while
  # xc is the mouseX, but constrained
  # between the leftWall and rightWall!
  xm = mouseX
  xc = constrain(mouseX, leftWall, rightWall)

  # Draw the walls.
  stroke(150)
  line(leftWall,  0, leftWall,  height)
  line(rightWall, 0, rightWall, height)

  # Draw xm and xc as circles.
  noStroke
  fill(150)
  ellipse(xm, 33, 9, 9)   # Not Constrained
  fill(0)
  ellipse(xc, 66, 9, 9)   # Constrained
end

構文

戻値