npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kaygnas-polyline-3d

v0.1.1

Published

A lite weight library created for drawing polyline which is consist of multiple points in WebGL.

Downloads

2

Readme

Polyline 3D

A lite weight library created for drawing polyline which is consist of multiple points in WebGL.

Online Demo

Source code could be found here.

Installation

npm i kaygnas-polyline-3d gl-matrix

Caution: gl-matrix is a peerDependency, please make sure to install it.

Usage

Create a Dot. Dot is the basic element of a polyline, a dot has four properties x, y, size, depth.

import { Dot } from 'kaygnas-polyline-3d'
const dot = new Dot(
  0.0, // x position of dot
  0.0, // y position of dot
  0.01, // size of dot, determine the width of line
  0.01, // depth of dot, determine the depth of line in z direction
)

Create a list of dots could be easy using Dot.fromValue:

import { Dot } from 'kaygnas-polyline-3d'
const dots = [
  // the outline of a triangle
  [-0.1, -0.1, 0.01, 0.01],
  [0.0, 0.1, 0.02, 0.02],
  [0.1, 0.1, 0.03, 0.03],
  [-0.1, -0.1, 0.01, 0.01],
].map(Dot.fromValue)

Create a Polyline3DMeshBuilder. MeshBuilder help to build a list of dot into 3d mesh, which would be render by the Polyline3DMeshRenderer(we would talk about how to do that later).

import { Polyline3DMeshBuilder } from 'kaygnas-polyline-3d'
const builder = new Polyline3DMeshBuilder(
  dots, // dots to build
  {
    smooth: true, // whether to soomth the line of dots
    interpolationCount: 20, // if smooth is on, interpolationCount determine how smooth the line would be. The bigger interpolationCount is, the smoother the line would be.
  }
)

Create a Polyline3DMeshRenderer. MeshRenderer help to render the mesh. Also, MeshRenderer control how the light of scene would be and where the camera is.

import { Polyline3DMeshRenderer } from 'kaygnas-polyline-3d'
const renderer = new Polyline3DMeshRenderer(canvasElement)
renderer.setMeshs(builder.build()) // set the meshs to render.
renderer.render() // render the meshs.

Change the light of scene or the position of camera.

import { vec3 } from 'gl-matrix'
render.lightPosition = vec3.fromValues(0.0, 0.0, 1.0) // set the position of light, the light source is just a spot.
renderer.lightColor = vec3.fromValues(1.0, 0.0, 0.0) // set the color of the light, in this line it is set to red.
renderer.ambientLight = vec3.fromValues(0.0, 0.0, 0.4) // set the ambient light of the scene, in this line it is set to dark blue.
renderer.eye = vec3.fromValues(-0.5, 0.0, 1.0) // set the position of camera.

Create a MouseTracker. MouseTracker help to track the movement of mouse, therefore we could transform the polyline as we want. It is helpful when requirement of making the line interactive is met. Normaliy, it is used to rotate the line.

import { MouseTracker } from 'kaygnas-polyline-3d'
import { mat4 } from 'gl-matrix'
const controler = new MouseTracker({
  traget: canvasElement,
  onTrack: (e) => {
    const sensitivity = 0.02
    const rotation = { x: e.movementY * sensitivity, y: e.movementX * sensitivity }
    const rotateMat = mat4.create() // create a rotateMatrix to transform the line.
    mat4.rotateX(rotateMat, rotateMat, rotation.x)
    mat4.rotateY(rotateMat, rotateMat, rotation.y)
    mat4.multiply(renderer.modelMat, rotateMat, renderer.modelMat) // apply the rotateMatrix to modelMatrix.
    renderer.render()
  },
})
controler.enable() // Do not forget to enable the controler. By the way, to disable just call `controler.disable()`.

That's all. It might be difficult to understand what's going on with the vec3 and mat4, here's some reference which might help.