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

bristle

v0.2.9

Published

A small JS library for drawing dynamic paths into html canvas elements.

Downloads

3

Readme

Bristle

What is it?

A small JS library to help make drawing dynamic paths into <canvas> elements easier.

Gist

A simple example

Note: For the path functions, 2D vectors are represented as two element arrays, so [x, y]

let canvasCtx = document.querySelector('#my-canvas').getContext('2d');
let bristleObj = bristle(canvasCtx);

canvasCtx.beginPath();

bristleObj.sequence((bristleCtx) => {
    bristleCtx
        .moveTo([10, 10])
        .lineTo([50, 50) 
        .lineTo([50, 10])
        .commit(); //This will send our stored instructions into the 2D context object.
    });
    
canvasCtx.fillStyle = '#4A4';
canvasCtx.fill();

Ok, so that's the very basics. Now, let's add some transformations that are applied to subsequent input operations.

let canvasCtx = document.querySelector('#my-canvas').getContext('2d');
let bristleObj = bristle(canvasCtx);

bristleObj.sequence((bristleCtx) => {
    // The bristle 'context' object has a transform stack we can push offsets to.
    bristleCtx.pushTransform([0, 10])
    .moveTo([0, -10])
    .lineTo([-10, 0])
    
    // Transforms combine as we push them to the stack,
    //     so our current global transform point is [0, 50].
    .pushTransform([0, 40]) 
    .lineTo([-10, 0])
    .lineTo([0, 10])
    .lineTo([10, 0])
    
    // And now we're back to [0, 10].
    .popTransform()
    .lineTo([10, 0])
    .commit((canvasCtx, trigger) => { //we can pass a function to .commit
            // The predicate we pass to .commit gets the original CanvasRenderingContext2D
            //   object we passed in when we created bristleCtx.
            canvasCtx.beginPath();
    
            // By calling this trigger function, all our stored instructions 
            //   get dumped into the canvas context
            trigger();
    
            // We can do our canvas context drawing 
            //   configuration/execution in this predicate function
            canvasCtx.fillStyle = '#0C0'; 
            canvasCtx.fill();
        });
    });

And now, we can break our paths up into sequences that can be chained together

let canvasCtx = document.querySelector('#my-canvas').getContext('2d');
let bristleObj = bristle(canvasCtx);

bristleObj.sequence((bristleCtx) =>
    bristleCtx.pushTransform([-40, 0])
        .moveTo([-10, 0])
        // The .sequence method takes a handler function
        .sequence((seqCtx) => {
            // The first argument passed into the
            //   sequence handler funtion is our bristleCtx object
            //   unless that behavior has been overridden

            seqCtx.pushTransform([40, 40])
                .lineTo([-20, 10])
                .lineTo([20, 10]);

            seqCtx.sequence((seqCtx) => {
                seqCtx.pushTransform([40, -40])
                    .lineTo([10, 0])
                    .lineTo([-10, 0]);
            });

            // When we return from a sequence, our transform is
            //   right where we left it.
            seqCtx.lineTo([0, -10]);
        });
    });