简体   繁体   English

HTML5 Canvas绘制和更新

[英]HTML5 Canvas drawing and updating

Sorry for how vague the title is, but I'm not completely sure how to sum up what I'm trying ask. 抱歉标题太模糊了,但我不确定如何总结我要问的问题。

First off, here is a jsFiddle of what I have so far. 首先,这是到目前为止的jsFiddle

I'm just starting off learning to use the HTML5 canvas element, as well as using js OOP. 我刚开始学习使用HTML5 canvas元素以及js OOP。

As you can see from the fiddle, I create a rectangle class (so that I may create as many squares as possible, although I think that may be redundant), and reference it to draw. 从小提琴中可以看到,我创建了一个矩形类(以便我可以创建尽可能多的正方形,尽管我认为这可能是多余的),并引用它进行绘制。 However, when I set the height = width , I always end up with a non-square. 但是,当我设置height = width ,我总是以非正方形结尾。 I'm relatively new to using the canvas, but I assume thats because the square's height and width are relative to the size of the canvas element, and so the canvas element's height and width must not be proportional (since their assigned percent values as opposed to definite pixels). 我是使用画布的新手,但我认为那是因为正方形的高度和宽度与画布元素的大小有关,因此画布元素的高度和宽度不得成比例(因为它们分配的百分比值与确定像素)。

I can fix this by setting the size of the square to pixels, but then when my keypress event is called, the square doesn't move at all. 我可以通过将正方形的大小设置为像素来解决此问题,但是当调用按键事件时,正方形完全不会移动。

Also, I have the keypress event so that a user can move the square with the arrow keys. 另外,我还有按键事件,以便用户可以使用箭头键移动正方形。 However, the up/down keys values seem to be inverted, even when I flip-flop them in the code? 但是,即使我在代码中将上/下键值进行了翻转,它们似乎也被反转了吗? And the left/right keys don't do anything? 左右键什么都不做?

And, I also can only make the square move up/down once? 而且,我也只能使方块向上/向下移动一次? Shouldn't my code allow me to increase my x/y values as long as the user keeps pushing the corresponding buttons? 只要用户不断按相应的按钮,我的代码是否应该允许我增加x / y值?

If someone could check this out and give me some pointers as to what I'm doing wrong, that would be great! 如果有人可以检查出这个问题并给我一些有关我做错了什么的指示,那太好了! I'm trying a lot of new things with this, as its just a learning experience, so if you feel so inclined, help me learn! 我正在尝试许多新事物,因为这只是一种学习经验,所以如果您有这样的意愿,请帮助我学习!

You experienced a thing that often goes wrong with resizing a canvas - trying to use CSS for it. 您遇到了调整画布大小时经常出错的事情-尝试使用CSS。 Using CSS for canvas drawing will scale it - not changing the resolution. 使用CSS进行画布绘制会缩放它-不会更改分辨率。 For setting the resolution, use the width= and height= attributes in HTML. 要设置分辨率,请使用HTML中的width=height=属性。

As for the movements, it appears that keyup works better. 至于运动,看来keyup效果更好。 However, for the up key, the y would decrement (top is 0 , bottom is something positive). 但是,对于up键, y会减小(top为0 ,bottom为正数)。

A few other things I'd like to highlight. 我想强调的其他几件事。

Using canvas as a variable name for the context is a little ambiguous. 使用canvas作为上下文的变量名有点含糊。 There is a difference between the canvas and its context used for drawing. 画布与其用于绘制的上下文之间有区别。

Also, you state //browser doesn't support html5 if ctx does not exist. 另外,您声明//browser doesn't support html5如果ctx不存在, //browser doesn't support html5 However, in case the browser does not support canvas/HTML5, the .getContext('2d') function call will fail already because it's not a function in that case. 但是,如果浏览器不支持canvas / HTML5,则.getContext('2d')函数调用将已经失败,因为在这种情况下它不是函数。 Your else statement will only get executed if the function does exist but does not return something. 仅当函数确实存在但不返回任何内容时,您的else语句才会执行。 That will probably never be the case. 可能永远不会这样。

Empty else loops aren't very useful either, but that doesn't hurt :) 空的else循环也不是很有用,但这并不有害:)

http://jsfiddle.net/DqrEm/4/ http://jsfiddle.net/DqrEm/4/

Your problem isn't with the size of your square, but with how you size your canvas . 您的问题不在于正方形的大小,而在于canvas大小。 In your jsfiddle, when I changed the canvas starting line to <canvas id="practice" width="500" height="500"> the square was drawn properly. 在您的jsfiddle中,当我将canvas起始行更改为<canvas id="practice" width="500" height="500"> ,正方形已正确绘制。 I tried changing the width and height in the CSS from 100% to 500px but the square was still drawn incorrectly. 我尝试将CS​​S中的widthheight从100%更改为500px,但是仍然无法正确绘制正方形。

Update: Setting the width and height to 100% in the actual canvas tag(ie <canvas id="practice" width="100%" height="100%"> ) works too. 更新:在实际的canvas标签中将widthheight设置为100%(即<canvas id="practice" width="100%" height="100%"> )也可以。

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

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