BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / image.js

image.js

Framework 3
timfa Tim Falken · pushed by timfa · 9 years ago
4a56d325 History
image.js 2.0 KiB Download Raw
1//<DOC> <category> Image
2//<DOC> <function> Constructor: new image(urlToImage, subImages = 1, subImagesPerRow = 1)
3function image(url, subImgTotal, perRow)
4{
5 subImgTotal = subImgTotal || 1;
6 perRow = perRow || 1;
7
8 //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1.
9 this.subImages = subImgTotal;
10 //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1.
11 this.subImagesPerRow = perRow;
12
13 var body = document.body;
14 body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>');
15 this.g_image = document.getElementById('image');
16 document.getElementById('image').id = "loadedImage";
17
18 Object.defineProperty(this, "width",
19 {
20 get: function()
21 {
22 return this.g_image.width / this.subImagesPerRow;
23 }
24 });
25
26 Object.defineProperty(this, "height",
27 {
28 get: function()
29 {
30 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
31 }
32 });
33
34 //<DOC> <function> image.draw(sourceX, sourceY, sourceWidth, destX, destY, destWidth, destHeight, rotation, alpha)<br><br>Draw the image on screen.
35 this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
36 {
37 var shouldDraw = true;
38
39 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
40 shouldDraw = false;
41
42 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
43 shouldDraw = false;
44
45 if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
46 shouldDraw = false;
47
48 if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
49 shouldDraw = false;
50
51 if(shouldDraw)
52 {
53 var context = GameWindow.canvasContext;
54 context.save();
55 var oldAlpha = context.globalAlpha;
56 context.globalAlpha = alpha;
57
58 context.translate(dx, dy);
59 context.rotate(rotation * (Math.PI/180));
60
61 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
62
63 context.restore();
64 context.globalAlpha = oldAlpha;
65 }
66 }
67}