Skip to content

Commit

Permalink
closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
aslansky committed Jan 22, 2014
1 parent 340e810 commit 9e0cedb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
20 changes: 14 additions & 6 deletions index.js
Expand Up @@ -15,7 +15,7 @@ module.exports = function(fileName, opt) {
throw new PluginError('gulp-sprite', 'Missing fileName option for gulp-sprite');
}

opt = lodash.extend({margin: 5, preprocessor: 'css', imagePath: '', prefix: '_'}, opt);
opt = lodash.extend({margin: 5, preprocessor: 'css', imagePath: '', prefix: '', orientation: 'vertical'}, opt);

var firstFile = null;
var sprites = [];
Expand All @@ -40,16 +40,24 @@ module.exports = function(fileName, opt) {
sprites.push({
'img': img,
'name': gutil.replaceExtension(file.relative, ''),
'x': opt.margin,
'y': ctxHeight + opt.margin,
'x': opt.orientation === 'vertical' ? opt.margin : ctxWidth + opt.margin,
'y': opt.orientation === 'vertical' ? ctxHeight + opt.margin: opt.margin,
'width': img.width,
'height': img.height,
'image': path.join(opt.imagePath, fileName)
});

ctxHeight = ctxHeight + img.height + 2 * opt.margin;
if (img.width + 2 * opt.margin > ctxWidth) {
ctxWidth = img.width + 2 * opt.margin;
if (opt.orientation === 'vertical') {
ctxHeight = ctxHeight + img.height + 2 * opt.margin;
if (img.width + 2 * opt.margin > ctxWidth) {
ctxWidth = img.width + 2 * opt.margin;
}
}
else {
ctxWidth = ctxWidth + img.width + 2 * opt.margin;
if (img.height + 2 * opt.margin > ctxHeight) {
ctxHeight = img.height + 2 * opt.margin;
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion readme.md
Expand Up @@ -57,7 +57,7 @@ gulp.task('sprites', function () {

### prefix

**default:** '_'
**default:** ''

**description:** The prefix for the stylesheet file

Expand All @@ -68,6 +68,13 @@ gulp.task('sprites', function () {
**description:** The output style for the stylesheets.
One of: css, less, sass, scss or stylus.

### orientation

**default:** vertical

**description:** The orientation in which the images are aligned in the sprite
On of: vertical, horizontal

### margin

**default:** 5
Expand Down
22 changes: 20 additions & 2 deletions test/main.js
Expand Up @@ -17,7 +17,7 @@ var file2 = new gutil.File({
contents: fs.readFileSync(path.join(__dirname, 'fruit-computer.png'))
});

it('should create a sprite png file', function (cb) {
it('should create a vertical sprite png file', function (cb) {
var stream = sprite('sprites.png');

stream.on('data', function (file) {
Expand All @@ -34,11 +34,29 @@ it('should create a sprite png file', function (cb) {
stream.end();
});

it('should create a horizontal sprite png file', function (cb) {
var stream = sprite('sprites.png', {
orientation: 'horizontal'
});

stream.on('data', function (file) {
var img = new Image();
img.src = file.contents;
assert.equal(file.relative, 'sprites.png');
assert.equal(img.width, 110);
assert.equal(img.height, 69);
cb();
});

stream.write(file1);
stream.write(file2);
stream.end();
});

it('should create a stylesheet file', function (cb) {
var stream = sprite('sprites.png', {
imagePath: '../img',
cssPath: './test',
prefix: '',
preprocessor: 'css'
});

Expand Down

0 comments on commit 9e0cedb

Please sign in to comment.