Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
problems with image resizing

I'm trying to resize an image which comes from a Quill editor. I used this links as guides:

  • https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
  • Resize image with javascript canvas (smoothly)

This is my code:

async handleImageAdded(file, Editor, cursorLocation, resetUploader) {
        // Resizing
            var img = document.createElement("img");
            img.src = URL.createObjectURL(file);
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, 200, 200);
            var newFile = await new Promise(resolve => canvas.toBlob(resolve));
            document.getElementById("preview").src = URL.createObjectURL(newFile); //check
            
        //Sending data to server   

Expected behaviour: will display a resized version of the image which was sent in a file variable. (if I switch newFile to file, everything works just as expected)

Current behaviour: a white rectangle 300x150px

Could you please help me to find what am I missing?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The reason was the absence of image.onload event handler. This worked for me:

async handleImageAdded(file, Editor, cursorLocation, resetUploader) {
        // Resizing
            var img = document.createElement("img");
            img.src = URL.createObjectURL(file);
            var newFile;
            const promise = new Promise(resolve => {
                img.onload = function (event) {
                            // Dynamically create a canvas element
                            var canvas = document.createElement("canvas");

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

                            // Actual resizing
                            ctx.drawImage(img, 0, 0, img.width * 0.5, img.height * 0.5);

                            canvas.toBlob(blob => {
                                newFile = blob; 
                                console.log(newFile) 
                                resolve();
                            });
                            // Show resized image in preview element
                            //var dataurl = canvas.toDataURL(file.type);
                            //document.getElementById("preview").src = dataurl;
                }
            })
            await promise;
            //var newFile = await new Promise(resolve => canvas.toBlob(resolve));
            document.getElementById("preview").src = URL.createObjectURL(newFile); //check
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!