制作一个 canvas 图像的大小?

Make a canvas the size of an image?

我正在读取一个文件,检查它的 exif 数据,添加到 canvas 并在必要时进行转换。

我需要保持 canvas 与原始图像大小相同,我试过这个:

ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, img.width, img.height);

这似乎将 canvas 大小默认为 300 x 150。

如何使 canvas 图像的大小?

ctx.canvas.width = img.width
ctx.canvas.height = img.height

应该可以正常工作