BlueGit

timfa frameworkv3

subImage calc in image

timfa wsadhelios · pushed by timfa

Authored 2022-10-20 · 4 years ago

Hash
94864fd3dd93f68a14dcd5a5c033eff0988999b3
Parent
ab969f12
2 files changed +21 −14
framework/engine/image.js +19 −2
@@ -24,7 +24,7 @@ class image{
24 24 console.log("Image loaded: " + image.src);
25 25 }
26 26
27 draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
27 draw(scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
28 28 {
29 29 var shouldDraw = true;
30 30
@@ -52,7 +52,9 @@ class image{
52 52
53 53 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
54 54
55 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
55 var subImageData = this.getSubImage(subImage);
56
57 context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw/2, -dh/2, dw, dh);
56 58
57 59 context.restore();
58 60 context.globalAlpha = oldAlpha;
@@ -80,4 +82,19 @@ class image{
80 82 {
81 83 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
82 84 }
85
86 getSubImage(subImageIndex)
87 {
88 var subImage = Math.round(subImageIndex);
89 subImage %= this.subImages;
90
91 var c = subImage % this.subImagesPerRow;
92 var r = Math.floor(subImage / this.subImagesPerRow);
93 var sw = (this.g_image.width / this.subImagesPerRow);
94 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
95 var sx = sw * c;
96 var sy = sh * r;
97
98 return {sw : sw, sh : sh, sx: sx, sy: sy};
99 }
83 100 }
@@ -24,7 +24,7 @@ class image{
24 console.log("Image loaded: " + image.src);
24 console.log("Image loaded: " + image.src);
25 }
25 }
26
26
27 draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
27 draw(scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
28 {
28 {
29 var shouldDraw = true;
29 var shouldDraw = true;
30
30
@@ -52,7 +52,9 @@ class image{
52
52
53 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
53 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
54
54
55 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
55 var subImageData = this.getSubImage(subImage);
56
57 context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw/2, -dh/2, dw, dh);
56
58
57 context.restore();
59 context.restore();
58 context.globalAlpha = oldAlpha;
60 context.globalAlpha = oldAlpha;
@@ -80,4 +82,19 @@ class image{
80 {
82 {
81 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
83 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
82 }
84 }
85
86 getSubImage(subImageIndex)
87 {
88 var subImage = Math.round(subImageIndex);
89 subImage %= this.subImages;
90
91 var c = subImage % this.subImagesPerRow;
92 var r = Math.floor(subImage / this.subImagesPerRow);
93 var sw = (this.g_image.width / this.subImagesPerRow);
94 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
95 var sx = sw * c;
96 var sy = sh * r;
97
98 return {sw : sw, sh : sh, sx: sx, sy: sy};
99 }
83}
100}
framework/engine/objectComponents/renderer.js +2 −12
@@ -95,19 +95,8 @@ class Renderer
95 95 this.g_subImgOld = this.subImage;
96 96 }
97 97
98 var subImage = Math.round(this.subImage);
99 subImage %= this.sprite.subImages;
100
101 var c = subImage % this.sprite.subImagesPerRow;
102 var r = Math.floor(subImage / this.sprite.subImagesPerRow);
103 var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);
104 var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));
105 var sx = sw * c;
106 var sy = sh * r;
107
108 98 this.sprite.draw(
109 99 this.gameObject.scene,
110 sx, sy, sw, sh,
111 100 Math.round(this.gameObject.transform.position.x),
112 101 Math.round(this.gameObject.transform.position.y),
113 102 this.gameObject.transform.size.x,
@@ -115,7 +104,8 @@ class Renderer
115 104 this.gameObject.transform.rotation,
116 105 this.mirrorX,
117 106 this.mirrorY,
118 this.alpha
107 this.alpha,
108 this.subImage
119 109 );
120 110 }
121 111
@@ -95,19 +95,8 @@ class Renderer
95 this.g_subImgOld = this.subImage;
95 this.g_subImgOld = this.subImage;
96 }
96 }
97
97
98 var subImage = Math.round(this.subImage);
99 subImage %= this.sprite.subImages;
100
101 var c = subImage % this.sprite.subImagesPerRow;
102 var r = Math.floor(subImage / this.sprite.subImagesPerRow);
103 var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);
104 var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));
105 var sx = sw * c;
106 var sy = sh * r;
107
108 this.sprite.draw(
98 this.sprite.draw(
109 this.gameObject.scene,
99 this.gameObject.scene,
110 sx, sy, sw, sh,
111 Math.round(this.gameObject.transform.position.x),
100 Math.round(this.gameObject.transform.position.x),
112 Math.round(this.gameObject.transform.position.y),
101 Math.round(this.gameObject.transform.position.y),
113 this.gameObject.transform.size.x,
102 this.gameObject.transform.size.x,
@@ -115,7 +104,8 @@ class Renderer
115 this.gameObject.transform.rotation,
104 this.gameObject.transform.rotation,
116 this.mirrorX,
105 this.mirrorX,
117 this.mirrorY,
106 this.mirrorY,
118 this.alpha
107 this.alpha,
108 this.subImage
119 );
109 );
120 }
110 }
121
111