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; this.cacheWidth = -1; this.cacheHeight = -1; // Keep the Image out of the DOM — canvas can draw from it directly. if (image && image.style) { image.style.display = "none"; } console.log("Image loaded: " + (image && image.src ? image.src : "")); } draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms, textures, lights) { 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 || !scene.glContext || !this.g_image) { return; } const subImageData = this.getSubImage(subImage); const imgW = this.g_image.width; const imgH = this.g_image.height; const glTextures = {}; if (textures) { for (const name in textures) { if (!Object.prototype.hasOwnProperty.call(textures, name)) { continue; } const src = textures[name]; if (!src) { continue; } const htmlImage = src.g_image || src; glTextures[name] = scene.glContext.getTexture(htmlImage); } } scene.glContext.drawSprite({ texture: scene.glContext.getTexture(this.g_image), x: dx, y: dy, w: dw, h: dh, rotationDeg: rotation, mirrorX: mirrorX, mirrorY: mirrorY, alpha: alpha, uvRect: { u0: subImageData.sx / imgW, v0: subImageData.sy / imgH, u1: (subImageData.sx + subImageData.sw) / imgW, v1: (subImageData.sy + subImageData.sh) / imgH }, shader: shader || scene.glContext.defaultShader, uniforms: uniforms, textures: glTextures, lights: lights }); } hasAnimationCycle (label) { return this.animationCycles[label] != null; } addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed) { this.animationCycles[label] = { frames: cycle, loop: loop, chain: chain, chainDefaultIndex: chainDefaultIndex, chainSpeed: chainSpeed }; } get width () { if (this.cacheWidth < 0) { this.cacheWidth = this.g_image.width / this.subImagesPerRow; } return this.cacheWidth; } get height () { if (this.cacheHeight < 0) { this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); } return this.cacheHeight; } cacheWidth = -1; cacheHeight = -1; getSubImage (subImageIndex) { var subImage = Math.round(subImageIndex); subImage %= this.subImages; var c = subImage % this.subImagesPerRow; var r = Math.floor(subImage / this.subImagesPerRow); var sw = (this.g_image.width / this.subImagesPerRow); var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow)); var sx = sw * c; var sy = sh * r; return { sw: sw, sh: sh, sx: sx, sy: sy }; } }