Gleam - v1.0.2
    Preparing search index...

    Class Vec2

    2D vector. Scalar args to set/add/sub/mult/div/rem/mod/equals broadcast to both axes: vec.add(5) adds 5 to x and y, vec.mult(-1) negates both. Pass (x, y) or a Vector2 for per-axis values.

    Index

    Constructors

    Properties

    x: number = 0

    Horizontal component.

    y: number = 0

    Vertical component.

    Methods

    • Set each component to its absolute value. Mutates and returns this.

      Returns Vec2

    • Angle in radians. No arg: angle of this from origin. With other: angle from this toward other.

      Parameters

      Returns number

    • Clamp each axis to its [min, max] range. y defaults to x. Mutates and returns this.

      Parameters

      • x: [number, number]
      • y: [number, number] = x

      Returns Vec2

    • Approximate equality (within approxEqual tolerance). Scalar broadcasts.

      Parameters

      Returns boolean

    • Approximate equality (within approxEqual tolerance). Scalar broadcasts.

      Parameters

      • x: number
      • Optionaly: number

      Returns boolean

    • true when both components are finite (rules out NaN and ±Infinity).

      Returns boolean

    • Euclidean magnitude (sqrt(x² + y²)).

      Returns number

    • Manhattan magnitude (|x| + |y|).

      Returns number

    • Apply callback to each component (index is 0 for x, 1 for y). Mutates and returns this.

      Parameters

      • callback: (value: number, index: number) => number

      Returns Vec2

      new Vec2(3.6, -2.1).map(Math.trunc);   // Vec2 { x: 3, y: -2 }
      new Vec2(2, 5).map((v, i) => v * (i + 1)); // Vec2 { x: 2, y: 10 }
    • Larger of the two components.

      Returns number

    • Smaller of the two components.

      Returns number

    • Per-axis Euclidean modulo (result sign matches the divisor). Mutates and returns this.

      Parameters

      Returns Vec2

    • Per-axis Euclidean modulo (result sign matches the divisor). Mutates and returns this.

      Parameters

      • x: number
      • Optionaly: number

      Returns Vec2

    • Flip the sign of both components (same as mult(-1)). Mutates and returns this.

      Returns Vec2

    • Scale to unit length. Zero-length vectors are left untouched and warn (throttled). Mutates and returns this.

      Returns Vec2

    • Scale so |x| + |y| === 1. Zero-length vectors are left untouched. Mutates and returns this.

      Returns Vec2

    • Per-axis remainder (JavaScript %, sign follows the dividend). Mutates and returns this.

      Parameters

      Returns Vec2

    • Per-axis remainder (JavaScript %, sign follows the dividend). Mutates and returns this.

      Parameters

      • x: number
      • Optionaly: number

      Returns Vec2

    • Round each component to the nearest integer. Mutates and returns this.

      Returns Vec2

    • Tuple [x, y].

      Returns [number, number]

    • Build a Rect with the argument as position and this as size.

      Parameters

      Returns Rect

    • Build a Rect with the argument as position and this as size.

      Parameters

      • x: number
      • Optionaly: number

      Returns Rect

    • Build a Rect with this as position and the argument as size.

      Parameters

      Returns Rect

    • Build a Rect with this as position and the argument as size.

      Parameters

      • x: number
      • Optionaly: number

      Returns Rect

    • Debug string like "Vec2 [x: 1, y: 2]".

      Returns string

    • Unit vector at angle rad (radians), scaled per-axis. scaleY defaults to scaleX.

      Parameters

      • rad: number
      • scaleX: number = 1
      • scaleY: number = scaleX

      Returns Vec2