twolfson/spritesmith

options to remove extension and directory path in JSON file

txtl opened this issue · 3 comments

txtl commented

It'd be great if there's an option to remove the directory path and file extension in the JSON file. I modified it for my purpose, but it'd be great to have that option in the official release so that I don't have to maintain it in a fork branch.

var name = img._filepath;

var path = require('path');
.....
...


processImages: function (images, options) {
    // Set up our algorithm/layout placement and export configuration
    options = options || {};
    var algorithmName = options.algorithm || algorithmDefault;
    **var extension = options.ext || false;**

    packedItems.forEach(function (item) {
      var meta = item.meta;
      var img = meta.img;
      **var name;
      if (!extension) {
         name = path.basename(img._filepath, path.extname(img._filepath));
      } else {
         name = img._filepath;
      }**
      coordinates[name] = {
        x: item.x,
        y: item.y,
        width: meta.actualWidth,
        height: meta.actualHeight
      };

.....

Thanks,

I'm not following your use case. Can you rephrase what your setup is and what you're trying to do?

txtl commented

For example, I have a workflow to convert a proprietary image format, represented by a name, to SVG and then PNG to generate the sprite sheet. During the workflow, we need to store the PNP images in a directory to use with this library. The JSON file generated by spritesmit uses the file names including the path as the keys for each frame, while our workflow needs to use the original name as the key to extracting the images from the sprite sheet because the frontend codes do not know the directory where the PNG files were stored. That's why we need to strip off the directory path and the extension to get the original name of the image.

Basically, it's a feature that TextTure Packer or Free Texture Packer supports, which I think is useful in many cases.

Thanks,

Okay, to paraphrase: you want to read in images files but change the filepath the result.coordinates references

spritesmith doesn't save any files to disk so any manipulation can be done before/after spritesmith for that scrubbing

Here are 2 ways to accomplish it: