简体   繁体   中英

try to load base64 img into a canvas

i'm currently trying to load a base64 img into my canvas

  console.log('Change');
  var canvas = document.getElementById('canvas');
  var ctx = canvas.getContext('2d');
    var image = new Image();
    image.onload = function() {
      ctx.drawImage(image, 0, 0);
    };
    image.src = stack[1].save;

stack[1].save contains a valid base64 png img URL('data:image/png;base64,xxxxxx'), when i paste this URL into my browser i can see a valid img

The fact is that nothing changes and i dont have any error

If you could help me this will be awesome, thank's

Yes the code you have shared should work OK.

Here is an example

 const canvas = document.getElementById('canvas') const ctx = canvas.getContext('2d') var image = new Image(); image.onload = () => { ctx.drawImage(image, 0, 0) } image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAQAAAAngNWGAAAAF0lEQVR42mNk+M9AFGAcVTiqcFQhCAAAf0sUAaSRMCEAAAAASUVORK5CYII=" var image2 = new Image() image2.onload = () => { for(i=1; i<9; i++) ctx.drawImage(image2, 30*i, 5+i) } image2.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg=="
 <canvas id="canvas"></canvas>

The only thing that could be wrong is that stack[1].save that you are using...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM