1

Converting image to the canvas with canvas.drawImage fails(but not throws err), the canvas looks transparent empty image

It doesn't happen for all image files I could not figure out which property of the image causes this but I uploaded an example photo with this issue

I started having this problem with chrome v83-v84 firefox works fine.

I want to upload a specified photo (its inside fiddle) and want to make canvas and draw canvas from the uploaded image and display it on chrome (chrome version should bigger or equal v85)

function testimg(src) {
        var img = document.createElement("img");
        img.src = src;
        document.querySelector('#test').appendChild(img)
    }

    $('#file').on('change', fileAdded)

    function fileAdded(event) {

        var reader = new FileReader;

        reader.onload = function () {
            var image = new Image();
            image.src = reader.result;
            image.onload = function () {
                document.querySelector('#test').appendChild(this)
                var imgToSend = processImg(this, this.width, this.height);
                testimg(imgToSend)
            };
        };

        reader.readAsDataURL(event.currentTarget.files[0]);

    }

    function processImg(image, srcWidth, srcHeight) {
        var canvas = document.createElement('canvas');
        canvas.width = srcWidth;
        canvas.height = srcHeight;

        var ctx = canvas.getContext("2d");

        ctx.drawImage(image, 0, 0);
        ctx.fill();

        return canvas.toDataURL();
    }

https://jsfiddle.net/msacar/2Lgmc85t/24/

3
  • It works on my machine. Might be related to a security mechanism preventing canvases to draw cross origin images developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image Commented Dec 1, 2020 at 10:16
  • Which version of chrome you are using ? Commented Dec 1, 2020 at 13:11
  • it was about chrome versions Commented Aug 25, 2021 at 14:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.