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

@svgdotjs/svg.draw.js

v3.0.1

Published

An extension for svg.js which allows to draw elements with mouse

Downloads

1,172

Readme

svg.draw.js

An extension of svg.js, which allows to draw elements with your mouse

Demo

For a demo see http://svgdotjs.github.io/svg.draw.js/

Get Started

Install the plugin:

npm install @svgdotjs/svg.js @svgdotjs/svg.draw.js

Include the script after svg.js into your page

<script src="node_modules/@svgdotjs/svg.js"></script>
<script src="node_modules/@svgdotjs/svg.draw.js"></script>

Draw your first rectangle using this simple piece of code:

<div id="myDrawing"></div>
<script>
  var canvas = new SVG().addTo('#myDrawing').size(500, 500)
  canvas.rect().draw() // Here we init a rectangle and start drawing it
</script>

Usage

As default the drawing starts with a click on the SVG element

canvas.rect().draw(options)

You can use your own mouse events. Just pass the event object to the draw Function

var canvas = drawing.rect()

canvas.on('mousedown', function (event) {
  rect.draw(event, options)
})
canvas.on('mouseup', function (event) {
  rect.draw(event)
})

The addon automatically knows when to start or stop drawing (most shapes start with the first event and stop with the second). However, when dealing with e.g. a polygon, you are able to set new points with every event. To finish the drawing you have to call the done function. See the next chapter for that.

Methods

svg.draw.js populates its methods it uses to draw the shape. This is useful in edgecases, but generally not needed (with some exceptions):

  • done must be called to complete polygons/polylines
  • cancel can be called on every shape to stop drawing and remove the shape
// Finishes the poly-shape
polygon.draw('done')

// Cancels drawing of a shape, removes it
polygon.draw('cancel')

/* The following are only useful in edge-cases */

// Draws a new point with the help of a (mouse) event
polygon.draw('point', event)

// Draws the point while moving the mouse (basically the animation)
polygon.draw('update', event)

// Stop drawing, cleans up
polygon.draw('stop', event)

Options

The following options can be used to modify the behavior of the addon:

  • snapToGrid: Specifies the size of the grid to which a point is aligned (default:1)
  • drawCircles: Specifies the need to draw little circles around the line/polyline/polygon points (default: true)

Note that you can specify the options only on the first call. When you want to change the options while drawing use polygon.draw('params', key, value) This is useful, for example if you only want to activate the grid option while CTRL is held down.

Events

svg.draw.js fires a few specific events which are:

  • drawstart
  • drawstop
  • drawupdate
  • drawpoint
  • drawdone
  • drawcancel

These events are called at the end of the corresponding method.

Each event object holds the relative position to the parent object of the shape (which is mostly the SVG-doc itself) as an array

Binding a function to the event is easy

var rect = canvas.rect().draw()
rect.on('drawstart', function (event) {
  console.log(event.detail) // Holds event, current point coords and matrix
})

Plugins

Currently svg.draw.js only supports all the basic shapes (line, polyline, polygon, rect, image, circle, ellipse). Any other type you want to draw, can be added using a plugin which just serves the functions to draw the shape.

For example:

SVG.Element.prototype.draw.extend('line polyline polygon', {

	// add methods here which should be added to the `draw` object
	// e.g.
	foo: function(){
		// can access this
	}

	// or even variables
	bar:5

}

The calc method is always needed which updates the point of the shape.

You can also extend two shape types at once:

SVG.Element.prototype.draw.extend({

	'line polyline polygon': {
		// add methods here which should be added to the `draw` object
		// e.g.
		foo: function(){
			// can access this
		}

		// or even variables
		bar:5
	}


	'circle':{
		// something
	}
}

See the implementation of all shapes as examples.