Theme:

Vector2

A 2D vector with x, y components.

Constructor

const v = new Vector2(x, y) // defaults to (0, 0)

Properties

  • x, y — Component values
  • width — Alias for x
  • height — Alias for y
  • isVector2 — Type flag (true)

Methods

Setting

  • set(x, y), setScalar(s), setX(x), setY(y)
  • setComponent(index, value) — Set by index (0=x, 1=y)
  • copy(v), fromArray(array, offset?), fromBufferAttribute(attribute, index)

Arithmetic

  • add(v), addScalar(s), addVectors(a, b), addScaledVector(v, s)
  • sub(v), subScalar(s), subVectors(a, b)
  • multiply(v), multiplyScalar(s)
  • divide(v), divideScalar(s)
  • negate()

Transforms

  • applyMatrix3(m) — Multiply by 3x3 matrix (implicit z=1)
  • rotateAround(center, angle) — Rotate around a point

Geometry

  • dot(v) — Dot product
  • cross(v) — 2D cross product (returns scalar)
  • angle() — Angle relative to positive x-axis
  • angleTo(v) — Angle to another vector

Length & Distance

  • length(), lengthSq(), manhattanLength()
  • distanceTo(v), distanceToSquared(v), manhattanDistanceTo(v)
  • normalize(), setLength(length)

Clamping & Rounding

  • min(v), max(v), clamp(min, max), clampScalar(min, max), clampLength(min, max)
  • floor(), ceil(), round(), roundToZero()

Other

  • lerp(v, alpha), lerpVectors(v1, v2, alpha)
  • equals(v), clone()
  • toArray(array?, offset?), getComponent(index)
  • random() — Random components in [0, 1)