Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sprite.js: 增加可选的压缩的参数 #16

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions tasks/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ module.exports = function (grunt) {
algorithm: 'binary-tree',
// 默认使用`pngsmith`图像处理引擎
engine: 'pngsmith',
// 是否压缩单位
cssmin: true,

// 扩展参数,不建议修改,image-set 模板,占位文件
IMAGE_SET_CSS_TMPL: IMAGE_SET_CSS_TMPL,
Expand Down Expand Up @@ -205,11 +207,24 @@ module.exports = function (grunt) {
css = css.replace(cssItem.imgPath, sliceData.spriteImg);

// Add a semicolon if needed
if(!rsemicolon.test(css)) {
css += ';';
}
css += IMAGE_SET_PLACE;
css += 'background-position:-'+ coord.x +'px -'+ coord.y +'px;';
if(!rsemicolon.test(css)) {
css += ';';
}
css += IMAGE_SET_PLACE;

if (options.cssmin) {
var x, y, bgX, bgY;

x = !coord.x ? 0 : -coord.x;
y = !coord.y ? 0 : -coord.y;

bgX = x == 0 ? 0 : (x + 'px');
bgY = y == 0 ? 0 : (y + 'px');

css += 'background-position:'+ bgX + ' ' + bgY;
} else {
css += 'background-position:-'+ coord.x +'px -'+ coord.y +'px;';
}

cssItem.newCss = css;
cssItem.height = coord.height;
Expand Down Expand Up @@ -384,4 +399,4 @@ module.exports = function (grunt) {
done(ret);
});
});
};
};