Skip to content

Coord

const coord = require("@vectarine/coord")

A module for dealing with coordinate systems and switching between them.

coord.px(
v_or_x: Vec.Vec2 | number,
screen_size_px_or_y: number | Vec.Vec2?,
screen_size_px_or_nil: Vec.Vec2?,
): ScreenPosition

Converts a Vec2 in pixels to a ScreenPosition The pixel convention has the origin (0,0) in the upper-left of the screen, with the lower-right at (screen_width-1, screen_height-1) This is the same convention as most other graphics APIs like HTML5 Canvas, Love2d, Processing, Raylib etc…

You can use either coord.px(10, 10) or coord.px(Vec.V2(10, 10)) to make ScreenPosition. To perform the conversion, this function uses the screen size of the current drawing target (screen or canvas) unless screen_size_px is provided.

coord.pxVec(
v_or_x: Vec.Vec2 | number,
screen_size_px_or_y: number | Vec.Vec2?,
screen_size_px_or_nil: Vec.Vec2?,
): ScreenVec

Converts a Vec2 in pixels to a ScreenVec The pixel convention has the origin (0,0) in the upper-left of the screen

You can use either coord.pxVec(10, 10) or coord.pxVec(Vec.V2(10, 10)) to make a ScreenVec.

To perform the conversion, this function uses the screen size of the current drawing target (screen or canvas) unless screen_size_px is provided.

coord.gl(v_or_x: Vec.Vec2 | number, y_or_nil: number | Vec.Vec2?): ScreenPosition

Converts a Vec2 using the OpenGL convention to a ScreenPosition The OpenGL convention has the origin (0,0) in the center of the screen, with the bottom-left at (-1,-1) and the upper-right at (1,1)

coord.glVec(v_or_x: Vec.Vec2 | number, y_or_nil: number | Vec.Vec2?): ScreenVec

Converts a Vec2 using the OpenGL convention to a ScreenVec

coord.vw(
v_or_x: Vec.Vec2 | number,
screen_size_px_or_y: number | Vec.Vec2?,
screen_size_px_or_nil: Vec.Vec2?,
): ScreenPosition
coord.vwVec(
v_or_x: Vec.Vec2 | number,
screen_size_px_or_y: number | Vec.Vec2?,
screen_size_px_or_nil: Vec.Vec2?,
): ScreenVec
coord.vh(
v_or_x: Vec.Vec2 | number,
screen_size_px_or_y: number | Vec.Vec2?,
screen_size_px_or_nil: Vec.Vec2?,
): ScreenPosition
coord.vhVec(
v_or_x: Vec.Vec2 | number,
screen_size_px_or_y: number | Vec.Vec2?,
screen_size_px_or_nil: Vec.Vec2?,
): ScreenVec
export type ScreenVec = ...

Operators

a + b — (a: ScreenVec, b: ScreenVec): ScreenVec

a - b — (a: ScreenVec, b: ScreenVec): ScreenVec

ScreenVec:gl(): Vec.Vec2
ScreenVec:px(): Vec.Vec2

Returns the position as a Vec2 in the pixel coordinate system

ScreenVec:scale(k: number): ScreenVec
export type ScreenPosition = ...

Represents somewhere on the screen. This is essentially a Vec2 with a unit. You can use px, gl, vh, or vw to create a ScreenPosition from a Vec2 using your preferred coordinate system.

Operators

a + b — (a: ScreenPosition, b: ScreenVec): ScreenPosition

a - b — ((ScreenPosition, ScreenPosition) -> ScreenVec) & ((ScreenPosition, ScreenVec) -> ScreenPosition)

ScreenPosition:gl(): Vec.Vec2

Returns the position as a Vec2 in the OpenGL coordinate system

ScreenPosition:px(screen_size: Vec.Vec2?): Vec.Vec2

Returns the position as a Vec2 in the pixel coordinate system

coord.CENTER = ...
coord.TOP_LEFT = ...
coord.TOP_RIGHT = ...
coord.BOTTOM_LEFT = ...
coord.BOTTOM_RIGHT = ...