MV Draw and Clear Rectangles\Bitmaps via Script calls

I'm managed to create a painting ability using this Script for each new rectangle:

var bitmap = new Bitmap(816, 624);
bitmap.fillRect(x, y, width, height, "rgb(255, 0, 0)")
SceneManager._scene.addChild(new Sprite(bitmap));


However, unfortunately, with time it drops your FPS

Also, I couldn't figure out how to clear rectangles. I've tried:

var bitmap = new Bitmap(816, 624);
bitmap.clearRect(x, y, width, height)
SceneManager._scene.addChild(new Sprite(bitmap));


and

this.bitmap.clearRect(x, y, width, height)

Coordinates, width, and height parameters are identical, but nothing gets eraser and it even further worsens my FPS.
BUMP
Hi there!

I risk saying that the cause of the fps drops, can be the number of rectangles you are putting on the map.

Assuming you are using script calls in the event script field to draw them, I will point some things out:

Try to put your rectangles in global variables for you to be able to call them later. You need to identify them to be able to reference them later.

var bitmap = new Bitmap(816, 624);
bitmap.fillRect(x, y, width, height, "rgb(255, 0, 0)")
window.mySprite = new Sprite(bitmap)
SceneManager._scene.addChild(window.mySprite);


So if you want to clear that rectangle later, you can just use the script call:
window.mySprite.bitmap.clearRect(x, y, width, height)

Because of the way you are doing, you are just creating a new rectangle and putting it on the map. The new rectangle(bitmap) will not clear the other rectangles you already draw.

I don't know exactly what you are doing and neither your javascript experience, so I tried to not use any terms that can confuse you and not take the explanation too deep.

But it seems to me that you can try to start writing a plugin to do this stuff. It will be better for you.

In short:

To create new rectangles, try to assign them to a global variable using the keyword "window".
To clear the rectangles, reference the created ones by their global variable id:
window.[rectangle name here].bitmap

Hope I was able to help you. Make that kind of explanation in English was thought to me xD
Thx, but unfortunately now the eraser would only remove my last drawn rectangle and ignore the rest of them.
I was trying to assign a unique global variable to each rectangle and then try to remove them one by one, but it doesn't work either.
DRAW

◆Script:self(this)["id"] += 1
◆Script:var bitmap = new Bitmap(816, 624);
: :bitmap.fillRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"], "rgb(255, 0, 0)")
: :window.self(this)["id"] = new Sprite(bitmap)
: :SceneManager._scene.addChild(window.window.self(this)["id"]);

ERASE

◆Script:self(this)["id count"] = 0
◆Ir a etiqueta:erase
◆Script:self(this)["id count"] += 1
◆Si:Script:self(this)["id count"] == self(this)["id"]
◆Script:window.self(this)["id count"].bitmap.clearRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"])
◆
:Otro
◆Script:window.self(this)["id count"].bitmap.clearRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"])
◆Etiqueta:erase
◆
:Fin

All of my coordinates are "rounded" and grid-based, they should be identical.
SceneManager._scene.addChild(window.window.self(this)["id"]);
I believe the problem can be here.
there is a typo there "window.window.self(this)["id"]"

You are repeating window two times.
True, and my looping was wrong too, but it still doesn't work. Maybe u can't replace the window.X part with a variable at all? Or it must contain a letter or smth. I'll try more variations.
Well, I didn't quite understand your whole event and the self(this) thing.
But if you try to do this in a script call, it will work.
JavaScript:
// Rectangle 1 DRAW


var bitmap = new Bitmap(400, 400)
bitmap.fillRect(0, 0, 400, 400, "red")
window.rectangle1 = new Sprite(bitmap)


// Add to Scene Map
SceneManager._scene.addChild(window.rectangle1)

// Rectangle 2 DRAW

var bitmap = new Bitmap(400, 400)
bitmap.fillRect(0, 0, 400, 400, "blue")
window.rectangle2 = new Sprite(bitmap)
window.rectangle2.x = 410 // Changing position X
window.rectangle2.y = 410 // Changing position Y

// Add to Scene Map
SceneManager._scene.addChild(window.rectangle2) // Add to scene map

// ERASE
window.rectangle1.bitmap.clearRect(200, 200, 200, 200)
window.rectangle2.bitmap.clearRect(200, 200, 200, 200)

I advise you to first try drawing these two examples. Them after you made it, you try to implement it in your event's logic.
Sorry, I'll explain.
Basically, self(this) is a self variable.
Now the only problem is that I have to replace the global variable's name (window.X) by my self variable's value (self(this)["id"])
So I wouldn't have to manually code each window.rectangle1, window.rectangle2, and so on.
Hope it makes sense
Ok... I think I get it.
Try to make it this way:

