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

graphical-notes

v4.2.2

Published

JS drawing application to ease creating diagrams using D3.

Downloads

11

Readme

Graphical Notes

Graphical Notes Version   License MIT

  • npm: npm install graphical-notes

Package License

This package is fully free with limited usages, but in advanced cases and to use this package as a whole drawing area with all features and +2000 shapes, you may request the pro version.

Getting Started

This package lets the simplicity for adding a new shape in your next project in simple steps by passing a string representation for the shape. For example graphics.add('rect') would add a rectangle to the canvas.

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

In the case we mentioned, the rect is an instance of Interface which have useful accessor methods for manipulating the shape, without exposing you to the complexity beneath.

You can also 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.

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

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

Change Data Attribute

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

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

Creating Shapes

All of the shapes in Graphics 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 Graphics object – it accepts a name (string) and an object (Shape).

class Circle extends Shape {}
graphics.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 Graphics object. By using this method to remove the shape, Graphics can ensure the cleanup is invoked to prevent memory leaks.

var rect = graphics.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 Graphics object, such as in the case of Events.DESELECT from Selectable.

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. Graphics 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.lengthGraphics 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 = graphics.add('rect').z(-Infinity);
rect.bringToFront().sendBackwards().bringForwards().sendToBack();