【发布时间】:2011-10-28 02:01:38
【问题描述】:
最近,我开始涉足 HTML5 和强大的画布。但是,我正在尝试完成某件事,但我不确定处理它的最佳方法是什么。
我正在尝试制作一组随机生成的带有窗户的建筑物,如以下使用 div 的示例所示:
我想出的问题是我希望能够在这些建筑物的窗户中随机生成图像/内容,并且能够在单击窗口时轻松捕获,但我不知道如何开始使用画布处理它。
示例建筑:
function Building()
{
this.floors = Math.floor((Math.random()+1)*7); //Number of Floors
this.windows = Math.floor((Math.random()+1)*3); //Windows per Floor
this.height = (this.floors*12); //1px window padding
this.width = (this.windows*12); //1px window padding
//Do I need to build an array with all of my windows and their locations
//to determine if a click occurs?
}
示例窗口:
function Window(x,y)
{
this.x = x; //X Coordinate
this.y = y; //Y Coordinate
this.color = //Random color from a range
this.hasPerson //Determines if a person in is the window
this.hasObject //Determines if an object is in the window
}
总结:我正在尝试生成带有窗户的随机建筑物,但是我不确定如何构建它们、显示它们并使用画布跟踪窗户位置。
更新:
我终于能够按照我的要求生成建筑物,但现在我需要做的就是在建筑物内生成窗户并跟踪它们的位置。
【问题讨论】:
-
如果有帮助,我很乐意尽可能详细说明或回答任何问题。
-
您会考虑为此使用一张桌子吗?因为本质上,您只需要一些行和列,对吗?表格可以很好地处理 onclick 事件,无需担心。
-
@TJ,我知道有几种方法可以做到这一点。我只是想更熟悉如何使用画布。
标签: javascript jquery html canvas drawing