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