BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

preloader for images
timfa wsadhelios · pushed by timfa · 4 years ago
89239de6 History
image.js 1.4 KiB Download Raw
1function image(image, subImgTotal, perRow)
2{
3 subImgTotal = subImgTotal || 1;
4 perRow = perRow || 1;
5
6 this.subImages = subImgTotal;
7 this.subImagesPerRow = perRow;
8 this.g_image = image;
9 image.id="loadedImage";
10 document.body.append(image);
11
12 Object.defineProperty(this, "width",
13 {
14 get: function()
15 {
16 return this.g_image.width / this.subImagesPerRow;
17 }
18 });
19
20 Object.defineProperty(this, "height",
21 {
22 get: function()
23 {
24 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
25 }
26 });
27
28 this.draw = function(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
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 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
55
56 context.restore();
57 context.globalAlpha = oldAlpha;
58 }
59 }
60
61 console.log("Image loaded: " + image.src);
62}