BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

image.js 3.6 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
31 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms, textures, lights)
32 {
33 if (!scene.glContext || !this.g_image)
34 { return; }
35
36 // One hypot for frustum pad (was computed 4× per sprite).
37 const pad = Math.sqrt(dw * dw + dh * dh);
38 if (dx < -pad || dy < -pad || dx > scene.real_size.x + pad || dy > scene.real_size.y + pad)
39 { return; }
40
41 let glTextures = null;
42
43 if (textures)
44 {
45 for (const name in textures)
46 {
47 if (!Object.prototype.hasOwnProperty.call(textures, name))
48 { continue; }
49
50 const src = textures[name];
51 if (!src)
52 { continue; }
53
54 if (!glTextures)
55 { glTextures = {}; }
56
57 const htmlImage = src.g_image || src;
58 glTextures[name] = scene.glContext.getTexture(htmlImage);
59 }
60 }
61
62 const opts = this._drawOpts || (this._drawOpts = {});
63 opts.texture = scene.glContext.getTexture(this.g_image);
64 opts.x = dx;
65 opts.y = dy;
66 opts.w = dw;
67 opts.h = dh;
68 opts.rotationDeg = rotation;
69 opts.mirrorX = mirrorX;
70 opts.mirrorY = mirrorY;
71 opts.alpha = alpha;
72 opts.uvRect = this.getUvRect(subImage);
73 opts.shader = shader || scene.glContext.defaultShader;
74 opts.uniforms = uniforms;
75 opts.textures = glTextures;
76 opts.lights = lights;
77 scene.glContext.drawSprite(opts);
78 }
79
80 getUvRect (subImage)
81 {
82 if (!this._uvRects)
83 { this._uvRects = []; }
84
85 const index = subImage | 0;
86 let rect = this._uvRects[index];
87 if (rect)
88 { return rect; }
89
90 const subImageData = this.getSubImage(index);
91 const imgW = this.g_image.width;
92 const imgH = this.g_image.height;
93 rect = {
94 u0: subImageData.sx / imgW,
95 v0: subImageData.sy / imgH,
96 u1: (subImageData.sx + subImageData.sw) / imgW,
97 v1: (subImageData.sy + subImageData.sh) / imgH
98 };
99 this._uvRects[index] = rect;
100 return rect;
101 }
102
103 hasAnimationCycle (label)
104 {
105 return this.animationCycles[label] != null;
106 }
107
108 addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
109 {
110 this.animationCycles[label] =
111 {
112 frames: cycle,
113 loop: loop,
114 chain: chain,
115 chainDefaultIndex: chainDefaultIndex,
116 chainSpeed: chainSpeed
117 };
118 }
119
120 get width ()
121 {
122 if (this.cacheWidth < 0)
123 { this.cacheWidth = this.g_image.width / this.subImagesPerRow; }
124
125 return this.cacheWidth;
126 }
127
128 get height ()
129 {
130 if (this.cacheHeight < 0)
131 { this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); }
132
133 return this.cacheHeight;
134 }
135
136 cacheWidth = -1;
137 cacheHeight = -1;
138
139 getSubImage (subImageIndex)
140 {
141 var subImage = Math.round(subImageIndex);
142 subImage %= this.subImages;
143
144 var c = subImage % this.subImagesPerRow;
145 var r = Math.floor(subImage / this.subImagesPerRow);
146 var sw = (this.g_image.width / this.subImagesPerRow);
147 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
148 var sx = sw * c;
149 var sy = sh * r;
150
151 return { sw: sw, sh: sh, sx: sx, sy: sy };
152 }
153}