Sprite sheets

A sprite sheet is a single image that contains many smaller images, called sprites. They’re common in games and on the web, where loading one large image can be faster than loading lots of small ones.

In Skala, a build task can pack everything assigned to its build group into a sprite sheet. Open the build settings via File › Build Settings (⇧⌘B), select a build task, and set Export Options to Sprite Sheet. Every layer, group, and page assigned to the build group becomes a sprite on the same sheet. Sprite sheets use the image format and color settings of the build task, just like any other export.

Further reading: Build settings

Sprites #

Each layer or group sprite is cropped to its visible content, with the padding from the build tab of the properties panel added around it. If the slice type is set to Canvas, the sprite is the full size of the document instead. Page sprites are always the full size of the document. Slices are ignored for sprite sheets, and sprites are never rotated.

Layers with nothing visible are left out.

Further reading: Slicing and padding

File names #

Sprite sheets are named after the build group, rather than the layers inside them, with -sprites added to the end. The build task’s name transform, prefix, and suffix are applied too. For example, a build group called “Meow” with a kebab-case name transform and a PNG format exports meow-sprites.png.

If there are more sprites than fit on one sheet, the sheets are numbered — meow-sprites-1.png, meow-sprites-2.png, and so on. Skala never deletes files, so if a later build needs fewer sheets, the numbered files from earlier builds are left in the export folder. It’s worth clearing the folder out if the number of sheets changes.

The same document always creates exactly the same sprite sheets and listing, so they’re friendly to version control.

Size and sizing #

The Sizing popup menu decides how large each sprite sheet is. Sprite sheets are never larger than 16384 pixels on a side.

  • Manual uses the Width and Height you enter. If the sprites don’t fit on one sheet, more sheets are created.
  • Best Fit makes one sheet, cropped to fit the sprites.
  • Power-of-two makes one sheet, with a size that’s a power of two, like 512×512 or 1024×512. The sheet is either square, or one side is twice as long as the other. Some older graphics hardware and game engines need textures to be sized this way.

Width and Height are only used when Sizing is set to Manual. They’re in pixels of the final sprite sheet. A sprite that is larger than the sheet is left out, and the build reports an error. The other sprites are still packed.

Padding #

The Padding field sets the space between sprites, and between the sprites and the edges of the sheet. It is in pixels of the final sprite sheet, and it is rounded to a whole pixel.

This is different from the padding set in the build tab of the properties panel, which is part of each sprite. Padding around sprites can help avoid neighbouring sprites bleeding into each other when the sheet is scaled or filtered.

Packing and alignment #

The Alignment popup menu decides how sprites are arranged.

  • Automatic packs sprites as tightly as possible.
  • Grid places sprites in a grid, filling rows from left to right and top to bottom. Every cell is the size of the largest sprite, plus the padding, and each sprite sits in the top left of its cell.

The Packing popup menu decides the order sprites are placed in. Sprites that are the same size keep their document order.

  • Optimal tries several orders, and keeps the one with the fewest sheets, then the smallest area. With Grid alignment, this is document order.
  • Increasing Size places sprites from smallest to largest.
  • Decreasing Size places sprites from largest to smallest.

If you’re using JPEG for a sprite sheet, any area that isn’t covered by a sprite is black, because JPEG doesn’t support transparency.

Listing files #

A listing file describes where each sprite is on the sheet, so your code can find them. Choose a format from the Listing popup menu. The options are Raw Text, CSS, XML, JSON, and No Listing.

There is one listing file for all the sheets of a build task. It is named like the sheets, with the listing format’s file extension, for example meow-sprites.json.

Every rectangle in a listing is in pixels of the sprite sheet, measured from the top left. Each rectangle covers the sprite, including the padding from the build tab of the properties panel, but not the padding of the sprite sheet. Sprites are listed in document order. Each sprite’s name is its export name, or its layer name if the export name is empty, with the build task’s name transform applied. If two sprites end up with the same name, both are listed, and the build reports a warning.

JSON listings (.json) look like this.

{
   "version": 1,
   "scale": 1,
   "sheets": [
      {
         "image": "meow-sprites.png",
         "size": { "width": 512, "height": 512 },
         "sprites": [
            { "name": "cat-idle", "x": 2, "y": 2, "width": 64, "height": 48 }
         ]
      }
   ]
}

XML listings (.xml) contain the same information.

<?xml version="1.0" encoding="utf-8"?>
<sprites version="1" scale="1">
   <sheet image="meow-sprites.png" width="512" height="512">
      <sprite name="cat-idle" x="2" y="2" width="64" height="48"/>
   </sheet>
</sprites>

CSS listings (.css) have a class for each sprite, which sets the sprite sheet as the background, offset to show the right sprite. The class name is the sprite name, with any character other than letters, numbers, hyphens, and underscores replaced by a hyphen. Names that start with a number get an underscore added to the start. A comment at the top of the file notes the scale.

/* Sprite sheet listing, version 1. The scale is 1. The values are sheet pixels. */
.cat-idle { background: url("meow-sprites.png") -2px -2px no-repeat; width: 64px; height: 48px; }

Raw text listings (.txt) have one sprite per line, with tab separated columns for the sheet file name, sprite name, x, y, width, and height. There is no header line.

meow-sprites.png	cat-idle	2	2	64	48