How to change the opacity (alpha, transparency) of an element in a canvas element after it has been drawn?

前端 未结 8 1127
庸人自扰
庸人自扰 2020-11-27 09:51

Using the HTML5 element, I would like to load an image file (PNG, JPEG, etc.), draw it to the canvas completely transparently, and then fade it i

8条回答
  •  Happy的楠姐
    2020-11-27 10:20

    I think this answers the question best, it actually changes the alpha value of something that has been drawn already. Maybe this wasn't part of the api when this question was asked.

    given 2d context c.

    function reduceAlpha(x, y, w, h, dA) {
        var screenData = c.getImageData(x, y, w, h);
        for(let i = 3; i < screenData.data.length; i+=4){
        screenData.data[i] -= dA; //delta-Alpha
        }
        c.putImageData(screenData, x, y );
    }
    

提交回复
热议问题