Skip to content

Commit

Permalink
feat(examples): added NodeJS example
Browse files Browse the repository at this point in the history
  • Loading branch information
oori committed Aug 14, 2019
1 parent 550a30c commit fde0653
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,17 @@ createCanvas: (width, height) => HTMLCanvasElement

Node
---
Install with:

See `examples/nodejs`:
```shell
$ cd examples/nodejs
$ npm install
$ node index
```
This will build both PNG and HTML files into `output` directory.


Or, manually try with:
```shell
$ npm install hashicon canvas
```
Expand Down
20 changes: 20 additions & 0 deletions examples/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const hashicon = require('../../dist/hashicon.cjs.js')
const fs = require('fs');
const outputDir = __dirname + "/output";
const hashString = "test";

const { createCanvas } = require('canvas');
const canvas = hashicon(hashString, { createCanvas });

// Try canvas.toDataURL
fs.writeFile(
`${outputDir}/${hashString}.html`,
`<img src="${canvas.toDataURL()}" />`,
err => console.log(`HTML file ${err ? 'got error: ' + err : 'created'}`)
);

// Try canvas.createPNGStream
const out = fs.createWriteStream(`${outputDir}/${hashString}.png`);
const stream = canvas.createPNGStream();
stream.pipe(out);
out.on('finish', () => console.log('PNG file created'));
2 changes: 2 additions & 0 deletions examples/nodejs/output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.html
*.png
Loading

0 comments on commit fde0653

Please sign in to comment.