diff --git a/src/utils/layer-util.ts b/src/utils/layer-util.ts index 02baa0e..8c1b402 100644 --- a/src/utils/layer-util.ts +++ b/src/utils/layer-util.ts @@ -90,6 +90,11 @@ export const cropLayerContent = async ( layer: Layer, cropRectangle: Rectangle ) if ( layer.mask ) { layer.mask = await resizeImage( layer.mask, width, height, left, top, width, height ); } + + if ( isLargerThanCrop ) { + layer.width = width; + layer.height = height; + } } else { layer.left -= left; layer.top -= top; diff --git a/tests/unit/utils/layer-util.spec.ts b/tests/unit/utils/layer-util.spec.ts index fec3fac..a290033 100644 --- a/tests/unit/utils/layer-util.spec.ts +++ b/tests/unit/utils/layer-util.spec.ts @@ -135,7 +135,7 @@ describe( "Layer utilities", () => { }); describe( "when cropping a Layer", () => { - it( "should resize the source contents", () => { + it( "should resize the source contents", async () => { const source = createMockCanvasElement(); const layer = LayerFactory.create({ @@ -145,7 +145,7 @@ describe( "Layer utilities", () => { height: 100, source, }); - cropLayerContent( layer, { left: 10, top: 20, width: 40, height: 50 }); + await cropLayerContent( layer, { left: 10, top: 20, width: 40, height: 50 }); expect( mockResizeImage ).toHaveBeenCalledWith( source, 40, 50, 10, 20, 40, 50 ); }); @@ -198,6 +198,20 @@ describe( "Layer utilities", () => { expect( layer.top ).toEqual( -25 ); }); + it( "should update the Layer dimensions when the Layer size is larger than the crop size", async () => { + const layer = LayerFactory.create({ + left: 0, + top: 0, + width: 80, + height: 100, + source: createMockCanvasElement(), + }); + await cropLayerContent( layer, { left: 10, top: 20, width: 40, height: 50 }); + + expect( layer.width ).toEqual( 40 ); + expect( layer.height ).toEqual( 50 ); + }); + it( "should not resize the source contents but offset the Layer when the Layer size is smaller than the crop size", () => { const layer = LayerFactory.create({ left: 0, @@ -212,5 +226,19 @@ describe( "Layer utilities", () => { expect( layer.left ).toEqual( -10 ); expect( layer.top ).toEqual( -20 ); }); + + it( "should not update the Layer dimensions when the Layer size is smaller than the crop size", () => { + const layer = LayerFactory.create({ + left: 0, + top: 0, + width: 30, + height: 40, + source: createMockCanvasElement(), + }); + cropLayerContent( layer, { left: 10, top: 20, width: 40, height: 50 }); + + expect( layer.width ).toEqual( 30 ); + expect( layer.height ).toEqual( 40 ); + }); }); }); \ No newline at end of file