使用JavaScript无法调整Image()对象的大小 [英] Resize Image() object with javascript not working

查看:55
本文介绍了使用JavaScript无法调整Image()对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有一个图像对象,正在将其用作乒乓球游戏的背景.但是,图片比画布大,因此我尝试以此缩小图片

I have an image object in javascript, which I am using as the background of a pong game. However, the image is larger than the canvas, so I attempted shrinking it with this

var bg=new Image(); 
bg.src='bg.png';
bg.width=600;
bg.height=400;

但是,在画布中,图像没有改变尺寸.有谁知道是什么原因造成的?

However, in the canvas, the image did not change sizes. Does anyone know what is causing this?

推荐答案

要调整整个图像的大小,而不是从源头手动调整其大小,请将width和height参数添加到 drawImage()方法,像这样

To resize the entire image, instead of resizing it manually from the source, add the width and height parameters into the drawImage() method, like this

 ctx.drawImage(bg,0,0,600,400);

这篇关于使用JavaScript无法调整Image()对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