🎉 JointJS has new documentation! ðŸ¥³

JointJS+ Raster

format.Raster

This plugin adds new methods for exporting paper to PNG and JPEG raster formats to the joint.format namespace:

toPNG(paper, callback[, options]) Executes the callback function with a PNG data URI representing the content of the paper as the first parameter. The second parameter of the callback is an optional error in the case something happened during the processing.
toJPEG(paper, callback[, options]) Executes the callback function with a JPEG data URI string representing the content of the paper as the first parameter. The second parameter of the callback is an optional error in the case something happened during the processing.
toDataURL(paper, callback[, options]) The general version of two previous methods. Executes the callback function with a data URI string representing the content of the paper in specified MIME type as the first parameter. The second parameter of the callback is an optional error in the case something happened during the processing.
toCanvas(paper, callback[, options]) Executes the callback function with a HTMLCanvasElement of applicable parameters from the paper as the first parameter. The second parameter of the callback is an optional error in the case something happened during the processing.

The available options are:

width number Set the width of the image in pixels.
height number Set the height of the image in pixels.
size number Size multiplier for generating raster images in a higher resolution. It expects a string in the form of [Number] + 'x' describing how many times the image width and height should be multiplied. It defaults to '1x'.
backgroundColor string The image background color.
padding number | object The space between the image border and the image content. It can also be an object with left, top, right and bottom properties.
type string The standard MIME type for the image format to return (only applies to toDataURL()).
quality number The quality level of a JPEG image compression in the range of 0.0 to 1.0 (only applies to toJPEG()).
area g.Rect An area which should be displayed in the resulting raster image. The value is an object with x, y, width and height properties, describing the origin and size of a rectangular area on the paper in the local coordinates. It defaults to the paper content bounding box - paper.getContentBBox() transformed into the local coordinates. This is the same behaviour as area in paper.prototype.toSVG().
// Export selected elements only and add a padding
var padding = 20;
joint.format.toPNG(paper, callback, {
  area: graph.getCellsBBox(selection.collection.toArray()).inflate(padding)
});
useComputedStyles boolean The same behaviour as useComputedStyles in paper.prototype.toSVG().
stylesheet string The same behaviour as stylesheet in paper.prototype.toSVG().
beforeSerialize(SVGDocument, paper) function The same behaviour as beforeSerialize in paper.prototype.toSVG().
fillFormControls boolean The same behaviour as fillFormControls in paper.prototype.toSVG().
grid boolean When set to true, the grid is rendered in the export. It defaults to false.

Note an exception 'dia.Paper: raster size exceeded' can be thrown if the raster size reaches the browser limits. In that case the raster size / resolution can be lowered through the size option e.g { size: '.5x' }.

Installation

Include joint.format.svg.js and joint.format.raster.js file into your HTML:

<script src="joint.format.raster.js"></script>

(Note that this plugin requires the SVG Export.)

Usage

joint.format.toPNG(paper, function(imageData) { sendToServer(imageData); });

joint.format.toJPEG(paper, function(imageData) { offerForDownload(imageData); }, {
    width: 640,
    height: 320,
    quality: 0.7
});