| 1 | class image{ |
| 2 | subImages = null; |
| 3 | subImagesPerRow = null; |
| 4 | loop = false; |
| 5 | chain = null; |
| 6 | |
| 7 | g_image = null; |
| 8 | |
| 9 | animationCycles = {}; |
| 10 | |
| 11 | constructor(image, subImgTotal = 1, perRow = 1, loop = false, chain = null) |
| 12 | { |
| 13 | subImgTotal = subImgTotal || 1; |
| 14 | perRow = perRow || 1; |
| 15 | |
| 16 | this.subImages = subImgTotal; |
| 17 | this.subImagesPerRow = perRow; |
| 18 | this.g_image = image; |
| 19 | this.loop = loop; |
| 20 | this.chain = chain; |
| 21 | |
| 22 | image.id="loadedImage"; |
| 23 | document.body.append(image); |
| 24 | console.log("Image loaded: " + image.src); |
| 25 | } |
| 26 | |
| 27 | draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha) |
| 28 | { |
| 29 | var shouldDraw = true; |
| 30 | |
| 31 | if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 32 | shouldDraw = false; |
| 33 | |
| 34 | if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 35 | shouldDraw = false; |
| 36 | |
| 37 | if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 38 | shouldDraw = false; |
| 39 | |
| 40 | if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 41 | shouldDraw = false; |
| 42 | |
| 43 | if(shouldDraw) |
| 44 | { |
| 45 | var context = scene.context; |
| 46 | context.save(); |
| 47 | var oldAlpha = context.globalAlpha; |
| 48 | context.globalAlpha = alpha; |
| 49 | |
| 50 | context.translate(dx, dy); |
| 51 | context.rotate(rotation * (Math.PI/180)); |
| 52 | |
| 53 | (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1); |
| 54 | |
| 55 | context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh); |
| 56 | |
| 57 | context.restore(); |
| 58 | context.globalAlpha = oldAlpha; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | addAnimationCycle(label, cycle, loop, chain, chainDefaultIndex, chainSpeed) |
| 63 | { |
| 64 | this.animationCycles[label] = |
| 65 | { |
| 66 | frames: cycle, |
| 67 | loop: loop, |
| 68 | chain: chain, |
| 69 | chainDefaultIndex: chainDefaultIndex, |
| 70 | chainSpeed: chainSpeed |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | get width() |
| 75 | { |
| 76 | return this.g_image.width / this.subImagesPerRow; |
| 77 | } |
| 78 | |
| 79 | get height() |
| 80 | { |
| 81 | return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); |
| 82 | } |
| 83 | } |