JavaScript:
var rectName = self(this)["id"]
window[rectName] = new Sprite(bitmap)
Okay, I had to change it a bit for it to work cause it wouldn't do anything otherwise:
var rectName = self(this)["id"]
window["rectName"] = new Sprite(bitmap)

But now it only works on mouse click for some reason (I don't have any mouse click conditions)
And weirdly the eraser tool still only removes one rectangle 😀
I'm lost

Here's my new code:

Code:
◆Si:Script:s["paint mode"] == 1
      ◆Script:self(this)["id count"] = 0
      ◆Etiqueta:erase
      ◆Script:self(this)["id count"] += 1
      ◆Script:var rectName = self(this)["id count"]
      :      :window["rectName"].bitmap.clearRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"])
      ◆Si:Script:self(this)["id count"] != self(this)["id"]
        ◆Ir a etiqueta:erase
        ◆
      :Fin
      ◆
    :Otro
      ◆Script:self(this)["id"] += 1
      ◆Script:var bitmap = new Bitmap(816, 624);
      :      :bitmap.fillRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"], "rgb(255, 0, 0)")
      :      :var rectName = self(this)["id"]
      :      :window["rectName"] = new Sprite(bitmap)
      :      :SceneManager._scene.addChild(window["rectName"]);
      ◆
    :Fin

Maybe I could create an Undo button instead. Is it possible to use clearRect without bitmaps parameters? To simply remove it by it's id.
Okay, I had to change it a bit for it to work cause it wouldn't do anything otherwise:
var rectName = self(this)["id"]
window["rectName"] = new Sprite(bitmap)

But now it only works on mouse click for some reason (I don't have any mouse click conditions)
And weirdly the eraser tool still only removes one rectangle 😀
I'm lost

Here's my new code:

Code:
◆Si:Script:s["paint mode"] == 1
      ◆Script:self(this)["id count"] = 0
      ◆Etiqueta:erase
      ◆Script:self(this)["id count"] += 1
      ◆Script:var rectName = self(this)["id count"]
      :      :window["rectName"].bitmap.clearRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"])
      ◆Si:Script:self(this)["id count"] != self(this)["id"]
        ◆Ir a etiqueta:erase
        ◆
      :Fin
      ◆
    :Otro
      ◆Script:self(this)["id"] += 1
      ◆Script:var bitmap = new Bitmap(816, 624);
      :      :bitmap.fillRect(self(this)["xMath"], self(this)["yMath"], self(this)["pixel size"], self(this)["pixel size"], "rgb(255, 0, 0)")
      :      :var rectName = self(this)["id"]
      :      :window["rectName"] = new Sprite(bitmap)
      :      :SceneManager._scene.addChild(window["rectName"]);
      ◆
    :Fin

Maybe I could create an Undo button instead. Is it possible to use clearRect without bitmaps parameters? To simply remove it by it's id.
I don't know what you are doing with the "self" thing as I said before.
What does self(this)["id"] returns?

You need to know that the window keyword is an object( global object ), and what you are doing is creating new properties for that object.

To create a new property on an object, in javascript, you can do one of that below:

JavaScript:
object.propertyName
object["string"]
object[variable]

In the case of a variable, it must return a string or a number value.

I suggest you first create your rectangles outside the logic of your event. Which means, without the "self" thing. Just like I did before.
Create 3 rectangles and play with them. Change its place, draw, clear, etc. Once you are done with it, you can try to apply that to your event logic.

Another suggestion would be for you to create an array instead, to add all your rectangles(it's a little bit more advanced than what we are doing before):

window.myRectList = []

And then add all your rectangles there:

JavaScript:
// Create the rectangle
var bitmap = new Bitmap(400, 400)
bitmap.fillRect(0, 0, 400, 400, "red")
var sprite = new Sprite(bitmap)

// Add to Scene Map
SceneManager._scene.addChild(sprite)

// Add to your rectangle list
window.myRectList.push(sprite)

So to reference your rectangles, you can use:
window.myRectList[index] // Replace index with a number starting from zero. This will return the sprite that holds your rectangle(bitmap)
0 = first rectangle
1 = second rectangle
etc...
window.myRectList[0].bitmap.clearRect(x, y, width, height)
Read an interesting post about working with arrays and managed to fix my script! Thank you so much!
Solved!

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/draw-and-clear-rectangles-bitmaps-via-script-calls.139806/