BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

image.js 3.0 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, shader, uniforms)
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 || !scene.glContext || !this.g_image)
50 { return; }
51
52 const subImageData = this.getSubImage(subImage);
53 const imgW = this.g_image.width;
54 const imgH = this.g_image.height;
55
56 scene.glContext.drawSprite({
57 texture: scene.glContext.getTexture(this.g_image),
58 x: dx,
59 y: dy,
60 w: dw,
61 h: dh,
62 rotationDeg: rotation,
63 mirrorX: mirrorX,
64 mirrorY: mirrorY,
65 alpha: alpha,
66 uvRect: {
67 u0: subImageData.sx / imgW,
68 v0: subImageData.sy / imgH,
69 u1: (subImageData.sx + subImageData.sw) / imgW,
70 v1: (subImageData.sy + subImageData.sh) / imgH
71 },
72 shader: shader || scene.glContext.defaultShader,
73 uniforms: uniforms
74 });
75 }
76
77 hasAnimationCycle (label)
78 {
79 return this.animationCycles[label] != null;
80 }
81
82 addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
83 {
84 this.animationCycles[label] =
85 {
86 frames: cycle,
87 loop: loop,
88 chain: chain,
89 chainDefaultIndex: chainDefaultIndex,
90 chainSpeed: chainSpeed
91 };
92 }
93
94 get width ()
95 {
96 if (this.cacheWidth < 0)
97 { this.cacheWidth = this.g_image.width / this.subImagesPerRow; }
98
99 return this.cacheWidth;
100 }
101
102 get height ()
103 {
104 if (this.cacheHeight < 0)
105 { this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); }
106
107 return this.cacheHeight;
108 }
109
110 cacheWidth = -1;
111 cacheHeight = -1;
112
113 getSubImage (subImageIndex)
114 {
115 var subImage = Math.round(subImageIndex);
116 subImage %= this.subImages;
117
118 var c = subImage % this.subImagesPerRow;
119 var r = Math.floor(subImage / this.subImagesPerRow);
120 var sw = (this.g_image.width / this.subImagesPerRow);
121 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
122 var sx = sw * c;
123 var sy = sh * r;
124
125 return { sw: sw, sh: sh, sx: sx, sy: sy };
126 }
127}