Skip to content

Commit

Permalink
enabled rendering of multiple sprite sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Gräfenstein committed Apr 5, 2018
1 parent 2ddd0f3 commit 380dd31
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export interface Options extends CallbackOptions {
*/
framesX?: number

/**
* Number of frames in one column of a single sprite sheet.
*/
framesY?: number

/**
* Number of sequences.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/utils/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { naturalSize } from './naturalSize'
export interface MeasureSheetOptions {
frames: number
framesX?: number
framesY?: number
detectSubsampling?: boolean
}

Expand Down Expand Up @@ -45,6 +46,8 @@ export interface SpriteSpec {
export function measure(images: HTMLImageElement[], options: MeasureSheetOptions): SheetSpec[] {
if (images.length === 1) {
return [measureSheet(images[0], options)]
} else if (options.framesX && options.framesY) {
return measureMutipleSheets(images, options)
} else {
return measureFrames(images, options)
}
Expand Down Expand Up @@ -88,6 +91,23 @@ function measureFrames(images: HTMLImageElement[], options: MeasureSheetOptions)
return result
}

function measureMutipleSheets(images: HTMLImageElement[], options: MeasureSheetOptions): SheetSpec[] {
const result: SheetSpec[] = []
for (let id = 0; id < images.length; id++) {
// TODO: optimize
// dont measure images with same size twice
const sheet = measureSheet(images[id], {
frames: undefined,
framesX: options.framesX,
framesY: options.framesY,
detectSubsampling: options.detectSubsampling
})
sheet.id = id
result.push(sheet)
}
return result
}

function measureImage(image: HTMLImageElement, options: MeasureSheetOptions, result: SheetSpec): SheetSpec {
const size = naturalSize(image)
result.isSubsampled = options.detectSubsampling && detectSubsampling(image, size.width, size.height)
Expand Down

0 comments on commit 380dd31

Please sign in to comment.