| 1 | function image(url, subImgTotal, perRow) |
| 2 | { |
| 3 | subImgTotal = subImgTotal || 1; |
| 4 | perRow = perRow || 1; |
| 5 | |
| 6 | this.subImages = subImgTotal; |
| 7 | this.subImagesPerRow = perRow; |
| 8 | |
| 9 | var body = document.body; |
| 10 | body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>'); |
| 11 | this.g_image = document.getElementById('image'); |
| 12 | document.getElementById('image').id = "loadedImage"; |
| 13 | |
| 14 | Object.defineProperty(this, "width", |
| 15 | { |
| 16 | get: function() |
| 17 | { |
| 18 | return this.g_image.width / this.subImagesPerRow; |
| 19 | } |
| 20 | }); |
| 21 | |
| 22 | Object.defineProperty(this, "height", |
| 23 | { |
| 24 | get: function() |
| 25 | { |
| 26 | return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); |
| 27 | } |
| 28 | }); |
| 29 | |
| 30 | this.draw = function(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha) |
| 31 | { |
| 32 | var shouldDraw = true; |
| 33 | |
| 34 | if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 35 | shouldDraw = false; |
| 36 | |
| 37 | if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 38 | shouldDraw = false; |
| 39 | |
| 40 | if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 41 | shouldDraw = false; |
| 42 | |
| 43 | if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 44 | shouldDraw = false; |
| 45 | |
| 46 | if(shouldDraw) |
| 47 | { |
| 48 | var context = scene.context; |
| 49 | context.save(); |
| 50 | var oldAlpha = context.globalAlpha; |
| 51 | context.globalAlpha = alpha; |
| 52 | |
| 53 | context.translate(dx, dy); |
| 54 | context.rotate(rotation * (Math.PI/180)); |
| 55 | |
| 56 | context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh); |
| 57 | |
| 58 | context.restore(); |
| 59 | context.globalAlpha = oldAlpha; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | console.log("Image loaded: " + url); |
| 64 | } |