points-to-vertices
v0.2.0
Published
:white_square_button: a points to vertices array convertor
Downloads
27
Readme
points-to-vertices
:white_square_button: a points to vertices array convertor
Usage
import pointsToVertices from 'points-to-vertices';
const points = [
{
x: 1,
y: 1,
z: 1,
color: `rgba(255, 0, 0, .3)`
},
{
x: 1,
y: - 1,
z: 0,
color: `#FF0000`
}
];
const vertices = pointsToVertices(points);
console.log(vertices);
// > [1, 1, 1, 1, 0, 0, .3, 1, -1, 0, 1, 0, 0, 1]
Each point can have x, y, z and color (vec4, hex, rgb or rgba) properties
points-to-vertices
fills in the blanks with (provided) defaults.
result is an array of vertices
[
// point 1
1, 1, 1, // x, y, z
1, 0, 0, .3, // r, g, b, a
// point 2
1, -1, 0, // x, y, z
1, 0, 0, 1 // r, g, b, a
]
Installation
Install via yarn
yarn add points-to-vertices (--dev)
or npm
npm install points-to-vertices (--save-dev)
configuration
You can pass in extra options as a configuration object (all parameters are optional)
import pointsToVertices from 'points-to-vertices';
const points = [
{
x: -1,
y: 0,
z: 0,
color: `rgba(255, 0, 0, .3)`
},
{
x: 0,
y: - 1,
}
];
const vertices = pointsToVertices(points, {
meta: true, // wrap the data in an object
color: false // no colors in result vertices array, only points
dx: 1, // default x
dy: 1, // default y
dz: - 1, // default z
dcolor: `#FFFF00` // default color
});
➖ color ( boolean ) ✏️ true
📝 output colors in vertices array
ℹ️ false
=> [x, y, z]
vertex
ℹ️ true
=> [x, y, z, r, g, b, a]
vertex (default)
➖ meta ( boolean ) ✏️ false
📝 wrap the result in an object (with a meta property)
ℹ️ returns extra info on vertices, start and end of position, color, total length
ℹ️ meta: false
returns a simple vertices array
example output with meta: true
(annotated for clarity)
{
data: [
// point 1
0, 0, 1, // position (x, y, z)
0, 1, 1, 1, // color (r, g, b, a)
// point 2
1, - 1, 0, // position (x, y, z)
0, 1, 1, 1 // color (r, g, b, a)
],
meta: {
// each vertex is 7 long
vertexLength: 7,
// there are 2 vertices in this array
amount: 2,
// position is 3 long, starts at index 0
position: { start: 0, length: 3 },
// color is 4 long, starts at index 3
color: { start: 3, length: 4 }
}
}
you can pass in default values for x, y, z and color
if no value is set for x, y, z, or color, points-to-vertices
will use the default
➖ dx ( number ) ✏️ 0
📝 default x for a vertex
➖ dy ( number ) ✏️ 0
📝 default y for a vertex
➖ dz ( number ) ✏️ 0
📝 default z for a vertex
➖ dcolor ( vec4 | string ) ✏️ [0, 0, 0, 1] (black)
📝 default z for a vertex
Examples
See example
folder
Builds
If you don't use a package manager, you can access points-to-vertices
via unpkg (CDN), download the source, or point your package manager to the url.
points-to-vertices
is compiled as a collection of CommonJS modules & [ES2015 modules](http://www.2ality.com/2014/0
-9/es6-modules-final.html) for bundlers that support the jsnext:main
or module
field in package.json (Rollup, Webpack 2)
The points-to-vertices
package includes precompiled production and development UMD builds in the dist
folder. They can be used directly without a bundler and are thus compatible with many popular JavaScript module loaders and environments. You can drop a UMD build as a <script>
tag on your page. The UMD builds make points-to-vertices
available as a window.pointsToVertices
global variable.
License
The code is available under the MIT license.
Contributing
We are open to contributions, see CONTRIBUTING.md for more info.
Misc
This module was created using generator-module-boilerplate.