From b43b67ecac18de13a8c14aea0e9f872b0fc65924 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sun, 30 Apr 2023 18:34:17 +0300 Subject: [PATCH] update tiled sprite draw --- .prettierignore | 1 + src/js/objects.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.prettierignore b/.prettierignore index 05b0aa4..d1f6971 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,5 +1,6 @@ **/.git **/node_modules +**/dist *.html *.css diff --git a/src/js/objects.js b/src/js/objects.js index c47aaf0..7f54d11 100644 --- a/src/js/objects.js +++ b/src/js/objects.js @@ -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 + ); + } } } }