|
1 | | -import type { ImageSource } from './ImageSource'; |
| 1 | +import { ImageSource } from './ImageSource'; |
2 | 2 | import type { SourceView } from './Sprite'; |
3 | 3 | import { Sprite } from './Sprite'; |
4 | 4 | import type { GraphicOptions } from './Graphic'; |
@@ -178,6 +178,114 @@ export class SpriteSheet { |
178 | 178 | throw Error(`Invalid sprite coordinates (${x}, ${y})`); |
179 | 179 | } |
180 | 180 |
|
| 181 | + /** |
| 182 | + * Returns a sprite that has a new backing image the exact size of the sprite that tha is a copy of the original sprite slice. |
| 183 | + * |
| 184 | + * Useful if you need to apply effects, manipulate, or mutate the image and you don't want to disturb the original sprite sheet. |
| 185 | + * |
| 186 | + */ |
| 187 | + public async getSpriteAsStandalone(x: number, y: number): Promise<Sprite> { |
| 188 | + if (x >= this.columns || x < 0) { |
| 189 | + throw Error(`No sprite exists in the SpriteSheet at (${x}, ${y}), x: ${x} should be between 0 and ${this.columns - 1} columns`); |
| 190 | + } |
| 191 | + if (y >= this.rows || y < 0) { |
| 192 | + throw Error(`No sprite exists in the SpriteSheet at (${x}, ${y}), y: ${y} should be between 0 and ${this.rows - 1} rows`); |
| 193 | + } |
| 194 | + const spriteIndex = x + y * this.columns; |
| 195 | + const sprite = this.sprites[spriteIndex]; |
| 196 | + const cnv = document.createElement('canvas'); |
| 197 | + const ctx = cnv.getContext('2d'); |
| 198 | + cnv.width = sprite.width; |
| 199 | + cnv.height = sprite.height; |
| 200 | + |
| 201 | + if (!sprite) { |
| 202 | + throw Error(`Invalid sprite coordinates (${x}, ${y})`); |
| 203 | + } |
| 204 | + if (!ctx) { |
| 205 | + throw Error('Unable to create canvas context'); |
| 206 | + } |
| 207 | + |
| 208 | + ctx.drawImage( |
| 209 | + sprite.image.image, |
| 210 | + sprite.sourceView.x, |
| 211 | + sprite.sourceView.y, |
| 212 | + sprite.sourceView.width, |
| 213 | + sprite.sourceView.height, |
| 214 | + 0, |
| 215 | + 0, |
| 216 | + sprite.sourceView.width, |
| 217 | + sprite.sourceView.height |
| 218 | + ); |
| 219 | + |
| 220 | + const imgSrc = new ImageSource(cnv.toDataURL()); |
| 221 | + await imgSrc.load(); |
| 222 | + |
| 223 | + return new Sprite({ |
| 224 | + image: imgSrc, |
| 225 | + sourceView: { |
| 226 | + x: 0, |
| 227 | + y: 0, |
| 228 | + width: sprite.width, |
| 229 | + height: sprite.height |
| 230 | + }, |
| 231 | + destSize: { |
| 232 | + width: sprite.width, |
| 233 | + height: sprite.height |
| 234 | + } |
| 235 | + }); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Returns a new image exact size and copy of the original sprite slice. |
| 240 | + * |
| 241 | + * Useful if you need to apply effects, manipulate, or mutate the image and you don't want to disturb the original sprite sheet. |
| 242 | + */ |
| 243 | + public async getSpriteAsImage(x: number, y: number): Promise<HTMLImageElement> { |
| 244 | + if (x >= this.columns || x < 0) { |
| 245 | + throw Error(`No sprite exists in the SpriteSheet at (${x}, ${y}), x: ${x} should be between 0 and ${this.columns - 1} columns`); |
| 246 | + } |
| 247 | + if (y >= this.rows || y < 0) { |
| 248 | + throw Error(`No sprite exists in the SpriteSheet at (${x}, ${y}), y: ${y} should be between 0 and ${this.rows - 1} rows`); |
| 249 | + } |
| 250 | + const spriteIndex = x + y * this.columns; |
| 251 | + const sprite = this.sprites[spriteIndex]; |
| 252 | + const cnv = document.createElement('canvas'); |
| 253 | + const ctx = cnv.getContext('2d'); |
| 254 | + cnv.width = sprite.width; |
| 255 | + cnv.height = sprite.height; |
| 256 | + |
| 257 | + if (!sprite) { |
| 258 | + throw Error(`Invalid sprite coordinates (${x}, ${y})`); |
| 259 | + } |
| 260 | + if (!ctx) { |
| 261 | + throw Error('Unable to create canvas context'); |
| 262 | + } |
| 263 | + |
| 264 | + ctx.drawImage( |
| 265 | + sprite.image.image, |
| 266 | + sprite.sourceView.x, |
| 267 | + sprite.sourceView.y, |
| 268 | + sprite.sourceView.width, |
| 269 | + sprite.sourceView.height, |
| 270 | + 0, |
| 271 | + 0, |
| 272 | + sprite.sourceView.width, |
| 273 | + sprite.sourceView.height |
| 274 | + ); |
| 275 | + |
| 276 | + const imgSrc = new Image(sprite.width, sprite.height); |
| 277 | + imgSrc.src = cnv.toDataURL(); |
| 278 | + |
| 279 | + return await new Promise((resolve, reject) => { |
| 280 | + imgSrc.onload = () => { |
| 281 | + resolve(imgSrc); |
| 282 | + }; |
| 283 | + imgSrc.onerror = (e) => { |
| 284 | + reject(e); |
| 285 | + }; |
| 286 | + }); |
| 287 | + } |
| 288 | + |
181 | 289 | /** |
182 | 290 | * Create a sprite sheet from a sparse set of {@apilink SourceView} rectangles |
183 | 291 | * @param options |
|
0 commit comments