简体   繁体   English

使用jQuery宽度和高度调整画布大小以适合图像尺寸

[英]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 我有一个可以使用香草javascript而不是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. 使用jQuery版本时,我得到了拉伸图像。

why? 为什么?

That's a known bug: 这是一个已知的错误:

Just write it like so: 就像这样写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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