Skip to content

Instantly share code, notes, and snippets.

@fabien-d
Created October 17, 2014 15:31
Show Gist options
  • Save fabien-d/b94118e63b817ed9859b to your computer and use it in GitHub Desktop.
Save fabien-d/b94118e63b817ed9859b to your computer and use it in GitHub Desktop.
Fabric and Node Canvas Rendering Tests
var fs = require( 'fs' );
var fabric = require( 'fabric' ).fabric;
var out = fs.createWriteStream( __dirname + '/node-canvas-fabric.png' );
var canvas = fabric.createCanvasForNode( 500, 500 );
canvas.add( new fabric.Text( 'Lorem ipsum', {
fontFamily: 'sans-serif',
fontSize: 50,
left: 250,
top: 250,
originX: 'center',
originY: 'bottom',
textAlign: 'center'
} ) );
canvas.add( new fabric.Line( [ 0, 250, 500, 250 ], {
stroke: '#000',
strokeWidth: 1
} ) );
canvas.add( new fabric.Line( [ 250, 0, 250, 500 ], {
stroke: '#000',
strokeWidth: 1
} ) );
var stream = canvas.createPNGStream();
stream.on( 'data', function ( chunk ) {
out.write( chunk );
} );
var Canvas = require( 'canvas' );
var canvas = new Canvas( 500, 500 )
var ctx = canvas.getContext( '2d' );
ctx.font = '50px sans-serif';
ctx.textAlign = 'center';
ctx.fillText( 'Lorem ipsum', 250, 250 );
ctx.beginPath();
ctx.moveTo( 250, 0) ;
ctx.lineTo( 250, 500 );
ctx.lineWidth = 1;
ctx.stroke();
ctx.beginPath();
ctx.moveTo( 0, 250 );
ctx.lineTo( 500, 250 );
ctx.lineWidth = 1;
ctx.stroke();
var fs = require( 'fs' );
var out = fs.createWriteStream( __dirname + '/node-canvas.png' );
var stream = canvas.pngStream();
stream.on( 'data', function ( chunk ) {
out.write( chunk );
} );
stream.on( 'end', function () {
console.log( 'saved png' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment