Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making a shape a const, and using it multiple times in js

I’m trying to make a pixelated image by hand, but the editor im using does not allow copy and pasting, which means I have to type everything by hand, so defining a new rectangle each time I wanna use it is too much work.

What I tried was:

const grey = new Rectangle(10, 10);
grey.setPosition (100, 400);
grey.setColor ("#686968");
add (grey);
grey. setPosition (110, 400);
grey.setColor("#686968");
add (grey);

But instead of adding another grey square, it just moved the first one.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I also looked online and did not find anything that helped.

>Solution :

Create a function that makes the Rectangle for you, based on some parameters:

function getColoredRectangle(color, x, y, width = 10, height = 10, ) {
    const rect = new Rectangle(width, height);
    rect.setPosition(x, y);
    rect.setColor(color);
    return rect;
}

add(getColoredRectangle("#686968", 100, 400))
add(getColoredRectangle("#123456", 110, 400))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading