Js canvas fillRect()坐标作为变量不起作用 [英] Js canvas fillRect() coordinates as variable not working

查看:298
本文介绍了Js canvas fillRect()坐标作为变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的画布正方形: draw.fillRect(sq);
并且在那之前: sq = 400, 400,0,0; 。控制台显示

I have a canvas square like this: draw.fillRect(sq); and before that: sq = "400, 400, 0, 0";. The console says

CanvasRenderingContext2D.fillRect的参数不足。

"Not enough arguments to CanvasRenderingContext2D.fillRect.".

我认为这是因为将 sq 视为一个参数。有什么办法可以避免这种情况?要将所有四个参数存储在一个变量中?

I assume this is because It treats sq as one argument. Is there any way to avoid this? To get all four arguments stored in one variable?

预先感谢。

推荐答案

您的假设是正确的, fillRect需要4个参数。

Your assumption is right, "fillRect" needs 4 arguments.

以下是有关如何将它们保存在一个变量中的建议:

Here a proposal on how to save them in one variable:

Var rectData = {
"x": 400,
"y": 400,
"width": 0,
"height": 0
};

然后像这样使用它:

draw.fillRect(rectData.x,rectData.y, rectData.width, rectData.height);

顺便说一句:宽度/高度为0可能不是您想要的。您将看不到任何矩形:)最好先测试您的代码段,例如在
Mozilla开发人员网络中。

By the way: Using a width / height of 0 probably is not what you want. You will not see any rectangle :) Best test your code-snippets first, e.g. in the Mozilla Developer Network.

与应用解决方案相比,使用对象文字可以使您的代码更具可读性,因此调试会更简单。

Compared to the 'apply' solution, using a object literal will make your code more readable so debugging will be simpler.

这篇关于Js canvas fillRect()坐标作为变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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