From ef4655bbe8b1ea8beed8f08af67bb588e578a941 Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 12 Apr 2022 09:20:25 -0500 Subject: [PATCH] fix: clamp subimage --- packages/graphics/src/image.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/graphics/src/image.js b/packages/graphics/src/image.js index dd99172..9461379 100644 --- a/packages/graphics/src/image.js +++ b/packages/graphics/src/image.js @@ -1,3 +1,4 @@ +import {Rectangle} from '@avocado/math'; import {Resource} from '@avocado/resource'; import {imageSize} from 'image-size'; import LRU from 'lru-cache'; @@ -116,15 +117,21 @@ export default class Image extends Resource { [, , subimage.texture.width, subimage.texture.height] = rectangle; } else { - const frame = { - x: rectangle[0], - y: rectangle[1], - width: rectangle[2], - height: rectangle[3], - }; - // eslint-disable-next-line global-require - const {Texture} = require('@pixi/core'); - subimage.texture = new Texture(this.texture, frame); + const intersection = Rectangle.intersection( + Rectangle.compose([0, 0], this.size), + rectangle, + ); + if (!Rectangle.isNull(intersection)) { + const frame = { + x: intersection[0], + y: intersection[1], + width: intersection[2], + height: intersection[3], + }; + // eslint-disable-next-line global-require + const {Texture} = require('@pixi/core'); + subimage.texture = new Texture(this.texture, frame); + } } return subimage; }