【发布时间】:2015-10-06 06:09:04
【问题描述】:
我是 JS 新手,还没有接触过 jQuery 之类的东西。我要做的就是在画布上绘制 4 个简单的圆圈。我将对象放入一个数组中,因为我计划将来可能会扩展,但这些信息是无关紧要的。
我需要帮助的是了解如何在数组中创建对象然后在画布上显示它们的基础知识。这是我功能失调的代码:
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d");
var W = 400, H = 500
canvas.width = W, canvas.height = H;
var ball = [3];
for (i===0;i<=3; i++){
ball[i] = {
x: (Math.floor(Math.random() * 350) + 50),
y: (Math.floor(Math.random() * 450) + 50),
color: "red",
radius: (Math.floor(Math.random() * 35) + 15),
draw: balls(x, y, color, radius)
};
}
function balls(x, y, color, radius){
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2*Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
【问题讨论】:
标签: javascript arrays html object canvas