BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

image.js 2.9 KiB Download Raw
1class image
2{
3 subImages = null;
4 subImagesPerRow = null;
5 loop = false;
6 chain = null;
7
8 g_image = null;
9
10 animationCycles = {};
11
12 constructor (image, subImgTotal = 1, perRow = 1, loop = false, chain = null)
13 {
14 subImgTotal = subImgTotal || 1;
15 perRow = perRow || 1;
16
17 this.subImages = subImgTotal;
18 this.subImagesPerRow = perRow;
19 this.g_image = image;
20 this.loop = loop;
21 this.chain = chain;
22
23 this.cacheWidth = -1;
24 this.cacheHeight = -1;
25
26 // Keep the Image out of the DOM — canvas can draw from it directly.
27 if (image && image.style)
28 { image.style.display = "none"; }
29
30 console.log("Image loaded: " + (image && image.src ? image.src : ""));
31 }
32
33 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
34 {
35 var shouldDraw = true;
36
37 if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
38 {shouldDraw = false;}
39
40 if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
41 {shouldDraw = false;}
42
43 if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
44 {shouldDraw = false;}
45
46 if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
47 {shouldDraw = false;}
48
49 if (shouldDraw)
50 {
51 var context = scene.context;
52 context.save();
53 var oldAlpha = context.globalAlpha;
54 context.globalAlpha = alpha;
55
56 context.translate(dx, dy);
57 context.rotate(rotation * (Math.PI / 180));
58
59 (mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);
60
61 var subImageData = this.getSubImage(subImage);
62
63 context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);
64
65 context.restore();
66 context.globalAlpha = oldAlpha;
67 }
68 }
69
70 hasAnimationCycle (label)
71 {
72 return this.animationCycles[label] != null;
73 }
74
75 addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
76 {
77 this.animationCycles[label] =
78 {
79 frames: cycle,
80 loop: loop,
81 chain: chain,
82 chainDefaultIndex: chainDefaultIndex,
83 chainSpeed: chainSpeed
84 };
85 }
86
87 get width ()
88 {
89 if (this.cacheWidth < 0)
90 {this.cacheWidth = this.g_image.width / this.subImagesPerRow;}
91
92 return this.cacheWidth;
93 }
94
95 get height ()
96 {
97 if (this.cacheHeight < 0)
98 {this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);}
99
100 return this.cacheHeight;
101 }
102
103 cacheWidth = -1;
104 cacheHeight = -1;
105
106 getSubImage (subImageIndex)
107 {
108 var subImage = Math.round(subImageIndex);
109 subImage %= this.subImages;
110
111 var c = subImage % this.subImagesPerRow;
112 var r = Math.floor(subImage / this.subImagesPerRow);
113 var sw = (this.g_image.width / this.subImagesPerRow);
114 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
115 var sx = sw * c;
116 var sy = sh * r;
117
118 return { sw: sw, sh: sh, sx: sx, sy: sy };
119 }
120}