BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

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