How to detect if an image generated via canvas is blank (transparent PNG)?

后端 未结 2 1077
一生所求
一生所求 2021-02-05 16:00

I am working on an application in which an image is created/edited on a HTML5 canvas and then saved into a file-store/cloud. The problem is that of \"saving efficiency\". On sav

2条回答
  •  孤街浪徒
    2021-02-05 16:20

    HTML:

    
    

    SCRIPT:

    isCanvasTransparent(document.getElementById("canvas_img"));
    
    function isCanvasTransparent(canvas) { // true if all pixels Alpha equals to zero
      var ctx=canvas.getContext("2d");
      var imageData=ctx.getImageData(0,0,canvas.offsetWidth,canvas.offsetHeight);
      for(var i=0;i

    UPDATE:

    Dont use CSS style declarations like border: 1px solid black; for CANVAS, because border included into canvas image, and, as result, alpha chanel is always not equals to zero.

提交回复
热议问题