0

I have this code

let img= html2canvas($("#divPage")[0], {
    onrendered: function(canvas) {
        var myImage = canvas.toDataURL("image/png");                                              
        return myImage;
    },    
    allowTaint: true,
    taintTest: false
});
img.then((value) => {
    console.log(value);
    window.open(value);
});

window.open(value); open a new tab with the link https://www1.inscription.tn/ORegMx/servlet/[object%20HTMLCanvasElement]

so the problem is let img is in the wrong place, it let the method return the object which is the HTMLCanvasElement instead of returning the image data url returned from onrendered: function(canvas)

I've checked these answers:

Why is my asynchronous function returning Promise { } instead of a value?

How can i fix that in order to get the correct value ?

Hope someone can help,

Thanks

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
user11845248
  • 131
  • 8
  • Sounds like the promise doesn't resolve with the return value of the `onrendered` callback. Why not just drop that and use the `value.toDataURL()` in the `then` callback? – Bergi Jul 28 '19 at 16:56
  • @Bergi can you share a code with that suggestion please i'm getting an error `TypeError: html2canvas(...).then is not a function` – user11845248 Jul 28 '19 at 17:07
  • But that is what your current code was doing as well (`img` being the promise)? – Bergi Jul 28 '19 at 18:13
  • 1
    It would seem that `html2canvas()` is designed to be used with its `onrendered` option *or* the `then()` method of the returned promise. Both expose the same `canvas` element to a callback function. Try using one approach or the other, not both. – Roamer-1888 Jul 29 '19 at 07:18

0 Answers0