简体   繁体   English

如何在两层画布的底部画布上注册click事件

[英]How do I register a click event on the bottom canvas of two layered canvases

I have a game I'm working on which will have multiple canvases. 我正在开发一个游戏,其中包含多个画布。 One for the map, another for user interface, another for game objects, etc. The user interface canvas will be the top most canvas, but I'm wondering, if I wanted to register a click event on say, the map layer, how would I do so with the ui canvas being on top of every other canvas? 一个用于地图,另一个用于用户界面,另一个用于游戏对象,等等。用户界面画布将是最上面的画布,但是我想知道是否要在地图层上注册一个click事件,如何我会在ui画布位于其他所有画布之上的情况下这样做吗? As it stands now, when I click, it only registers the click on the ui canvas, which is what it's supposed to since it's sitting on top of the others. 就目前而言,当我单击时,它只将单击注册在ui画布上,这是应该的,因为它位于其他画布之上。 How do I register the click on one of the other canvases instead? 如何在其他画布之一上注册点击?

CSS 的CSS

canvas{
    position: absolute;
}

#ui{
    bottom: 0;
    z-index: 5;
}

#basemap{
    z-index: 0;
}

HTML 的HTML

<canvas id="ui"></canvas>
<canvas id="basemap"></canvas>

JS JS

var canvases = document.getElementsByTagName("canvas");

for(var i = 0; i < canvases.length; i++){
    canvases[i].onclick = function(){
        console.log(this);
    }
}

Pen 钢笔

Set pointer-events to none on the #ui element. #ui元素#ui pointer-events设置为none This will cause pointer-related events to be ignored by #ui and pass through to the underlying element: 这将导致与指针相关的事件被#ui忽略,并传递到基础元素:

#ui{
    bottom: 0;
    z-index: 5;
    pointer-events: none;
}

Set pointer-events CSS attribute to none on #ui element, it will disable the click on it. pointer-events CSS属性在#ui元素上设置为none ,将禁用对其的单击。

#ui {
  bottom: 0;
  z-index: 5;
  pointer-events: none;
}

the value none instructs the mouse event to go "through" the element and target whatever is "underneath" that element instead. 值none指示鼠标事件“遍历”该元素并以该元素“之下”的对象为目标。

Pointers event information 指针事件信息

The canvas does not provide a UI so you don't need to register both canvas events. 画布不提供UI,因此您不需要注册两个画布事件。 You will be needing to check the click, mousedown, mouseup events against the UI coordinates and current mouse state (dragging). 您将需要根据UI坐标和当前鼠标状态(拖动)检查click,mousedown,mouseup事件。 If the mouse is over a UI graphic you handle the UI requirements, if not you handle the game map's needs. 如果鼠标悬停在UI图形上,则可以处理UI要求,否则,可以满足游戏地图的需要。 As the mouse coordinates will be the same for both canvas there is no point in adding an additional redundant event listener, it will only make the code overly complex. 由于两个画布的鼠标坐标都相同,因此没有必要添加额外的冗余事件侦听器,这只会使代码过于复杂。

Additionally you should not react to mouse events in the event handlers. 此外,您不应在事件处理程序中对鼠标事件做出反应。 Mouse event handlers can fire much more often than the canvas will be updated. 鼠标事件处理程序的触发频率比画布更新的触发频率高得多。 You should use the event handlers to just record the current mouse state (location, buttons down) and in the main game loop is where you handle the mouse interaction. 您应该使用事件处理程序来记录当前的鼠标状态(位置,按下按钮),并且在主游戏循环中可以处理鼠标交互。 This will reduce the amount of code that needs to run per mouse event and greatly simplifies the code. 这将减少每个鼠标事件需要运行的代码量,并大大简化了代码。

Mouse event handlers are designed to simplify the interaction with individual UI components, the canvas breaks the paradigm by not being able to define the UI / map components as distinct registrable items with events. 鼠标事件处理程序旨在简化与单个UI组件的交互,画布通过无法将UI /地图组件定义为带有事件的不同可注册项而打破了范例。 Treating the canvas as if it is a DOM will be a disadvantage to the overall code quality and game performance. 将画布当作DOM对待将不利于整体代码质量和游戏性能。

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

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