@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
27 lines • 926 B
JavaScript
import { Node } from '../../programmatic/node.js';
import { Vec2Schema } from '../../schemas/index.js';
export default class NodeDefinition extends Node {
static title = 'Normalize Vector2';
static type = 'studio.tokens.vector2.normalize';
static description = 'Normalizes a vector to have a length of 1';
constructor(props) {
super(props);
this.addInput('vector', {
type: Vec2Schema
});
this.addOutput('value', {
type: Vec2Schema
});
}
execute() {
const { vector } = this.getAllInputs();
const length = Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1]);
if (length === 0) {
// Handle zero vector case
this.outputs.value.set([0, 0]);
return;
}
this.outputs.value.set([vector[0] / length, vector[1] / length]);
}
}
//# sourceMappingURL=normalize.js.map