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

get-blueprint

v0.2.2

Published

Drawing tool for creating diagrams using D3.

Downloads

32

Readme

Blueprint

Travis   npm   License MIT


Blueprint Diagram

Getting Started

Adding a shape is as simple as passing in a string representation for your shape. For example blueprint.add('rect') would add a rectangle to the canvas.

var rect = blueprint.add('rect');

In the above case the rect is an instance of Interface which encapsulates a lot of useful accessor methods for manipulating the shape, without exposing you to the complexity beneath.

You may set attributes using setAttr and get attributes using getAttrInterface also has shorthand methods for setting attributes individually, such as x(), y(), height(), width(), etc...

Interface also has the attr method for setting or getting depending on if a value was passed:

var value = Math.random();
expect(rectangle.width(value).width()).toEqual(value); // √

Note: The z() method is for setting the z-index but isn't applied directly to your rect shape, it is instead applied to the g element that is wrapped around your shape. When you define the z-index for your shape, the Events.REORDER event will be dispatched and all elements will be re-ordered using their z-indexes using d3.sort. See Z-Index Management for further information.

Debugging: Data Attribute

Mostly for debugging purposes, each Interface object has a toString method which returns the ID of the attribute ([object Interface: BP5]) which corresponds to the data-id — which can be changed — attribute on your shape's g element: <g data-id="BP5">...</g>. Each Shape object also has a toString method which returns the shape's ID ([object Rect: BP5]) which is a nexus between the Shape and its Interface. You may also return the Shape instance — although it's not recommended — by taking it from the blueprint.shapes array.

this.shapes.push({
    shape: shape, // all of the complexity.
    interface: shape.getInterface() // interface developers deal with.
});

For the blueprint.all method the interface of each shapes object is returned:

return this.shapes.map((model) => model.interface);

Change Data Attribute

By default Blueprint sets the data-id attribute on each element's group, but this can be changed using the constructor:

var blueprint = new Blueprint(svg, {
    dataAttribute: 'data-blueprint-id'
});

Creating Shapes

All of the shapes in Blueprint use hooks to allow for the easy creation of custom shapes.

  • [x] getTag — For specifying the root element's tag name;
  • [x] addInterface — For adding the specialised interface;
  • [ ] addAttributes — For applying custom attributes;
  • [ ] addElements — For appending elements to the group/shape elements;

Shapes can be registered with the register method on the Blueprint object – it accepts a name (string) and an object (Shape).

class Circle extends Shape {}
blueprint.register('circle', Circle);

Removing Shapes

Once you've created a shape, you will probably wish to remove it at some point. For this, the Interface object has a remove method which dispatches an Events.REMOVE event to the Blueprint object. By using this method to remove the shape, Blueprint can ensure the cleanup is invoked to prevent memory leaks.

var rect = blueprint.add('rect').fill('blue').x(100);
rect.remove(); // bye bye.

Dispatchers

Each Shape has a Dispatcher (this.dispatcher) which is capable of dispatching events that affect every shape, whereas Feature and Interface objects only have dispatchers capable of dispatching events to the Shape object — if an event is intended to be broadcast to all shapes, then it's the responsibility of the Shape object to relay the dispatched event to the Blueprint object, such as in the case of Events.DESELECT from Selectable.

In the above diagram we can see that Blueprint has the main dispatcher that it injects into Shape — each Interface and Feature have their own dispatchers.

Z-Index Management

Technically SVG doesn't have a z-index property as CSS does, and therefore the Z value is determined by the insertion order of the elements. Blueprint provides a handful of convenience methods for managing the Z index. Aside from the typical z method which accepts any numerical value — including Infinity and -Infinity which will be translated to in between 1 and groups.lengthBlueprint has the following methods:

  • sendToBack() sends the shape to the back — 1;
  • bringToFront() brings the shape to the front — groups.length;
  • sendBackwards() sends the shape backwards one step — z() - 1;
  • bringForwards() brings the shape forwards one step — z() + 1;
// shufflin', shufflin'...
var rect = blueprint.add('rect').z(-Infinity);
rect.bringToFront().sendBackwards().bringForwards().sendToBack();