It doesn't work, I can only clean the first child.
JavaScript:
const s = SceneManager._scene._spriteset;
const w = 500, h = 500;
const container = new PIXI.Container();
container.name = 'container';
s.addChild(container);
const bitmap = new Bitmap(w, h);
const ctx = bitmap._context;
const r = PIXI.RenderTexture.create(w, h);
Graphics._renderer.render(container, r);
ctx.lineWidth = 10;
ctx.strokeStyle = 'red';
ctx.strokeRect(75, 140, 150, 110);
ctx.fillRect(130, 190, 40, 60);
ctx.beginPath();
ctx.moveTo(50, 140);
ctx.lineTo(150, 60);
ctx.lineTo(250, 140);
ctx.closePath();
r.destroy({destroyBase: true});
bitmap._setDirty();
const spr = new Sprite(bitmap);
spr.name = 'house';
container.addChild(spr);
If I'm not wrong with this script, parent-child are: s -> container -> house, but its can only clear with spr.
After, maybe the problem is you define child with ".name", I'm look if there is a way to define a child with a position xy.
If clearRect can only works for the first child with the same name then, if each child are defined by a specific coordinate I guess it will work in the same way:
in my case each drawings cannot be have the same coordinate so I don't see why it wouldn't work
I'm looking to see if its possible to do it with PIXI.Point, "getGlobalPosition", "toGlobal". After maybe there are a more radical solution in using "removeChild"/"removeChildren" before to clean them.
The issue is that you are adding more "children" when you draw a new drawing. There is no issue with the naming because it is more like how there are people with the same name.
Let's say child 3 is named house and then child 4 is named house as well. Every child can be named house because they exist in a different part of the array. Like this:
['Jim', 'Tom', 'House', 'House']
Each one is in a different part of the array even with the same name. Or,like this:
[0,0,0,0,0]
Or
['house', 'house', 'house', 'house']
When you reference a new bitmap, you create a new area that also needs to be cleared. You cannot ask the computer to clear one bitmap and expect it to clear all of it without telling it that (A loop is needed).
So, you run a loop instead. Let's say all the children you added has the name 'house' and there are 7 of them. Also, you added them all to 1 container only. You need to do:
JavaScript:
const s = SceneManager._scene._spriteset.getChildByName('container');
for (const child of s.children) {
child.bitmap.clearRect(0, 0, 200, 200);
}
This means that you are clearing that area from 0,0 to 200,200 for every child you added in the container. If you only want to clear the children named 'house' and there were other children with different names, you filter it out:
JavaScript:
const s = SceneManager._scene._spriteset.getChildByName('container');
for (const child of s.children) {
if (child.name === 'house') child.bitmap.clearRect(0, 0, 200, 200);
}
You seem like you can work through this pretty well so I am going to leave the rest up to you.
The flexibility of the bitmap/sprite/canvas drawing is awesome, but it also means that you need to keep track of everything.
If you draw it on the same bitmap without creating a new one, then one clearRect would work. That means you need to get the ._context of that bitmap and then starting drawing on that specific one without creating a new bitmap.
It sounds to me like every drawing you are creating is assigned a new bitmap. Therefore, you need to specifically reference every bitmap you created in order to clear it properly.
Same names with different positions in the array is fine - like I said, it's just another way to get the child. Use the index, use the array, use anything that works with you. When it doesn't work, take a step back and try it a different approach.
Thanks!