-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new example: fetching svg from wikipedia and return png
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
require.paths.unshift(__dirname+'/../deps/node-canvas/lib'); | ||
require.paths.unshift(__dirname+'/../deps/node-htmlparser/lib'); | ||
|
||
var fs = require('fs'), | ||
sys = require('sys'), | ||
http = require('http'), | ||
CanvasSvg = require('../lib/canvas-svg'); | ||
|
||
CanvasSvg.load.debug = true; | ||
|
||
CanvasSvg.load(function (err) { | ||
if (err) throw err; | ||
|
||
var svg = ""; | ||
var wikimedia = http.createClient(80, 'upload.wikimedia.org'); | ||
var request = wikimedia.request("/wikipedia/commons/a/a2/ECGbasic.svg", | ||
{'host':'upload.wikimedia.org'}); | ||
request.end(); | ||
request.on('response', function (response) { | ||
console.log('STATUS: ' + response.statusCode); | ||
console.log('HEADERS: ' + JSON.stringify(response.headers)); | ||
response.setEncoding('utf8'); | ||
response.on('data', function (chunk) {svg += chunk;}); | ||
response.on('end', function () { | ||
console.log("data fetched"); | ||
CanvasSvg.svg.render(svg, function (err, canvas) { | ||
console.log("image rendered"); | ||
if (err) throw err; | ||
var out = fs.createWriteStream('wikipedia.png'), | ||
png = canvas.createPNGStream(); | ||
sys.pump(png, out); | ||
png.on('end', function () { | ||
out.end(); | ||
}); | ||
out.on('close', function () { | ||
console.log("png saved"); | ||
process.exit(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
|