draw-triangles-2d

1.0.0 • Public • Published

draw-triangles-2d

stable

Draws indexed triangles from a simplicial complex using Canvas2D context.

Positions and cells look like this:

{ positions: [ [25,25], [50, 50], ... ], cells: [ [0,1,2], [1,2,3], ... ] }

Demo:

var drawTriangles = require('draw-triangles-2d')
 
var path = [ [25, 25], [40, 30], [50, 75], [125, 15] ]
 
//get a thick polyline
var mesh = require('extrude-polyline')({ 
    thickness: 20,
    miterLimit: 1.5
}).build(path)
 
function render(ctx, width, height) {
    ctx.clearRect(0,0,width,height)
 
    ctx.beginPath()
    drawTriangles(ctx, mesh.positions, mesh.cells)
    ctx.strokeStyle = 'black'
    ctx.lineJoin = 'bevel'
    ctx.stroke()
}

Simplicial complexes are a common ground between various modules, such as triangulate-contours, extrude-polyline, etc.

Usage

NPM

draw(context, positions, cells[, start, end])

Draws the 2D positions and triangle cells (i.e. indices into positions) to the given context. You can specify start (inclusive) and end (exclusive) indices to only render a sub range of the cells. These values defualt to 0 and the length of cells, respectively.

License

MIT, see LICENSE.md for details.

Package Sidebar

Install

npm i draw-triangles-2d

Weekly Downloads

16

Version

1.0.0

License

MIT

Last publish

Collaborators

  • mattdesl