Is it possible to make a gradient-transparent/layer masking image using canvas?

后端 未结 5 1908
傲寒
傲寒 2021-02-05 14:01

I\'ve been following the lessons about transparency and gradients on the Mozilla site: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Applying_styles_an

5条回答
  •  野性不改
    2021-02-05 14:18

    Its possible to use context.globalCompositeOperation to make the the mask

    context.drawImage(img, 0, 0, img.width, img.height, 0,0, img.width, img.height);
    context.globalCompositeOperation = "destination-out";
    gradient = context.createLinearGradient(0, 0, 0, img.height);
    gradient.addColorStop(0, "rgba(255, 255, 255, 0.5)");
    gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
    context.fillStyle = gradient;
    context.fillRect(0, 0, img.width, img.height);
    

    This do not do per pixel manipulation and should be faster

提交回复
热议问题