Skip to content

Instantly share code, notes, and snippets.

@hirorock
Last active January 18, 2016 12:02
Show Gist options
  • Select an option

  • Save hirorock/3bf2b663d1fd7a3ea24f to your computer and use it in GitHub Desktop.

Select an option

Save hirorock/3bf2b663d1fd7a3ea24f to your computer and use it in GitHub Desktop.
gulp.spritesmithを使う時の実装メモ
var gulp = require('gulp'),
plumber = require('gulp-plumber'),//エラー発生時もタスクを継続する
notify = require('gulp-notify'),//エラー通知
using = require('gulp-using'),//
spritesmith = require('gulp.spritesmith');
gulp.task('default', function () {
var spriteData = gulp.src('src/images/sprite/**/*.png')
.pipe(plumber({
errorHandler: notify.onError("Error: <%= error.message %>") //<-
}))
.pipe(using())
.pipe(spritesmith({
imgName: 'sprite.png',
imgPath: '/assets/images/sprite.png',
algorithm: 'top-down',
cssOpts: {
//functions: false, // 出力されるScss内にMixinを作成させない。cssTemplateを使用時は無視される
},
cssTemplate: 'gulpfile.spritesmith_template.txt', // 出力されるSCSSのテンプレート
algorithm: 'top-down', // https://github.com/twolfson/gulp.spritesmith#algorithms
padding: 50,
cssName: '_sprite.scss'
}));
spriteData.css.pipe(gulp.dest('src/sass/'));
spriteData.img.pipe(gulp.dest('dest/assets/images/'));
});
{{#items}}
${{name}}_x : {{#if x}}{{px.x}}{{else}}0{{/if}};
${{name}}_y : {{#if y}}{{px.y}}{{else}}0{{/if}};
${{name}}_offset_x : {{#if x}}{{px.offset_x}}{{else}}0{{/if}};
${{name}}_offset_y : {{#if y}}{{px.offset_y}}{{else}}0{{/if}};
${{name}}_width : {{px.width}};
${{name}}_height : {{px.height}};
${{name}}_total_width : {{px.total_width}};
${{name}}_total_height : {{px.total_height}};
${{name}}_image : '{{{escaped_image}}}';
${{name}} : {{#if x}}{{px.x}}{{else}}0{{/if}} {{#if y}}{{px.y}}{{else}}0{{/if}} {{#if x}}{{px.offset_x}}{{else}}0{{/if}} {{#if y}}{{px.offset_y}}{{else}}0{{/if}} {{px.width}} {{px.height}} {{px.total_width}} {{px.total_height}} '{{{escaped_image}}}';
{{/items}}
/*
The provided mixins are intended to be used with the array-like variables
.icon-email {
@include sprite($icon-email);
}
.icon-email {
@include r-sprite($icon-email);
}
*/
@mixin sprite-width($sprite) {
width: nth($sprite, 5);
}
@mixin sprite-height($sprite) {
height: nth($sprite, 6);
}
@mixin sprite-position($sprite) {
$sprite-offset-x: nth($sprite, 3);
$sprite-offset-y: nth($sprite, 4);
background-position: $sprite-offset-x $sprite-offset-y;
}
@mixin sprite-image($sprite) {
$sprite-image: nth($sprite, 9);
background-image: url(#{$sprite-image});
}
@mixin sprite($sprite) {
@include sprite-image($sprite);
@include sprite-position($sprite);
@include sprite-width($sprite);
@include sprite-height($sprite);
}
@mixin r-sprite-width($sprite) {
width: (nth($sprite, 5) * .5);
}
@mixin r-sprite-height($sprite) {
height: (nth($sprite, 6) * .5);
}
@mixin r-sprite-position($sprite) {
$sprite-offset-x: (nth($sprite, 3) * .5);
$sprite-offset-y: (nth($sprite, 4) * .5);
background-position: $sprite-offset-x $sprite-offset-y;
}
@mixin r-sprite-size($sprite) {
background-size: (nth($sprite, 7) *.5) (nth($sprite, 8) *.5);
}
@mixin r-sprite($sprite) {
@include sprite-image($sprite);
@include r-sprite-position($sprite);
@include r-sprite-width($sprite);
@include r-sprite-height($sprite);
@include r-sprite-size($sprite);
}
/*
The `sprites` mixin generates identical output to the CSS template
but can be overridden inside of SCSS
@include sprites($spritesheet-sprites);
*/
@mixin sprites($sprites) {
@each $sprite in $sprites {
$sprite-name: nth($sprite, 10);
.#{$sprite-name} {
@include sprite($sprite);
}
.retina-#{$sprite-name} {
@include r-sprite($sprite);
}
}
}
{
"private": "true",
"name": "gulp_test_spritesmith",
"version": "0.0.0",
"main": "gulpfile.js",
"scripts": {
"gulp": "gulp"
},
"author": "",
"license": "ISC",
"devDependencies": {
"gulp": "^3.8.11",
"gulp-notify": "^2.2.0",
"gulp-plumber": "^1.0.1",
"gulp-sass": "^2.0.4",
"gulp-using": "^0.1.0",
"gulp.spritesmith": "^6.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment