Snippets

Elvis Morales Apply different sized vertical and horizontal borders to an image using a single function on JavaScript

Created by Elvis Morales
function pixelOnEdgeDifferentThicknesses( pixel, image, borderWidth, borderHeight ) {
    
    var x = pixel.getX();
    var y = pixel.getY();
    
    if ( x < borderWidth ) return true;
    if ( y < borderHeight ) return true;
    if ( x >= image.getWidth() - borderWidth ) return true;
    if ( y >= image.getHeight() - borderHeight ) return true;
    
    return false;
}

function setBlack( pixel ) {
    pixel.setRed(0);
    pixel.setGreen(0);
    pixel.setBlue(0);
    return pixel;
}

var image = new SimpleImage( "island.png" );
for ( var pixel of image.values() ) {
    if ( pixelOnEdgeDifferentThicknesses( pixel, image, 50, 20 ) ) {
        pixel = setBlack( pixel );
    }
}

print(image);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.