Browse Source

text example using streaming api

v1.x
Tj Holowaychuk 15 years ago
parent
commit
d5f8b08c6d
  1. 10
      examples/text.js
  2. 3
      lib/canvas.js
  3. 3
      lib/pngstream.js

10
examples/text.js

@ -5,7 +5,8 @@
var Canvas = require('../lib/canvas') var Canvas = require('../lib/canvas')
, canvas = new Canvas(200, 200) , canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d'); , ctx = canvas.getContext('2d')
, fs = require('fs');
ctx.globalAlpha = .2; ctx.globalAlpha = .2;
@ -32,4 +33,9 @@ ctx.strokeText("Wahoo", 50, 100);
ctx.fillStyle = '#000'; ctx.fillStyle = '#000';
ctx.fillText("Wahoo", 49, 99); ctx.fillText("Wahoo", 49, 99);
canvas.savePNG(__dirname + '/text.png'); var out = fs.createWriteStream(__dirname + '/text.png')
, stream = canvas.createPNGStream();
stream.on('data', function(chunk){
out.write(chunk);
});

3
lib/canvas.js

@ -15,7 +15,8 @@ var canvas = require('../build/default/canvas')
, Context2d = canvas.CanvasRenderingContext2d , Context2d = canvas.CanvasRenderingContext2d
, CanvasGradient = canvas.CanvasGradient , CanvasGradient = canvas.CanvasGradient
, cairoVersion = canvas.cairoVersion , cairoVersion = canvas.cairoVersion
, PNGStream = require('./pngstream'); , PNGStream = require('./pngstream')
, fs = require('fs');
/** /**
* Export `Canvas` as the module. * Export `Canvas` as the module.

3
lib/pngstream.js

@ -41,7 +41,8 @@ var PNGStream = module.exports = function PNGStream(canvas, sync) {
: 'streamPNG'; : 'streamPNG';
this.sync = sync; this.sync = sync;
this.canvas = canvas; this.canvas = canvas;
if ('streamPNG' == method) throw new Error('async png streaming not yet implemented'); // TODO: implement async
if ('streamPNG' == method) method = 'streamPNGSync';
process.nextTick(function(){ process.nextTick(function(){
canvas[method](function(err, chunk, len){ canvas[method](function(err, chunk, len){
if (err) { if (err) {

Loading…
Cancel
Save