[Solved] Mask Image to be used to draw source image?

How do I clip an image using a mask?

Example:
Source Image:
Image.png


Mask Image:
ImageMask.png


Result Image:
OutputImage.png


I need to only "draw/fill" from the source image the black part in the mask image.

I know I've done it before but just can't seem to remember or figure it out again...
feeling pretty dumb at the moment.

Any help is much appreciated.

Pretty sure I would use something like this:

Thanks for reading 🙂
See if this helps

Code:
// With Mask, No Hit Area
const starButton3 = new PIXI.Sprite(yellowStar);

starButton3.position.set(450, 200);

starButton3.buttonMode = true;
starButton3.interactive = true;

const squareMask = new PIXI.Graphics()
    .beginFill(0xFFFFFF)
    .drawRect(starButton3.x, starButton3.y, 75, 200)
    .endFill();

starButton3.mask = squareMask;

EDIT:

Hmm, it seems pixi masks only work in webGL mode. Canvas mode won't work. Maybe going with the draw/fill approach might be more supported in case someone is on a device that doesn't support webGL

1596045481825.png


Code:
const back = PIXI.Texture.from("img/pictures/back.png")
const mask = PIXI.Texture.from("img/pictures/clip.png")

function testRun() {
  var backSpr = new PIXI.Sprite(back)
  var maskSpr = new PIXI.Sprite(mask)    
  backSpr.mask = maskSpr    
  SceneManager._scene.addChild(backSpr);
}

This is what I used to test. I made a script call after the map loads to make sure the two images have been loaded properly.
IIRC (not home right now) you actually in this instance use the shape itself as the base sprite, and mask it with the visual sprite (the blue one). Make sure both the mask and the maskee share the same parent. You can just add them both to a PIXI.Container
here is someone that made it like that found here.
it's probably a different method, buts its effective and simple to use.

it depends how you want it to work though, but you get an idea.
See if this helps

Code:
// With Mask, No Hit Area
const starButton3 = new PIXI.Sprite(yellowStar);

starButton3.position.set(450, 200);

starButton3.buttonMode = true;
starButton3.interactive = true;

const squareMask = new PIXI.Graphics()
    .beginFill(0xFFFFFF)
    .drawRect(starButton3.x, starButton3.y, 75, 200)
    .endFill();

starButton3.mask = squareMask;

EDIT:
Hmm, it seems pixi masks only work in webGL mode. Canvas mode won't work. Maybe going with the draw/fill approach might be more supported in case someone is on a device that doesn't support webGL

View attachment 153280

Code:
const back = PIXI.Texture.from("img/pictures/back.png")
const mask = PIXI.Texture.from("img/pictures/clip.png")

function testRun() {
  var backSpr = new PIXI.Sprite(back)
  var maskSpr = new PIXI.Sprite(mask)   
  backSpr.mask = maskSpr   
  SceneManager._scene.addChild(backSpr);
}

This is what I used to test. I made a script call after the map loads to make sure the two images have been loaded properly.
Thank you!!! Many thanks, this is a great start for me at the very least.
Works like a charm. Thanks for such a quick response too. ☺️

IIRC (not home right now) you actually in this instance use the shape itself as the base sprite, and mask it with the visual sprite (the blue one). Make sure both the mask and the maskee share the same parent. You can just add them both to a PIXI.Container
Yes indeed seems to work exactly as needed. Thanks for the nice info ☺️

here is someone that made it like that found here.
it's probably a different method, buts its effective and simple to use.

it depends how you want it to work though, but you get an idea.
Ahh very cool, I knew I had seen a plugin for this in the past. Thanks ever so much for the linkage.

Seems I have plenty enough to go on now guys.

Thank a bunch guys! 🙂👍
I think I shall now report this as closed. :popcorn:
The linked plugin uses the mask approach, and as the author points out there can be issues with canvas mode. Especially on android?

If there was a way to accomplish masking without webGL that might be useful, especially if you're working with simple masks.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-mask-image-to-be-used-to-draw-source-image.124696/