How to crop/mask images in a plugin?

I need to essentially crop an image to the dimensions of a rectangle and can’t seem to figure out how. I’ll add some photos for an example.

Here I have the image and I have the Rectangle object:
Screen Shot 2022-12-22 at 10.33.44 AM

I need to be able to programmatically crop the image to the dimensions of the Rectangle object and then get a PNG of the cropped image, essentially creating a mask of the two. Is this possible? Or do I need to be cropping in the UI via the browser canvas?

Group both layers and then set your rectangle as a mask.

node.isMask = true

And for cropping, you need to change the scaleMode to CROP and the imageTransform matrix.

To learn how to edit readonly properties, see here: