BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

image.js 1.7 KiB Download Raw
1class image{
2 subImages = null;
3 subImagesPerRow = null;
4 loop = false;
5 chain = null;
6
7 g_image = null;
8
9 animationCycles = {};
10
11 constructor(image, subImgTotal = 1, perRow = 1, loop = false, chain = null)
12 {
13 subImgTotal = subImgTotal || 1;
14 perRow = perRow || 1;
15
16 this.subImages = subImgTotal;
17 this.subImagesPerRow = perRow;
18 this.g_image = image;
19 this.loop = loop;
20 this.chain = chain;
21
22 image.id="loadedImage";
23 document.body.append(image);
24 console.log("Image loaded: " + image.src);
25 }
26
27 draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
28 {
29 var shouldDraw = true;
30
31 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
32 shouldDraw = false;
33
34 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
35 shouldDraw = false;
36
37 if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
38 shouldDraw = false;
39
40 if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
41 shouldDraw = false;
42
43 if(shouldDraw)
44 {
45 var context = scene.context;
46 context.save();
47 var oldAlpha = context.globalAlpha;
48 context.globalAlpha = alpha;
49
50 context.translate(dx, dy);
51 context.rotate(rotation * (Math.PI/180));
52
53 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
54
55 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
56
57 context.restore();
58 context.globalAlpha = oldAlpha;
59 }
60 }
61
62 addAnimationCycle(label, cycle)
63 {
64 this.animationCycles[label] = cycle;
65 }
66
67 get width()
68 {
69 return this.g_image.width / this.subImagesPerRow;
70 }
71
72 get height()
73 {
74 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
75 }
76}