Created
May 16, 2018 09:42
-
-
Save mykhailo-petrenko/080e146cd52289c6d0fc389da8b18113 to your computer and use it in GitHub Desktop.
Sprite generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install -D glob spritesmith spritesheet-templates | |
const fs = require('fs'); | |
const glob = require('glob'); | |
const Spritesmith = require('spritesmith'); | |
const templater = require('spritesheet-templates'); | |
const icons = glob.sync('./src/icons/*.png'); | |
const spritePath = `${__dirname}/sprite.png`; | |
const spriteRelativePath = `/sprite.png`; | |
const scssPath = `${__dirname}/sprite.scss`; | |
Spritesmith.run({ | |
src: icons, | |
}, function(err, result) { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
const {coordinates, properties, image} = result; | |
const sprites = []; | |
Object.keys(coordinates).forEach((path) => { | |
let name = path.split('/').pop(); | |
name = name.replace(/\..*$/, ''); | |
sprites.push({ | |
...coordinates[path], | |
name | |
}); | |
}); | |
fs.writeFileSync(spritePath, image); | |
const scss = templater({ | |
sprites: sprites, | |
spritesheet: { | |
...properties, | |
image: spriteRelativePath | |
} | |
}, { | |
format: 'scss' | |
}); | |
fs.writeFileSync(scssPath, scss); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment