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

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

[p5.js] constrain

概要

値の取りうる範囲を下限値と上限値で制限します。

書式

constrain(n, low, high)

引数

引数名内容備考オプションデフォルト値
n
low下限値
high上限値

戻値

制限された値

備考

関連