简体   繁体   English

无法正确调整画布中图像的尺寸?

[英]Cannot correctly size image in canvas?

I've been working on html5 projects more recently but I think I've hit a bump. 我最近一直在从事html5项目的工作,但我想我遇到了麻烦。 Currently I am working on a simple game built in html5 and jQuery. 目前,我正在开发一个使用html5和jQuery构建的简单游戏。 The issue at hand: properly displaying an image in the canvas. 当前的问题:在画布上正确显示图像。 What I have done is create a background that is 1080 x 720 and tried to place it into my canvas of the exact same specs. 我所做的是创建一个1080 x 720的背景,并尝试将其放到规格完全相同的画布上。 Not sure if this is a browser issue or if it something I have missed amongst the Google and Stack search results, but any help is greatly appreciated. 不知道这是浏览器问题,还是我在Google和Stack搜索结果中遗漏的东西,但是对您的帮助非常感谢。 (using Firefox 26 and Chrome 32) (使用Firefox 26和Chrome 32)

EDIT: Sorry, rest of details in comment below.. 编辑:对不起,下面的评论中的其余详细信息。

It's likely that your problem is that you've used CSS properties to set the size of the canvas instead of the width and height attributes on the element itself. 您的问题可能是您使用CSS属性来设置画布的大小,而不是元素本身的widthheight属性。

The former (if specified) set how big the canvas actually appears on the page. 前者(如果指定)设置画布实际在页面上显示的大小。 The latter set the actual number of pixels in the canvas, and default (AFAICR) to 300x100. 后者将画布中的实际像素数设置为默认值(AFAICR)为300x100。

If the two don't match, all canvas operations will be scaled. 如果两者不匹配,则会缩放所有画布操作。

So, if you just use: 因此,如果您只使用:

<canvas width="1080" height="720"> </canvas>

you should be fine. 你应该没事的。

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

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