简体   繁体   中英

resize a canvas with jquery width & height to fit an image dimensions

I have a code thats work with vanilla javascript but not with jquery

this works:

image.src = 'data:image/png;base64,' + <base64>;
c = document.getElementById('canvas-base');
c.height = image.height;
c.width = image.width;            
            ctx = c.getContext("2d");

            image.addEventListener('load', function () {
                ctx.drawImage(image, 0, 0);
                ctx.stroke();
            }, true);

but this isn't working:

c = $('#canvas-base');
c.height(image.height).width(image.width);
            ctx = c[0].getContext("2d");
            image.addEventListener('load', function () {
                ctx.drawImage(image, 0, 0);
                ctx.stroke();
            }, true);

with the jQuery version I get a stretched image.

why?

That's a known bug:

Just write it like so:

c.attr({"height": image.height , "width": image.width});

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