twolfson / spritesmith

Utility that takes sprites and converts them into a stylesheet and its coordinates
MIT License
916 stars 56 forks source link

options to remove extension and directory path in JSON file #84

Closed txtl closed 3 years ago

txtl commented 3 years ago

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.

https://github.com/twolfson/spritesmith/blob/03f5c3514d502296f645dfcafdec41c220eaef77/src/smith.js#L155

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,

twolfson commented 3 years ago

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

txtl commented 3 years ago

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,

twolfson commented 3 years ago

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: