You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

138 lines
2.3 KiB

/*!
* Canvas
* Copyright (c) 2010 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var canvas = require('../build/default/canvas')
, Canvas = canvas.Canvas
, Image = canvas.Image
, cairoVersion = canvas.cairoVersion
, Context2d = require('./context2d')
, PNGStream = require('./pngstream')
, fs = require('fs');
/**
* Export `Canvas` as the module.
*/
var Canvas = exports = module.exports = Canvas;
/**
* Library version.
*/
exports.version = '0.0.8';
/**
* Cairo version.
*/
exports.cairoVersion = cairoVersion;
/**
* Expose constructors.
*/
exports.Context2d = Context2d;
exports.PNGStream = PNGStream;
exports.Image = Image;
/**
* Context2d implementation.
*/
require('./context2d');
/**
* Image implementation.
*/
require('./image');
/**
* Inspect canvas.
*
* @return {String}
* @api public
*/
Canvas.prototype.inspect = function(){
return '[Canvas ' + this.width + ' ' + this.height + ']';
};
/**
* Get a context object.
*
* @param {String} contextId
* @return {Context2d}
* @api public
*/
Canvas.prototype.getContext = function(contextId){
if ('2d' == contextId) {
var ctx = new Context2d(this);
this.context = ctx;
ctx.canvas = this;
return ctx;
}
};
/**
* Create a `PNGStream` for `this` canvas.
*
* @return {PNGStream}
* @api public
*/
Canvas.prototype.createPNGStream = function(){
return new PNGStream(this);
};
/**
* Create a synchronous `PNGStream` for `this` canvas.
*
* @return {PNGStream}
* @api public
*/
Canvas.prototype.createSyncPNGStream = function(){
return new PNGStream(this, true);
};
/**
* Return a data url. Pass a function for async support.
*
* @param {String|Function} type
* @param {Function} fn
* @return {String}
* @api public
*/
Canvas.prototype.toDataURL = function(type, fn){
// Default to png
type = type || 'image/png';
// Allow callback as first arg
if ('function' == typeof type) fn = type, type = 'image/png';
// Throw on non-png
if ('image/png' != type) throw new Error('currently only image/png is supported');
var prefix = 'data:' + type + ';base64,';
if (fn) {
this.toBuffer(function(err, buf){
if (err) return fn(err);
var str = 'data:' + type
fn(null, prefix + buf.toString('base64'));
});
} else {
return prefix + this.toBuffer().toString('base64');
}
};