简体   繁体   English

画布吸引了div问题

[英]Canvas draw behind div issue

I'm using a javascript to draw inside a canvas, but I'm getting this weird issue where I cant "draw behind" the div of text, so when I hover over the div, it stops drawing and starts whenever i hover off the div and the line starts to continue where I hover off, but it renders weird line behind the div instead of line with styling like blur. 我正在使用JavaScript在画布内绘制,但是却遇到了一个奇怪的问题,即我无法在文本div后面“绘制”,因此当我将鼠标悬停在div上时,它将停止绘制并在我将鼠标悬停在div,并且该行开始在我悬停的地方继续,但是它在div后面呈现了怪异的线条,而不是像迷离这样的样式的线条。

At this point, I'd like to solve this problem in the way that i can draw behind the text. 在这一点上,我想以可以在文本后面画线的方式解决此问题。

Here is my code: (https://codepen.io/Zdendaaa/pen/yXWOmQ) 这是我的代码: (https://codepen.io/Zdendaaa/pen/yXWOmQ)

Any help? 有什么帮助吗? Thanks! 谢谢!

To your content div add 向您的内容div添加

pointer-events: none;

According to MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events The CSS property pointer-events allows authors to control under what circumstances (if any) a particular graphic element can become the target of mouse events. 根据MDN: https : //developer.mozilla.org/en-US/docs/Web/CSS/pointer-events CSS属性的指针事件允许作者控制在什么情况下(如果有)特定的图形元素可以变成鼠标事件的目标。

You could just add pointer-events: none as described in the other answer, but this is only a good solution, if you're sure that you don't need any other events on that specific element. 您可以只添加pointer-events: none另一个答案中所述的pointer-events: none ,但是如果您确定不需要在该特定元素上任何其他事件,那么这只是一个好的解决方案。 For example if you want to place an image inside your content div which should have an hover effect or let's say any kind of link, you're stuck with the same problem again. 例如,如果您想在内容div内放置一个具有悬停效果的图像,或者说是任何一种链接,那么您将再次遇到相同的问题。

A better approach would probably be to just wrap the canvas and your content in another div container and just add your mouseover listener to this wrapping element instead. 更好的方法可能是将画布和您的内容包装在另一个div容器中,然后将mouseover侦听器添加到此包装元素中。

In this case you don't have to prohibit certain events on specific elements. 在这种情况下,您不必禁止特定元素上的某些事件。

I edited your pen a bit with an example: https://codepen.io/anon/pen/YQbGKL 我用一个示例对您的笔进行了一些编辑: https : //codepen.io/anon/pen/YQbGKL

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

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