Skip to content

Commit

Permalink
Added new example
Browse files Browse the repository at this point in the history
  • Loading branch information
twolfson committed Nov 22, 2015
1 parent a41ad2e commit 5a42c58
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Spritesmith.run({src: sprites}, function handleResult (err, result) { /* ... */

```js
// Load in dependencies
var Spritesmtih = require('spritesmith');
var Spritesmith = require('spritesmith');

// Generate our spritesheet
var sprites = ['fork.png', 'github.png', 'twitter.png'];
Expand All @@ -99,6 +99,28 @@ Spritesmith.run({src: sprites}, function handleResult (err, result) {
});
```

### Usage with streaming output
We support streaming output by breaking down `run` into 2 parts:

```js
// Load in dependencies
var Spritesmith = require('spritesmith');

// Create a new spritesmith and process our images
var sprites = ['fork.png', 'github.png', 'twitter.png'];
var spritesmith = new Spritesmith();
spritesmith.createImages(sprites, function handleImages (err, images) {
images[0].width; // Width of image
images[0].height; // Height of image

// Create our result
var result = spritesmith.processImages(images);
result.image; // Readable stream outputting image
result.coordinates; // Object mapping filename to {x, y, width, height} of image
result.properties; // Object with metadata about spritesheet {width, height}
});
```

## Documentation
`spritesmith` exports a `Spritesmith` constructor as its `module.exports`.

Expand Down
30 changes: 30 additions & 0 deletions docs/getting-started-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Load in dependencies
var fs = require('fs');
var Spritesmith = require('../');

// Generate our spritesheet
var spritesmith = new Spritesmith();
spritesmith.createImages({
src: [
__dirname + '/fork.png',
__dirname + '/github.png',
__dirname + '/twitter.png'
]
}, function handleImages (err, images) {
// If there was an error, throw it
if (err) {
throw err;
}

// Otherwise, log info
console.log(images[0].width); // Width of image
console.log(images[0].height); // Height of image

// Create our result
var result = spritesmith.processImages(images);
console.log(JSON.stringify(result.coordinates, null, 2)); // Object mapping filename to {x, y, width, height} of image
console.log(JSON.stringify(result.properties, null, 2)); // Object with metadata about spritesheet {width, height}

// and output the image
result.image.pipe(fs.createWriteStream(__dirname + '/getting-started-stream.png'));
});
4 changes: 2 additions & 2 deletions docs/spritesheet.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Load in dependencies
var fs = require('fs');
var spritesmith = require('../');
var Spritesmith = require('../');

// Generate our spritesheet
spritesmith({
Spritesmith.run({
src: [
__dirname + '/fork.png',
__dirname + '/github.png',
Expand Down
1 change: 1 addition & 0 deletions src/smith.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Spritesmith.prototype = {
},
processImages: function (images, options) {
// Set up our algorithm/layout placement and export configuration
options = options || {};
var algorithmName = options.algorithm || algorithmDefault;
var layer = new Layout(algorithmName, options.algorithmOpts);
var padding = options.padding || 0;
Expand Down

0 comments on commit 5a42c58

Please sign in to comment.