class image{ subImages = null; subImagesPerRow = null; loop = false; chain = null; g_image = null; animationCycles = {}; constructor(image, subImgTotal = 1, perRow = 1, loop = false, chain = null) { subImgTotal = subImgTotal || 1; perRow = perRow || 1; this.subImages = subImgTotal; this.subImagesPerRow = perRow; this.g_image = image; this.loop = loop; this.chain = chain; image.id="loadedImage"; document.body.append(image); console.log("Image loaded: " + image.src); } draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha) { var shouldDraw = true; if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(shouldDraw) { var context = scene.context; context.save(); var oldAlpha = context.globalAlpha; context.globalAlpha = alpha; context.translate(dx, dy); context.rotate(rotation * (Math.PI/180)); (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1); context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh); context.restore(); context.globalAlpha = oldAlpha; } } addAnimationCycle(label, cycle, loop, chain, chainDefaultIndex, chainSpeed) { this.animationCycles[label] = { frames: cycle, loop: loop, chain: chain, chainDefaultIndex: chainDefaultIndex, chainSpeed: chainSpeed } } get width() { return this.g_image.width / this.subImagesPerRow; } get height() { return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); } }