BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

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