Skip to main content

$clamp

The $clamp operator constrains a value within minimum and maximum bounds.

Input

  • An array containing exactly three numeric values: the value to clamp, the minimum bound, and the maximum bound

Output

  • The clamped value within the specified bounds

Examples

Clamp Value Within Range

{
  "$clamp": [
    15,
    0,
    10
  ]
}
Result: 10 (since 15 is above the max of 10, it gets clamped to 10)

Clamp Value Below Range

{
  "$clamp": [
    -5,
    0,
    10
  ]
}
Result: 0 (since -5 is below the min of 0, it gets clamped to 0)

Value Within Range

{
  "$clamp": [
    5,
    0,
    10
  ]
}
Result: 5 (since 5 is within the range, it remains unchanged)

From Fields

{
  "$clamp": [
    {
      "$input": "value"
    },
    {
      "$input": "min"
    },
    {
      "$input": "max"
    }
  ]
}
Result: The value field clamped between the min and max field values