简体   繁体   English

覆盖画布对象,一起模糊吗?

[英]Overwriting a canvas object, blurring together?

I am a novice canvas programmer, just playing around with the idea of a hexagonal grid being a "game board" of sorts. 我是一个新手画布程序员,只是在玩六角形网格是某种“游戏板”的想法。

I took some code that I found online, and added to one of the methods to provide a "highlighting" capability. 我获取了一些在网上找到的代码,并将其添加到一种提供“突出显示”功能的方法中。 Inside the drawHex function, the strokeStyle will change to a neon blue color, and draw a neon blue hex on top of the board (indicating that hexagon is "highlighted"). 在drawHex函数内部,strokeStyle将变为霓虹蓝色,并在板子顶部绘制一个霓虹蓝色十六进制(指示六角形已“突出显示”)。 Then, if you click the same hex, it will redraw a black hexagon on top of it, in order to "erase" the highlight. 然后,如果您单击相同的十六进制,它将在其顶部重绘一个黑色六边形,以便“擦除”突出显示。 However, if you check it out in this jsfiddle: http://jsfiddle.net/DHW7f/ you'll see that highlighting a hexagon then clicking it again, results in some of the neon blue color remaining in the blurred edges of the hexagon. 但是,如果您在以下jsfiddle中进行检查: http : //jsfiddle.net/DHW7f/,您将看到突出显示六边形然后再次单击它,会导致六边形的模糊边缘中残留一些霓虹蓝色。

As I said, I am completely new to the idea of canvas... is there a better way to "highlight" an object like that? 正如我说的那样,我对画布的想法是全新的……是否有更好的方法来“突出”这样的对象? If not, is there a way to fix this blur? 如果没有,是否有办法解决这种模糊? I tried making the neon blue hexagon linewidth smaller, but it didn't seem to have an effect. 我尝试使霓虹灯蓝色六边形的线宽变小,但似乎没有效果。

This is the particular code involved in the highlighed hexagon: 这是高亮六边形所涉及的特定代码:

HexagonGrid.prototype.drawHex = function(x0, y0, fillColor, debugText, highlight, revert)     {
if (highlight == true && revert == false){
    this.context.strokeStyle = "#00F2FF";
}

Remember that on a <canvas> element, stuff gets drawn and it's not remembered: It's just drawn. 请记住,在<canvas>元素上,东西被绘制,并且不被记住:它只是被绘制。 Right now, you're drawing hexagons on top of hexagons. 现在,您正在六边形上绘制六边形。 This is a very efficient way to highlight and un-highlight shapes, but if you really want to get rid of the blur, you need to change the whole style of your program: 这是一种突出显示和取消突出显示形状的非常有效的方法,但是,如果您真的想摆脱模糊,则需要更改程序的整体样式:

  • Make a Hexagon constructor and then the HexagonGrid a kind of "system" of Hexagon s. 创建一个Hexagon构造函数,然后HexagonGrid一个HexagonGridHexagon的“系统”。 Store the Hexagon s in a hexes[] property. Hexagon保存在hexes[]属性中。
  • Don't use as many parameters in functions. 在函数中不要使用太多参数。 Save more stuff like location or stroke as properties so they're more constant. 将位置或笔触等更多内容另存为属性,以使它们更稳定。 This makes up for shapes drawn on the canvas not already being saved. 这弥补了尚未保存在画布上绘制的形状。
  • Transfer the .drawHex() method to the Hexagon prototype and then find a new system of finding a Hexagon in HexagonGrid.prototype.clickEvent() using the new system. .drawHex()方法转移到Hexagon原型,然后使用新系统在HexagonGrid.prototype.clickEvent()中找到一个用于查找Hexagon的新系统。 I wouldn't suggest a Hexagon.prototype.isMouseOn() method and looping through the hexes[] property because that would be inefficient. 我不建议使用Hexagon.prototype.isMouseOn()方法并通过Hexagon.prototype.isMouseOn() hexes[]属性进行循环,因为那样会导致效率低下。 You should do something similar to your current method where the hex clicked is calculated based on the mouse position. 您应该执行与当前方法类似的操作,在当前方法中,单击的十六进制是根据鼠标位置计算的。
  • Once you've found the right hex in HexagonGrid.prototype.clickEvent() , change the Hexagon 's stroke property and the stroke property of the old selected Hexagon and then redraw the whole grid. HexagonGrid.prototype.clickEvent()找到合适的十六进制后,更改Hexagon的stroke属性和旧选择的Hexagon的stroke属性,然后重绘整个网格。 This is inefficient compared to what you're doing now, but it's the only way to overwrite everything. 与您现在正在执行的操作相比,这种方法效率低下,但这是覆盖所有内容的唯一方法。 When you redraw the grid, make sure to "clear the canvas" by drawing a white background over everything. 重新绘制网格时,请确保通过在所有内容上绘制白色背景来“清除画布”。

I know that the solution I came up with is rather abstract, will probably take a lot of work to integrate, and will overall be less efficient since you're redrawing everything on each click, but unfortunately, that's what happens with <canvas> elements where there's no way alter previously drawn shapes. 我知道我想出的解决方案是相当抽象的,可能会花费很多工作来集成,并且总体上效率会降低,因为您每次单击都会重新绘制所有内容,但是不幸的是,这就是<canvas>元素发生的情况无法改变以前绘制的形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM