BlueGit

timfa frameworkv3

Files

frameworkv3 / image.js

image.js

image.js 1.7 KiB Download Raw
1//<DOC> <category> Image
2//<DOC> <function> Constructor: new image(urlToImage, subImages = 1, subImagesPerRow = 1)
3function image(url, subImgTotal = 1, perRow = 1)
4{
5 //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1.
6 this.subImages = subImgTotal;
7 //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1.
8 this.subImagesPerRow = perRow;
9
10 var body = document.body;
11 body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>');
12 this.g_image = document.getElementById('image');
13 document.getElementById('image').id = "loadedImage";
14
15 //<DOC> <function> image.draw(sourceX, sourceY, sourceWidth, destX, destY, destWidth, destHeight, rotation, alpha)<br><br>Draw the image on screen.
16 this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
17 {
18 var shouldDraw = true;
19
20 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
21 shouldDraw = false;
22
23 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
24 shouldDraw = false;
25
26 if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
27 shouldDraw = false;
28
29 if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
30 shouldDraw = false;
31
32 if(shouldDraw)
33 {
34 var context = GameWindow.canvasContext;
35 context.save();
36 var oldAlpha = context.globalAlpha;
37 context.globalAlpha = alpha;
38
39 context.translate(dx, dy);
40 context.rotate(rotation * (Math.PI/180));
41
42 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
43
44 context.restore();
45 context.globalAlpha = oldAlpha;
46 }
47 }
48}