制作巨大的国际象棋网格的最轻量方法是什么?
我正在开发一款浏览器游戏,我忍不住想知道制作游戏所在的网格/棋盘的最简单方法是什么。
现在,作为一个简单的示例,我将向您展示以下内容:
-链接不再有效,它基本上是一个 25x25 表格+tr+td 网格-
现在,随着网格变得越来越大,表格及其 td 创建了一个非常重的文件页面,这反过来......从浏览器引擎和计算机中吸收更多资源。
那么,带有 td 的桌子是制作巨大的网格状板的最轻量的方法吗?还是有您推荐的更轻的东西?
干杯 索特克拉
I'm working on a browser-game and I can't help but wonder about what's the lightest way to make the grid/board on which the game takes place.
Right now, as a mere sample, I'll show you this:
-link no longer active, it was basically a 25x25 table+tr+td grid-
Now, as the grid gets bigger and bigger, the table and its td's create a very heavy filepage which in turn...sucks in more resources from the browser engine and computer.
So, is a table with td's the most lightweight way to craft a huge grid-like board or is there something lighter that you recommend?
Cheers
Sotkra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
计算表格布局是一项非常复杂的工作浏览器,因为它必须知道最后一个单元格的尺寸才能计算每列的确切宽度。因此,如果您使用表格,请添加
table-布局:早期修复
。浮动框或相对定位可能会渲染得更快。
Computing a table layout is a very complex work for a browser, because it has to know the dimensions of the last cell to calculate the exact width of each column. So, if you use a table, add
table-layout:fixed
early.Floating boxes or relative positioning may be faster to render.
您可以尝试删除表格,而是使用 CSS {position:relative; 来定位各个部分。顶部:20 像素; left: 20px} 等并绘制重复的背景网格。
You could try getting rid of the table, instead positioning the pieces with CSS {position: relative; top: 20px; left: 20px} etc. and draw a repeating backrgound grid.
删除链接并通过 Javascript 中的点击事件(最好使用 JQuery 之类的东西)处理它们应该会大大减少文件大小。
Removing the links and handling them via the click event in Javascript (preferably with something like JQuery) should cut down a lot on the filesize.
您可以使用 JavaScript 来操作 DOM 来构建表。如果网格的内容非常规则,则执行此操作的代码将比 400x400 倍
加标记小得多。当然,性能取决于 JavaScript 引擎。但如果这还不算太糟糕的话,它甚至可能比解析所有 HTML 更快,而与网络传输时间无关。
You could build the table by using JavaScript to manipulate the DOM. If the grid's contents are very regular, the code to do that would be much smaller than having 400x400 times
<td></td>
plus markup. Of course performance then depends on the JavaScript engine. But if that does not suck completely, it could even be faster than parsing all the HTML, independant of the network transfer time.我的建议是将图块绝对定位在板内,以节省浏览器的 HTML(传输时间)和位置计算(加载时间)。 JS 来注册点击并处理它(更少的 HTML = 更少的传输和加载时间)。
所以,你可以有这个基本的 CSS:
然后像这样生成 html:
其中每个图块都有位置
left: [X*TileWidth]px;顶部: [Y*TileHeight]px;
.这就是 David M 的建议。
如果缩小页面大小,您还可以减少页面加载时间 - 因此,就像建议的那样,使用 JQuery 或其他 JavaScript 框架为每个
div
上的点击创建触发器将是理想的选择。我不是很懂行,但是使用 JQuery 会是这样的:
My suggestion is having the tiles absolutely positioned inside the board, to save on HTML (transfer time) and position calculations (load time) by the browser. JS to register the clicks and handle it (less HTML = less transfer and load time).
So, you could have this base CSS:
Then having the html generated like that:
In which every tile has position
left: [X*TileWidth]px; top: [Y*TileHeight]px;
.That, or David M's suggestion.
You can also cut on page load time if you make the page size smaller - so, like suggested, using JQuery or another JavaScript framework to create the trigger for the click on each
div
would be ideal.I'm not very savvy, but using JQuery it would be something like:
您可以使用 CSS 浮动来构建它,例如,如果正方形为 20x20px:
请记住使用具有有效文档类型的 IE http://htmlhelp.com/tools/validator/doctype.html 或在怪异模式下使用盒模型黑客 [http://tantek.com/CSS/Examples/boxmodelhack.html]。
如果您使用 ID 而不是类并在 CSS 中使用单个字母,则可能会减少上述内容的占用。
什么最快通常很大程度上取决于浏览器,例如,如果 IE 在表格方面表现更好,而其他浏览器在相对 DIV/浮动方面表现更好,我不会感到惊讶。
(我还没有测试上面的内容,但类似的东西应该可以工作。)
然后您可以将上面的内容转换为 JavaScript 以减少页面加载时间,例如:
You could build it with CSS floats, e.g if the squares are 20x20px:
Remember to use IE with a valid doctype http://htmlhelp.com/tools/validator/doctype.html or to use box model hacks in quirks mode [http://tantek.com/CSS/Examples/boxmodelhack.html].
You could probably make the above take up less if you use IDs instead of classes and use single letters in the CSS.
What's fastest usually depends wildly on the browser though, e.g. I wouldn't be surprised if IE performs better with tables, while other browsers perform better with relative DIVs/floats.
(I haven't tested the above, but something similar should work.)
You could then convert the above to JavaScript to reduce page load times, e.g:
http://www.w3schools.com/TAGS/tag_map.asp
如果您使用一个图像映射来为您的方格板生成触发器,您只需要重复的背景和一个填充总数和高度的透明图像。
http://www.w3schools.com/TAGS/tag_map.asp
If you use an imagemap to produce the triggers for your checkered board you would just need the repeated background and a transparent image that fills the total with and height.