Scalars Types
In this document, we will cover some of the scalar types used in a graphing library.
Vec2
The Vec2
class represents a 2D vector or a point in the 2D space.
It has two properties, x
and y
, representing the coordinates of the point or the dimensions of the vector.
Vec2ish
Vec2ish
is a flexible type that can represent a Vec2 instance or a similar 2D vector or point.
- ts
export type Vec2ish = Vec2 | [number, number] | { x: number; y: number };
- A
Vec2
instance. - A tuple of two numbers
- An object with
x
andy
properties.
Graph units
The Graph units can be of two types, either ViewSpace (vs) or Coord Space (cs).
- ts
type ScalarUnits = "vs" | "cs";
vs
: Represents ViewSpace units, relating to the graph size in pixels.cs
: Stands for Coord Space, representing coordinates in the graph.
Scalar
A scalar in this context can be a number or a string. When a string, it has to include the unit type (vs
or cs
) along with the value.
- ts
// Valid Scalars const scalar: Scalar = 10; const scalar: Scalar = "10vs"; const scalar: Scalar = "10cs"; const scalar: Scalar = "10.5vs"; const scalar: Scalar = "-10.5cs";
-
Plain numbers are inferred based on context, they are computed as ViewSpace for things like styling (font size, strokeWidth, etc), and Coord Space for positions or plot values.
-
Strings with 'vs' or 'cs' define explicitly the units of the scalar value.
ScalarPoint
Similar to Vec2ish, ScalarPoint is a flexible representation of a point in 2D space, and can be defined as a Vec2 object, an object with x and y properties, or an array of two numbers.
Differently from Vec2ish, the values of the point can be scalars.
- ts
// Valid Scalar Points const scalarPoint: ScalarPoint = [10, 10]; const scalarPoint: ScalarPoint = { x: 10, y: 10 }; const scalarPoint: ScalarPoint = { x: "10vs", y: "10vs" }; const scalarPoint: ScalarPoint = { x: "10cs", y: "10cs" }; const scalarPoint: ScalarPoint = { x: 10, y: "10vs" }; import { Vec2 } from "@coord/graph"; const scalarPoint: ScalarPoint = Vec2.of(10, 10);
- Like scalars,
number
values are inferred based on context, they default to ViewSpace for styling, and Coord Space for positions or plot values. - Strings with
vs
orcs
define explicitly the units of the point's components. This allows for the creation of points that mix ViewSpace and Coord Space units. - The Vec2 class offers a static method
of
to conveniently create aVec2
instance from aVec2ish
value.