### Installing Vector Library via npm
Source: https://github.com/basics/vector/blob/main/src/adapter/README.md
This command uses npm (Node Package Manager) to install the `@js-basics/vector` library, which includes the PlayCanvas adapter, into your project's `node_modules` directory. It's the standard way to install dependencies in Node.js projects.
```Shell
npm i @js-basics/vector
```
--------------------------------
### Importing Vector Library Components via npm (Module Load)
Source: https://github.com/basics/vector/blob/main/README.md
Illustrates the standard ES module import syntax to bring the library's core functions and classes (`calc`, `vector`, `victor`, `point`, `ipoint`) into scope when the library is installed via npm and used in a module environment.
```javascript
import { calc, vector, victor, point, ipoint } from "@js-basics/vector";
```
--------------------------------
### Creating a 3D Vector with Numeric Coordinates (Mutable)
Source: https://github.com/basics/vector/blob/main/README.md
Shows how to create a mutable 3D vector object using the `vector` factory function by providing its x, y, and z coordinates as numbers. Includes example debug output showing the vector's representation.
```javascript
const pos = vector(5, 6, 7);
const dir = vector(1, 0, 0);
console.log(debug`pos:${pos}, dir: ${dir}`);
// pos: { x: 5, y: 6, z: 7 } dir: { x: 1, y: 0, z: 0 }
```
--------------------------------
### Calculating Cross Product of Two Vectors
Source: https://github.com/basics/vector/blob/main/README.md
Shows how to use the `cross` method on a vector object to calculate the cross product with another vector. The result is a new vector orthogonal to both input vectors. Includes example debug output.
```javascript
const dir1 = vector(0, 1, 0);
const dir2 = vector(-1, 0, 1);
const cross = dir1.cross(dir2);
console.log(debug`cross: ${cross}`);
// cross: { x: 1, y: 0, z: 1 }
```
--------------------------------
### Importing Vector Operator Extension Components via npm (Module Load)
Source: https://github.com/basics/vector/blob/main/README.md
Illustrates the standard ES module import syntax to bring the operator extension functions (`cachedValueOf`, `cachedFactory`, `operatorCalc`) into scope when the library is installed via npm and used in a module environment, specifically importing from the `/operator` path.
```javascript
import {
cachedValueOf,
cachedFactory,
operatorCalc
} from "@js-basics/vector/operator";
```
--------------------------------
### Normalizing a Vector Using Arithmetic Operators
Source: https://github.com/basics/vector/blob/main/README.md
Shows how to normalize a vector by dividing it by its length using the arithmetic division operator (`/`) within the calculation function provided to the `vector` factory. This utilizes the library's operator overloading for division. Includes example debug output.
```javascript
const norm = vector(() => offsetA / offsetA.length);
console.log(debug`norm: ${norm}`);
// norm: { x: 0.967, y: 0.1658, z: 0.1934 }
```
--------------------------------
### Demonstrating Mutability and In-place Calculation for Point (2D Mutable)
Source: https://github.com/basics/vector/blob/main/README.md
Shows that the `point` type is a mutable 2D vector, allowing direct modification of `x` and `y`. It also demonstrates the `calc` method for performing in-place calculations on its components, similar to the `vector` type. Includes example debug output.
```javascript
const p1 = point(5, 6);
p1.x = 27;
console.log(debug`p1: ${p1}`);
// p1: { x: 27, y: 6 }
p1.calc(p => (p + 1) * 12);
console.log(debug`v1: ${v1}`);
// p1: { x: 336, y: 84 }
```
--------------------------------
### Combining Cross Product Normalization with Operator Overloading
Source: https://github.com/basics/vector/blob/main/README.md
Demonstrates using the `crossNormalize` method within the calculation function provided to the `vector` factory. This shows that method calls can be combined with arithmetic operators in the expression. The result is scaled by 50. Includes example debug output.
```javascript
const crossNormCalc = vector(() => dir1.crossNormalize(dir2) * 50);
console.log(debug`crossNormCalc: ${crossNormCalc}`);
// crossNormCalc: { x: 35.36, y: 0, z: 35.36 }
```
--------------------------------
### Creating a Vector via Operator Overloading Calculation
Source: https://github.com/basics/vector/blob/main/README.md
Illustrates how to create a new mutable vector by providing a calculation function to the `vector` factory. This function uses standard arithmetic operators on previously defined vector objects (`dir`, `pos`) and a number (30), demonstrating the core operator overloading feature. Includes example debug output.
```javascript
const offsetA = vector(() => dir * 30 + pos);
console.log(debug`offsetA: ${offsetA}`);
// offsetA: { x: 35, y: 6, z: 7 }
```
--------------------------------
### Demonstrating Immutability for Victor (3D Immutable)
Source: https://github.com/basics/vector/blob/main/README.md
Shows that the `victor` type is immutable. Attempting to modify its `x`, `y`, or `z` properties directly results in an error, enforcing that `victor` objects cannot be changed after creation. Includes example error output demonstrating this behavior.
```javascript
const v2 = victor(5, 6, 7);
try {
v2.x = 27;
} catch (error) {
console.log(`error: ${error}`);
// error: Error: set x() not implemented
}
```
--------------------------------
### Demonstrating Mutability and In-place Calculation for Vector (3D Mutable)
Source: https://github.com/basics/vector/blob/main/README.md
Shows that the `vector` type is mutable, allowing direct modification of its `x`, `y`, and `z` properties. It also demonstrates the `calc` method (different from the global `calc` function) used directly on the vector object to perform an in-place calculation on its own components. Includes example debug output.
```javascript
const v1 = vector(5, 6, 7);
v1.x = 27;
console.log(debug`v1: ${v1}`);
// v1: { x: 27, y: 6, z: 7 }
v1.calc(p => (p + 1) * 12);
console.log(debug`v1: ${v1}`);
// v1: { x: 336, y: 84, z: 96 }
```
--------------------------------
### Creating a Custom Factory Function for Operator-Overloaded Class
Source: https://github.com/basics/vector/blob/main/README.md
Shows how to create a factory function (`tuple`) for a custom class (`Tuple`) enhanced with `cachedValueOf`. This factory uses `cachedFactory` internally and integrates `operatorCalc` to handle the calculation function syntax, providing a consistent API with the library's vector factories for custom types. Includes example usage and output.
```javascript
const tuple = (() => {
const tupleFactory = cachedFactory(Tuple);
return (x, y, z) => {
if (typeof x === 'function') {
return operatorCalc(x, new Tuple());
}
return tupleFactory(x, y, z);
};
})();
const t1 = tuple(3, 4, 5);
const t2 = tuple(6, 7, 8);
const t = tuple(() => t1 + t2 * 2);
console.log(`t: ${JSON.stringify(t)}`);
// t: {"x":15,"y":18,"z":21}
```
--------------------------------
### Demonstrating Immutability for IPoint (2D Immutable)
Source: https://github.com/basics/vector/blob/main/README.md
Shows that the `ipoint` type is an immutable 2D vector. Attempting to modify its `x` or `y` properties directly results in an error, enforcing that `ipoint` objects cannot be changed after creation. Includes example error output demonstrating this behavior.
```javascript
const p2 = ipoint(5, 6);
try {
p2.x = 27;
} catch (error) {
console.log('error:', error.toString());
// error: Error: set x() not implemented
}
```
--------------------------------
### Calculating and Normalizing Vector Cross Product
Source: https://github.com/basics/vector/blob/main/README.md
Illustrates using the `crossNormalize` method, which combines the cross product calculation with subsequent normalization into a single operation. This is a common pattern for finding a unit vector perpendicular to two others. Includes example debug output showing the resulting unit vector.
```javascript
const crossNorm = dir1.crossNormalize(dir2);
console.log(debug`crossNorm: ${crossNorm}`);
// crossNorm: { x: 0.7071, y: 0, z: 0.7071 }
```
--------------------------------
### Mixing 2D and 3D Vector Operations
Source: https://github.com/basics/vector/blob/main/README.md
Shows how to perform calculations involving both 2D (`point`, `ipoint`) and 3D (`victor`) vector types. It demonstrates creating a 3D vector from a 2D vector and then creating a 2D vector from the 3D vector's 2D component (`.xy`), highlighting interoperability between space dimensions. Includes example debug output.
```javascript
// mix 2D and 3D space
const from2d = victor(() => point(25, 30) / 2);
const from3d = ipoint(() => from2d.xy * 2);
console.log(debug`from2d: ${from2d}`);
console.log(debug`from3d: ${from3d}`);
// from2d: { x: 12.5, y: 15, z: 0 }
// from3d: { x: 25, y: 30 }
```
--------------------------------
### Creating Vector Objects Inside Calculation Function
Source: https://github.com/basics/vector/blob/main/README.md
Illustrates that new vector objects can be instantiated directly within the calculation function passed to a vector factory like `victor`. The library's caching mechanism ensures this works as expected, allowing for complex inline vector constructions. Includes example debug output.
```javascript
// creating vector inside calculation works fine,
// thanks to caching in factory function
const inlineVec = victor(() => victor(25, 30, 0) / 2);
console.log(debug`inlineVec: ${inlineVec}`);
// inlineVec: { x: 12.5, y: 15, z: 0 }
```
--------------------------------
### Loading Vector Library via HTML Script Tag
Source: https://github.com/basics/vector/blob/main/README.md
Provides the HTML script tag required to load the `@js-basics/vector` library directly in a web browser environment using the IIFE build distributed via unpkg. This allows using the library globally via the `basics.vector` object.
```html
```
--------------------------------
### Demonstrating Vector Calculation Styles in JavaScript
Source: https://github.com/basics/vector/blob/main/README.md
Shows different ways to perform vector arithmetic in JavaScript: the standard method chaining approach, manual component-wise calculation, an intermediate approach, and the library's proposed operator overloading approach using the `calc` function for improved readability.
```javascript
// typical implementation of vector in js
const vec = aVec.multiply(bVec).multiply(4).substract(dVec).substract(1.5);
⇓
// better readable, but much more complicated
const vec = new Vec(aVec.x * bVec.x * 4 - dVec.x - 1.5,
aVec.y * bVec.y * 4 - dVec.y - 1.5,
aVec.z * bVec.z * 4 - dVec.z - 1.5);
⇓
// inspired by smart array handling
// first version of calling assigned function three times
const vec = oldCalc( aVec, bVec, dVec,
( aVec, bVec, dVec) => aVec * bVec * 4 - dVec - 1.5
);
⇓
// final version with overwritten valueOf() method
const vec = calc(() => aVec * bVec * 4 - dVec - 1.5);
```
--------------------------------
### Initializing Adapter in PlayCanvas Editor
Source: https://github.com/basics/vector/blob/main/src/adapter/README.md
This JavaScript code snippet, intended for a script asset in the PlayCanvas editor set to "After Engine" loading, checks if the `basics` object is available and then calls the `hijackPlayCanvas` function on the adapter, passing the global `pc` object to integrate with the PlayCanvas engine.
```JavaScript
if (typeof basics !== 'undefined') {
basics.vector.adapter.playcanvas.hijackPlayCanvas(pc);
}
```
--------------------------------
### Using pc.calc() for Vector Arithmetic (PlayCanvas)
Source: https://github.com/basics/vector/blob/main/src/adapter/README.md
This JavaScript code shows how to use the `pc.calc()` function provided by the adapter. It allows vector arithmetic operations (like multiplication and addition) to be expressed using standard operators within a function scope, demonstrating the resulting vector `offsetA` after calculating `dir * 30 + pos`. Requires the adapter to be loaded.
```JavaScript
const pos = pc.vec3(5, 6, 7);
const dir = pc.vec3(1, 0, 0);
// pos: { x: 5, y: 6, z: 7 } dir: { x: 1, y: 0, z: 0 }
const offsetA = pc.calc(() => dir * 30 + pos);
// offsetA: { x: 35, y: 6, z: 7 }
```
--------------------------------
### Loading PlayCanvas Adapter via HTML
Source: https://github.com/basics/vector/blob/main/src/adapter/README.md
This HTML script tag loads the PlayCanvas adapter for the `@js-basics/vector` library from a CDN (unpkg). It's suitable for including the adapter directly in a web page or when not using a module bundler.
```HTML
```
--------------------------------
### Accessing Vector Library Components from Global Scope (HTML Load)
Source: https://github.com/basics/vector/blob/main/README.md
Demonstrates how to destructure and access the main functions and classes (`calc`, `vector`, `victor`, `point`, `ipoint`) from the `basics.vector` global object after the library has been loaded via an HTML script tag in a browser.
```javascript
const { calc, vector, victor, point, ipoint } = basics.vector;
```
--------------------------------
### Loading PlayCanvas Adapter via ES Module Import
Source: https://github.com/basics/vector/blob/main/src/adapter/README.md
This JavaScript import statement loads and initializes the PlayCanvas adapter provided by the `@js-basics/vector` library when using a module bundler like Webpack or Rollup. It registers the adapter with the library.
```JavaScript
import "@js-basics/vector/adapter/playcanvas";
```
--------------------------------
### Loading Vector Operator Extension via HTML Script Tag
Source: https://github.com/basics/vector/blob/main/README.md
Provides the HTML script tag specifically for loading only the operator overloading functionality of the library when using the IIFE build from unpkg. This allows applying the operator extension mechanism to custom classes without loading the vector types themselves.
```html
```
--------------------------------
### Accessing Vector Operator Extension Components from Global Scope (HTML Load)
Source: https://github.com/basics/vector/blob/main/README.md
Demonstrates how to destructure and access the functions (`cachedValueOf`, `cachedFactory`, `operatorCalc`) from the `basics.vector.operator` global object after the operator extension script has been loaded via an HTML script tag.
```javascript
const { cachedValueOf, cachedFactory, operatorCalc } = basics.vector.operator;
```
--------------------------------
### Applying Operator Overloading to a Custom Class (Override valueOf)
Source: https://github.com/basics/vector/blob/main/README.md
Shows how to make a custom class (`Tuple` in this case) compatible with the library's operator overloading by calling the `cachedValueOf` function on the class constructor. This modifies the class prototype to handle arithmetic operations via `valueOf`, enabling operators.
```javascript
class Tuple {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}
cachedValueOf(Tuple);
```
--------------------------------
### Scaling Vector Length - Math
Source: https://github.com/basics/vector/blob/main/cheatsheet.md
Provides the formula for changing the length of a vector (`vec`) to a new length (`newLen`) while keeping its direction. It shows that normalizing the vector and multiplying by the new length is equivalent to scaling the original vector by the ratio of the new length to the original length. This is a fundamental vector operation.
```Math
normalize(vec) * newLen = vec * newLen / length(vec)
```
--------------------------------
### Calculating Pi - Math
Source: https://github.com/basics/vector/blob/main/cheatsheet.md
Defines or calculates the mathematical constant Pi using built-in functions `radians` and `acos`. It shows that converting 180 degrees to radians equals Pi, and that `acos(0.0) * 2.0` also equals Pi. This formula is commonly used to obtain an accurate Pi value.
```Math
π = radians(180.0) = acos(0.0) * 2.0
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.