• Stars
    star
    236
  • Rank 170,480 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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 svg.draw.js using bower:

      bower install svg.draw.js
    
  • Include the script after svg.js into your page

      <script src="svg.js"></script>
      <script src="svg.draw.js"></script>
    
  • Draw your first rectangle using this simple piece of code:

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

Usage

As default the drawing starts with a click on the svg-Element

var drawing = SVG('drawing');
drawing.rect().draw(options);

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

var drawing = SVG('myDrawing');
var rect = drawing.rect();

drawing.on('mousedown', function(event){
    rect.draw(event, options);
});
drawing.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. However the method done is needed for poly-shapes and 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 (mouse-)event
polygon.draw('point', event)

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

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

Options

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

  • snapToGrid: Specifies a 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 when you want to activate the grid-option when ctrl or soemthing is pressed.

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 Array

Binding a function to the Event is easy

var draw = SVG('drawing');
var rect = draw.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, polygone, rect, image, circle, ellipse). Any other type you want to draw and is available through SVG.invent (e.g. image or your own element) 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

}

Method calc is always needed which updates the point of the shape.

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

Changes in svg.draw.js v2

  • all shapes implemented as plugins
  • drawing takes transformations into account (you can even draw e.g. a rotated rectangle)
  • useRadius option is obsolete duo to the implementation of circle in svg.js v2

More Repositories

1

svg.js

The lightweight library for manipulating and animating SVG
JavaScript
11,031
star
2

svg.draggable.js

An extension for the svg.js library to make elements draggable
HTML
281
star
3

svgdom

Straightforward DOM implementation to make SVG.js run headless on Node.js
JavaScript
267
star
4

svg.filter.js

A plugin for svg.js adding svg filters
JavaScript
218
star
5

svg.import.js

A plugin for importing raw SVG into the svg.js library
JavaScript
98
star
6

svg.select.js

An extension of svg.js which allows to select an element
JavaScript
93
star
7

svg.panzoom.js

A plugin for svg.js which enables panzoom for viewbox elements
JavaScript
93
star
8

svg.resize.js

An extension of svg.js which allows to resize elements which are selected
JavaScript
90
star
9

svg.clock.js

A clock built with the svg.js library, inspired by the swiss railway clock
JavaScript
75
star
10

svg.pathmorphing.js

A plugin for the svg.js library to enable path morphing / animation
JavaScript
55
star
11

svg.easing.js

Easing equations for the fx module in the svg.js library
JavaScript
52
star
12

svg.topoly.js

A plugin for SVG.js to convert paths to polygons at a given sample rate
JavaScript
39
star
13

svg.shapes.js

A shapes plugin for the SVG.js library
JavaScript
36
star
14

svg.topath.js

A plugin for the svg.js library to convert any other shape to a path
JavaScript
29
star
15

svg.export.js

A plugin for the svg.js library to export raw svg
JavaScript
26
star
16

svg.parser.js

A helper library for svg.js providing parser functions to adopt and import existing svg's
JavaScript
17
star
17

svg.textmorph.js

A plugin for the svg.js library to enable text morphing / animation
JavaScript
16
star
18

svg.absorb.js

A plugin for the svg.js library adding the ability to absorb an existing svg into the dynamic svg.js structure
JavaScript
14
star
19

svg.screenbbox.js

A plugin for the svgjs library which gets the bbox of a path/polygon/polyline after all transformations applied.
JavaScript
14
star
20

svg.threed.js

An SVG.js plugin to visualise 3D scenes and objects
JavaScript
13
star
21

svgdotjs.github.io

Documentation and website for SVG.js
PHP
13
star
22

svg.pathhandles.js

Add handles to svg path as known in svg programs like incscape
JavaScript
8
star
23

svg.wiml.js

A plugin for the svg.js library adding support for the wiml markup language
JavaScript
3
star
24

svg.colorAt.js

An extension for svg.js which allows to pick a color from a gradint at a specific position
JavaScript
3
star
25

saucelab-configurator

A cli tool to create configurations objects for karma-saucelabs-launcher
JavaScript
1
star
26

svg.logo

The logo for svgdotjs
HTML
1
star