selaux / node-sprite-generator

Generates image sprites and their spritesheets (css, stylus, sass or less) from sets of images. Supports retina sprites. Provides express middleware and grunt task.
MIT License
191 stars 39 forks source link

Options.stylesheet does not accept `javascript` #78

Open elmpp opened 5 years ago

elmpp commented 5 years ago

Hi there,

I'm seeing an error when trying to output javascript styles.

I have tried this with both gm and jimp compositors.

error message:

ENOENT: no such file or directory, open 'javascript'
error Command failed with exit code 1.

config:

{
    src: [path.resolve(__dirname, './assets/spriteable/horizontal/*')],
    spritePath: path.resolve(__dirname, 'assets/__generated__/spritesheet/allbookies-hz.png'),
    stylesheetPath: path.resolve(__dirname, 'assets/__generated__/spritesheet-style/allbookies-hz.js'),
    stylesheet: 'javascript',
    compositor: 'gm',
    compositorOptions: {
      compressionLevel: 6,
    },
  }

package.json:

{
  "name": "org-image",
  "version": "0.86.0",
  "description": "image processing stuff",
  "license": "ISC",
  "main": "lib/org-image.js",
  "scripts": {
    "build": "tsc",
    "postinstall": "cd ../.. && yarn postinstall",
    "postuninstall": "cd ../.. && yarn postinstall",
    "transpile": "npm run build",
    "transpile:watch": "npm run build -- --watch",
    "type-check": "tsc --noEmit"
  },
  "devDependencies": {
    "@types/node-sprite-generator": "^0.10.1",
    "node-sprite-generator": "^0.10.2",
    "ts-node": "8.2.0"
  },
  "bin": {
    "spritesheet": "scripts/spritesheet.sh"
  }
}
navjeetfgmnt commented 4 years ago

@elmpp did you find a sol to this?

timohausmann commented 3 years ago

Same, there is no "javascript" template in this repository. I found two workarounds, these examples will create a json format, adjust the output to your likings.

Workaround 1: write custom output function

I think the easiest way is to set options.stylesheet to a custom function:

var nsg = require('node-sprite-generator');
var fs = require('fs');

nsg({
    src: [
        'faces-128/*.jpg'
    ],
    spritePath: 'faces-128/all.png',
    stylesheetPath: 'faces-128/all.json',
    stylesheet: function(layout, stylesheetPath, spritePath, options, callback) {
      const data = layout.images.map((img) => {
        const {x, y, width, height, path} = img;
        return { x, y, width, height, path }
      })

      fs.writeFileSync(stylesheetPath, JSON.stringify(data, null, 2))
    },
    compositor: 'jimp'
}, function (err) {
    console.log('Sprite generated!');
});

Workaround 2: add missing templates

You can also create the missing templates.

create new file node-sprite-generator/lib/stylesheet/javascript.js:

'use strict';

var getTemplatedStylesheet = require('./templatedStylesheet'),
    path = require('path');

module.exports = getTemplatedStylesheet(path.join(__dirname, '/templates/javascript.tpl'));

create new file node-sprite-generator/lib/stylesheet/templates/javascript.tpl:

[<% layout.images.forEach(function (image) { %>
  {
    "path": "<%= image.path %>",
    "x": <%= image.x %>,
    "y": <%= image.y %>,
    "width": <%= image.width %>,
    "height": <%= image.height %>
  }, <% }); %>
]