update tiled sprite draw

This commit is contained in:
Alexander Popov 2023-04-30 18:34:17 +03:00
parent e5b1498955
commit b43b67ecac
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
2 changed files with 41 additions and 4 deletions

View File

@ -1,5 +1,6 @@
**/.git
**/node_modules
**/dist
*.html
*.css

View File

@ -104,26 +104,62 @@ export class TiledSprite extends Sprite {
return -1;
}
// draw single image
context.drawImage(this.image(), this.x, this.y);
let copyUpByY = Math.ceil(this.y / this.image().height);
let copyUpByX = Math.ceil(this.y / this.image().width);
let copyDownByY = Math.ceil(
(context.canvas.height - this.y - this.image().height) / this.image().height
);
let copyDownByX = Math.ceil(
(context.canvas.width - this.y - this.image().width) / this.image().width
);
for (let i = 0; i <= copyUpByY; i++) {
for (let iy = 0; iy <= copyUpByY; iy++) {
if (copyUpByY === Infinity) {
break;
}
context.drawImage(this.image(), this.x, this.y - this.image().height * i);
for (let ix = 0; ix <= copyUpByX + 1; ix++) {
if (copyUpByX === Infinity) {
break;
}
context.drawImage(
this.image(),
this.x - this.image().width * ix,
this.y - this.image().height * iy
);
context.drawImage(
this.image(),
this.x + this.image().width * ix,
this.y - this.image().height * iy
);
}
}
for (let i = 0; i <= copyDownByY; i++) {
for (let iy = 0; iy <= copyDownByY; iy++) {
if (copyDownByY === Infinity) {
break;
}
context.drawImage(this.image(), this.x, this.y + this.image().height * i);
for (let ix = 0; ix <= copyUpByX + 1; ix++) {
if (copyUpByX === Infinity) {
break;
}
context.drawImage(
this.image(),
this.x - this.image().width * ix,
this.y + this.image().height * iy
);
context.drawImage(
this.image(),
this.x + this.image().width * ix,
this.y + this.image().height * iy
);
}
}
}
}