Get image data url in JavaScript?

后端 未结 8 2047
南笙
南笙 2020-11-21 05:11

I have a regular HTML page with some images (just regular HTML tags). I\'d like to get their content, base64 encoded preferably, without the need

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 05:47

    This is all you need to read.

    https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsBinaryString

    var height = 200;
    var width  = 200;
    
    canvas.width  = width;
    canvas.height = height;
    
    var ctx = canvas.getContext('2d');
    
    ctx.strokeStyle = '#090';
    ctx.beginPath();
    ctx.arc(width/2, height/2, width/2 - width/10, 0, Math.PI*2);
    ctx.stroke();
    
    canvas.toBlob(function (blob) {
      //consider blob is your file object
    
      var reader = new FileReader();
    
      reader.onload = function () {
        console.log(reader.result);
      }
    
      reader.readAsBinaryString(blob);
    });
    

提交回复
热议问题