|
|
@ -98,17 +98,33 @@ Canvas.prototype.createSyncPNGStream = function(){ |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Return a data url. |
|
|
|
* Return a data url. Pass a function for async support. |
|
|
|
* |
|
|
|
* @param {String} type |
|
|
|
* @param {String|Function} type |
|
|
|
* @param {Function} fn |
|
|
|
* @return {String} |
|
|
|
* @api public |
|
|
|
*/ |
|
|
|
|
|
|
|
Canvas.prototype.toDataURL = function(type){ |
|
|
|
// TODO: jpeg / svg / pdf :)
|
|
|
|
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'); |
|
|
|
return 'data:' + type |
|
|
|
+ ';base64,' + this.toBuffer().toString('base64'); |
|
|
|
|
|
|
|
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'); |
|
|
|
} |
|
|
|
}; |
|
|
|