How I can create a rectangle with Java Script

Asked

Viewed 200 times

0

I am trying to create a rectangle using Javascript, avoiding using the functions of the canvas as moveto and drawto. However my rectangle does not look good. Follows the code:

function retangulo(x1, y1, x2, y2){
            let horizontalx = x2 - x1;
            let verticaly = y2 - y1;
            linhaVertical(x1, y1, verticaly);
            linhaHorizontal(x1, y1, horizontalx);
            linhaVertical(x2, y2, verticaly);
            linhaHorizontal(x2, y2, horizontalx);

        }
function linhaVertical(x,y, size) {
            for(let i=0; i < size; i++) {
                acender(x, y + i);
            }
        }
function linhaHorizontal(x,y, size) {
            for(let i=0; i < size; i++) {
                acender(x + i, y);
            }
        }
function acender(x, y) {
            let index = (x + y * width) * 4;
            imageData.data[ index ] = 0;
            imageData.data[ index + 1] = 0;
            imageData.data [ index + 2] =0;
            imageData.data[ index + 3 ] = 255;
        }

Thank you for your attention in advance

  • Puts the error, or screenshot of the problem. And other do not use "Let" as it is only compatible in some browsers, always use "var".

  • 1

    @Sveen The support of let around 91% of world users, including the famous IE, see here. All browsers support, even if some only from a certain version, so I see no problem in using let unless you specifically need to support older versions of browsers.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.