Skip to main content

Opacity

This method asynchronously changes the opacity of the specified image element by setting the alpha channel (transparency) to a specified value for all pixels.

Syntax

changeOpacity(image, alpha)

Parameters

  • image : HTMLImageElement
    The image element to change the opacity of.

  • alpha : number
    The new alpha value (opacity) to set for all pixels (0 to 255).

    • alpha = 255: the image is completely transparent.
    • alpha = 0: the image is completely opaque.

Return

  • Promise : Promise<HTMLImageElement>
    A promise that resolves with the image element with adjusted opacity.

Throws

  • Error
    Thrown if the specified opacity factor is outside the valid range.

Examples

const editpix = new EditPix();

// image url
const url = "images/img.jpg";

// create image
var image = new Image();
image.src = url;

//waiting image load
image.onload = () => {
// change image opacity
editpix.changeOpacity(image, 128)
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
};