从数组中绘制(画布)多个项目的函数
我有一个 seat
函数,它接受多个值,其中 3 个用于在画布上绘制圆圈。
每个圆圈都是一个以人名命名的变量,包含来自 seat
函数的值。
我有以下函数来绘制各个圆圈:
ctx.fillPerson = function(p){ this.fillStyle = p.fillStyle; this.arc(p.centerX, p.centerY, p.radius, 0, 2*Math.PI, false); this.fill(); }
例如,当我执行 ctx.fillPerson(johnSmith); 时,这非常有效,但是我正在以数组形式创建多个人员分组,并且我需要一个函数来绘制这些数组中的人。
我有一个包含上面提到的变量的数组,如下所示: var SeatingOne = [johnSmith, joanSmith, bobJohnson, jillJohnson];
但是,我似乎无法正确使用该函数,我觉得我是在正确的轨道上,但我有限的 JavaScript 知识阻碍了我。到目前为止,我的想法是:
ctx.fillSeats = function(s){
for ( var i=0; i<s.length; ++i ){
ctx.beginPath();
ctx.fillPerson(indexOf(s));
}
}
我的想法是单步遍历数组并对每个数组项执行 beginPath();
和 ctx.fillPerson();
。我认为(?) indexOf
是否是正确的工具,但我不确定它是否正确以及如何获得所需的结果。
非常感谢任何帮助,如果我说了什么不正确的话,请纠正我(也不熟悉所有行话;))
谢谢
I have a seat
function which takes a number of values, 3 of which are used to draw circles on the canvas.
each circle is a var named after a person and containing the values from the seat
function.
I have the following function which I use to draw the individual circles:
ctx.fillPerson = function(p){ this.fillStyle = p.fillStyle; this.arc(p.centerX, p.centerY, p.radius, 0, 2*Math.PI, false); this.fill(); }
this works great when I do for example, ctx.fillPerson(johnSmith);
however I am creating multiple groupings of people in array form and I need a function to draw the people in these arrays.
I have an array containing the variables mentioned above like this: var seatingOne = [johnSmith, joanSmith, bobJohnson, jillJohnson];
However, I can't seem to get the function right, I feel like I am on the right track, but my limited javascript knowledge is holding me back. So far I have this:
ctx.fillSeats = function(s){
for ( var i=0; i<s.length; ++i ){
ctx.beginPath();
ctx.fillPerson(indexOf(s));
}
}
My thinking is that I step through the array and perform a beginPath();
and ctx.fillPerson();
for each array item. I THINK(?) indexOf
if the right tool to use but I am unsure if it is and exactly how to get the desired result.
Any help is much appreciated and please correct me if I said anything improperly (not hip to the all the lingo quite yet either ;) )
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ctx.fillPerson(s[i]);
我不知道是否需要结束每个循环的路径。
ctx.fillPerson(s[i]);
I don't know off the top of my head if you need to end the path each loop.