|
| 1 | +# VecUI |
| 2 | + |
| 3 | +Tiny, ergonomic, fun vector library for UI engineers. |
| 4 | + |
| 5 | +## Why? |
| 6 | + |
| 7 | +UIs are made of rectangles. Rectangles are made of 2 vectors. One for describing their origin point, and another for describing their width and height. Treating them as vectors instead of 4 disjointed numbers makes it much easier to reason about them and can trivially cut complex UI code in half (see demo). |
| 8 | + |
| 9 | +VecUI includes utilities to turn your UI into vectors and back again, and comes with a beautiful API that is both easy to read and write. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- All the Vector2D methods you know and love. |
| 14 | +- A beautiful, ergonomic API that doesn't make you repeat yourself. |
| 15 | +- Immutable values. Treat your vectors just like any other primitive. |
| 16 | +- Utilities for working with UIs. |
| 17 | +- Tiny. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +yarn add vecui |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +TODO |
| 28 | + |
| 29 | +## API |
| 30 | + |
| 31 | +The API consists of functions to create and manipulate vectors and rectangles. All vectors are 2 dimensional. Rectangles are composed of 2 vectors, one for the origin point, and another for the width and height. All values are immutable, functions return new values instead of mutating existing ones. |
| 32 | + |
| 33 | +### Rectangles |
| 34 | + |
| 35 | +#### **rect()** |
| 36 | + |
| 37 | +You can create rectangles using the `rect` function. |
| 38 | + |
| 39 | +```typescript |
| 40 | +const r1 = rect(vec(1, 2), vec(3, 4)); |
| 41 | +const r3 = rect({ x: 1, y: 2, width: 3, height: 4 }); |
| 42 | +const r4 = rect(htmlElement.getBoundingClientRect()); |
| 43 | +``` |
| 44 | + |
| 45 | +#### **o** |
| 46 | + |
| 47 | +The origin point of the rectangle. |
| 48 | + |
| 49 | +```typescript |
| 50 | +const r1 = rect(vec(1, 2), vec(3, 4)); |
| 51 | + |
| 52 | +r1.o; // => vec(1, 2) |
| 53 | +``` |
| 54 | + |
| 55 | +#### **d** |
| 56 | + |
| 57 | +The dimensions of the rectangle. |
| 58 | + |
| 59 | +```typescript |
| 60 | +const r1 = rect(vec(1, 2), vec(3, 4)); |
| 61 | + |
| 62 | +r1.d; // => vec(3, 4) |
| 63 | +``` |
| 64 | + |
| 65 | +#### **as()** |
| 66 | + |
| 67 | +Maps the rectangle to an object with the specified names. |
| 68 | + |
| 69 | +```typescript |
| 70 | +const r1 = rect(vec(1, 2), vec(3, 4)); |
| 71 | + |
| 72 | +r1.as(["x", "y", "width", "height"]); // => { x: 1, y: 2, width: 3, height: 4 } |
| 73 | +``` |
| 74 | + |
| 75 | +Alternatively, you can also use the `.as.css` shortcut to map the rectangle to an object with the names `top`, `left`, `width`, and `height`. |
| 76 | + |
| 77 | +```typescript |
| 78 | +const r1 = rect(vec(1, 2), vec(3, 4)); |
| 79 | + |
| 80 | +r1.as.css; // => { top: 1, left: 2, width: 3, height: 4 } |
| 81 | +``` |
| 82 | + |
| 83 | +#### **equals()** |
| 84 | + |
| 85 | +Checks if the vector equals another vector or specified components. |
| 86 | + |
| 87 | +```typescript |
| 88 | +const r1 = rect(vec(1, 2), vec(3, 4)); |
| 89 | +const r2 = rect(vec(1, 2), vec(3, 4)); |
| 90 | + |
| 91 | +r1.equals(r2); // => true |
| 92 | +``` |
| 93 | + |
| 94 | +### Vectors |
| 95 | + |
| 96 | +#### **vec()** |
| 97 | + |
| 98 | +You can create vectors using the `vec` function. |
| 99 | + |
| 100 | +```typescript |
| 101 | +const v1 = vec(1, 2); |
| 102 | +const v2 = vec([1, 2]); |
| 103 | +const v3 = vec({ x: 1, y: 2 }); |
| 104 | + |
| 105 | +// You can also create vectors with a single number. |
| 106 | +const v4 = vec(100); |
| 107 | +``` |
| 108 | + |
| 109 | +#### **x** |
| 110 | + |
| 111 | +The x component of the vector. |
| 112 | + |
| 113 | +```typescript |
| 114 | +const v1 = vec(1, 2); |
| 115 | + |
| 116 | +v1.x; // => 1 |
| 117 | +``` |
| 118 | + |
| 119 | +#### **y** |
| 120 | + |
| 121 | +The y component of the vector. |
| 122 | + |
| 123 | +```typescript |
| 124 | +const v1 = vec(1, 2); |
| 125 | + |
| 126 | +v1.y; // => 2 |
| 127 | +``` |
| 128 | + |
| 129 | +#### **add()** |
| 130 | + |
| 131 | +Adds another vector or components to this vector. |
| 132 | + |
| 133 | +```typescript |
| 134 | +const v1 = vec(1, 2); |
| 135 | +const v2 = vec(3, 4); |
| 136 | + |
| 137 | +v1.add(v2); // => vec(4, 6) |
| 138 | + |
| 139 | +// You can also directly add scalars to components. |
| 140 | +v1.add(3, 4); // => vec(4, 6) |
| 141 | +``` |
| 142 | + |
| 143 | +#### **sub()** |
| 144 | + |
| 145 | +Subtracts another vector or components from this vector. |
| 146 | + |
| 147 | +```typescript |
| 148 | +const v1 = vec(1, 2); |
| 149 | +const v2 = vec(3, 4); |
| 150 | + |
| 151 | +v1.sub(v2); // => vec(-2, -2) |
| 152 | + |
| 153 | +// You can also directly subtract scalars from components. |
| 154 | +v1.sub(3, 4); // => vec(-2, -2) |
| 155 | +``` |
| 156 | + |
| 157 | +#### **div()** |
| 158 | + |
| 159 | +Divides this vector by another vector or components. |
| 160 | + |
| 161 | +```typescript |
| 162 | +const v1 = vec(1, 2); |
| 163 | +const v2 = vec(3, 4); |
| 164 | + |
| 165 | +v1.div(v2); // => vec(0.33, 0.5) |
| 166 | + |
| 167 | +// You can also directly divide components by scalars. |
| 168 | +v1.div(3, 4); // => vec(0.33, 0.5) |
| 169 | + |
| 170 | +// Or divide by a single scalar. |
| 171 | +v1.div(2); // => vec(0.5, 1) |
| 172 | +``` |
| 173 | + |
| 174 | +#### **dot()** |
| 175 | + |
| 176 | +Calculates the dot product with another vector or components. |
| 177 | + |
| 178 | +```typescript |
| 179 | +const v1 = vec(1, 2); |
| 180 | +const v2 = vec(3, 4); |
| 181 | + |
| 182 | +v1.dot(v2); // => 11 |
| 183 | + |
| 184 | +// You can also directly calculate the dot product with components. |
| 185 | +v1.dot(3, 4); // => 11 |
| 186 | +``` |
| 187 | + |
| 188 | +#### **cross()** |
| 189 | + |
| 190 | +Calculates the cross product with another vector or components. |
| 191 | + |
| 192 | +```typescript |
| 193 | +const v1 = vec(1, 2); |
| 194 | +const v2 = vec(3, 4); |
| 195 | + |
| 196 | +v1.cross(v2); // => -2 |
| 197 | + |
| 198 | +// You can also directly calculate the cross product with components. |
| 199 | +v1.cross(3, 4); // => -2 |
| 200 | +``` |
| 201 | + |
| 202 | +#### **mul()** |
| 203 | + |
| 204 | +Element-wise multiplies the vector with another vector, components, or scalar. |
| 205 | + |
| 206 | +```typescript |
| 207 | +const v1 = vec(1, 2); |
| 208 | +const v2 = vec(3, 4); |
| 209 | + |
| 210 | +v1.mul(v2); // => vec(3, 8) |
| 211 | + |
| 212 | +// You can also directly multiply components by scalars. |
| 213 | +v1.mul(3, 4); // => vec(3, 8) |
| 214 | + |
| 215 | +// Or multiply by a single scalar. |
| 216 | +v1.mul(2); // => vec(2, 4) |
| 217 | +``` |
| 218 | + |
| 219 | +#### **len()** |
| 220 | + |
| 221 | +Calculates the length (L2 norm) of the vector. |
| 222 | + |
| 223 | +```typescript |
| 224 | +const v1 = vec(3, 4); |
| 225 | + |
| 226 | +v1.len(); // => 5 |
| 227 | +``` |
| 228 | + |
| 229 | +#### **norm()** |
| 230 | + |
| 231 | +Normalizes the vector. |
| 232 | + |
| 233 | +```typescript |
| 234 | +const v1 = vec(3, 4); |
| 235 | + |
| 236 | +v1.norm(); // => vec(0.6, 0.8) |
| 237 | +``` |
| 238 | + |
| 239 | +#### **rotRad()** |
| 240 | + |
| 241 | +Rotates the vector by a specified number of radians. |
| 242 | + |
| 243 | +```typescript |
| 244 | +const v1 = vec(1, 0); |
| 245 | + |
| 246 | +v1.rotRad(Math.PI / 2); // => vec(0, 1) |
| 247 | +``` |
| 248 | + |
| 249 | +#### **rotDeg()** |
| 250 | + |
| 251 | +Rotates the vector by a specified number of degrees. |
| 252 | + |
| 253 | +```typescript |
| 254 | +const v1 = vec(1, 0); |
| 255 | + |
| 256 | +v1.rotDeg(90); // => vec(0, 1) |
| 257 | +``` |
| 258 | + |
| 259 | +#### **isInRect()** |
| 260 | + |
| 261 | +Checks if the vector is within a given rectangle. |
| 262 | + |
| 263 | +```typescript |
| 264 | +const v1 = vec(1, 2); |
| 265 | + |
| 266 | +v1.isInRect({ x: 0, y: 0, width: 2, height: 3 }); // => true |
| 267 | +``` |
0 commit comments