How to apply a mask to CustomPIXIComponent
See original GitHub issueDescription
Hi Michal,
This is a question, I believe I can’t add the question label directly. I have been struggling with applying a mask with a CustomPIXIComponent. I have honestly no idea if this works. I thought I’d go ahead and ask you.
The following is my custom PIXI component, which renders a circle and animates along the X-axis:
const Circle = CustomPIXIComponent(
{
// Only necessary argument
customDisplayObject: (_props: any) => new PIXI.Graphics(),
// Apply props in a custom way with customDisplayObject passed as instance
customApplyProps: (
instance: PIXI.Graphics,
oldProps: any,
newProps: any
) => {
instance.clear();
instance.beginFill(oldProps.color, newProps.opacity);
instance.drawCircle(oldProps.y, newProps.x, oldProps.radius);
instance.endFill();
if (oldProps.mask) {
// const mask = PIXI.Sprite.fromImage("https://unsplash.it/200");
// mask.anchor.x = 0.5;
// mask.anchor.y = 0.5;
// mask.position.x = newProps.x;
// mask.position.y = oldProps.y;
// instance.mask = oldProps.mask;
}
}
},
"Circle"
);
Which, in my render method, looks like this:
public render() {
return (
<Container>
<Circle
x={this.bigCircleX}
y={this.props.stageCont
radius={this.bigCircleRadius}
opacity={this.opacity}
color={0xe88d3e}
/>
</Container>
);
}
The result:
How do I add a mask from a sprite or image, so that my circle has a ‘background’? I have tried the following:
- Render a
<Sprite />
inside my<Container />
(with aPixi.Texture.fromImage
as texture prop), but then, how do I apply this mask to my drawing instance insideCustomPIXIComponent
? - Create a new
PIXI.Sprite
as a mask insidecustomApplyProps
directly, applying this asinstance.mask
, but this does nothing, do I still need to add it to my container?
If you would have the time to give me some insights would be awesome. TIA.
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (10 by maintainers)
Top Results From Across the Web
How to apply mask in pixijs container that will not show it's ...
Remove the addChild() call, and it will mask properly, but in addition, you should set the container mast to a PIXI.Rectangle instead of...
Read more >How to use the react-pixi-fiber.CustomPIXIComponent ... - Snyk
To help you get started, we've selected a few react-pixi-fiber examples, based on popular ways it is used in public projects. Secure your...
Read more >Simple Mask | react-pixi-fiber github issue #57 - CodeSandbox
import React, { Component } from "react"; · import ReactDOM from "react-dom"; · import { Container, CustomPIXIComponent, Stage } from "react-pixi-fiber"; · import ......
Read more >react-pixi-fiber/Lobby - Gitter
Would you be able to describe your use case when it comes to portals? ... Does anyone know how I would animate a...
Read more >Using input masks in React Native - LogRocket Blog
Project setup and installing the masking library · Creating a phone number mask · OTP masked input · Creating date and time format...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think in general, with masks, you have two solutions:
<Graphics ref={maskRef} />
, take theref
and a apply it with amask
prop to other rendered component, like<Sprite mask={maskRef} />
. This will involve two-pass rendering, so you have tosetState
or callforceUpdate
after mount, so the ref is not null.@michalochman that’s okay. I figured it out, didn’t have the time yet to post my solution here.
Like you said, using the masks as a
ref
works when my components are interactive, but I indeed had to be sure to callforceUpdate()
after mount.example of one of my render methods: