防止意外选择/拖动突出显示

发布于 2024-11-29 16:04:35 字数 1260 浏览 1 评论 0原文

我有一个使用 html5 canvas 的绘图应用程序。当用户绘图并且笔滑出画布区域时,chrome 会以浅蓝色或黄色突出显示页面上的 html 元素。

screen shot

这会破坏绘图体验。有没有办法避免这种亮点的发生呢?

事件处理和绘制代码基于这篇文章: http://jsfiddle.net/rnNFB/1/< /a>

var x ;
var y ;

var lower = $('#lower').get(0).getContext('2d') ;
var upper = $('#upper').get(0).getContext('2d') ;

var dragging = false ;

function drawStroke(ctx){
    var i ;
    ctx.strokeStyle='rgba(0,0,0,0.5)' ;
    ctx.lineWidth=10 ;
    ctx.beginPath() ;
    ctx.moveTo(x[0],y[0]) ;
    for (i=1; i < x.length; i++){
        ctx.lineTo(x[i],y[i]) ;
    }
    ctx.stroke() ;
}

$('#upper').mousedown(function(e){
    x=[e.layerX];
    y=[e.layerY];
    dragging=true}) ;

$('#upper').mousemove(function(e){
    if (dragging){
        x.push(e.layerX);
        y.push(e.layerY);
        upper.clearRect(0,0,upper.canvas.width,upper.canvas.height) ;
        drawStroke(upper) ;
    }}) ;

$('#upper').mouseup(function(e){
    dragging = false ;
    upper.clearRect(0,0,upper.canvas.width,upper.canvas.height) ;
    drawStroke(lower) ;
}) ;

如果您在画布标签上方添加一些 h1 标签,然后在画布上绘制,拖动到边界框之外,您将看到蓝色突出显示。有没有办法阻止这种行为?

I have a drawing application using html5 canvas. When the user is drawing and pen slips out of the canvas area, chrome highlights html elements on the page in light blue or yellow.

screen shot

This is disruptive to the drawing experience. Is there a way to prevent such highlight from happening?

The event handling and drawing code is based off of this post: http://jsfiddle.net/rnNFB/1/

var x ;
var y ;

var lower = $('#lower').get(0).getContext('2d') ;
var upper = $('#upper').get(0).getContext('2d') ;

var dragging = false ;

function drawStroke(ctx){
    var i ;
    ctx.strokeStyle='rgba(0,0,0,0.5)' ;
    ctx.lineWidth=10 ;
    ctx.beginPath() ;
    ctx.moveTo(x[0],y[0]) ;
    for (i=1; i < x.length; i++){
        ctx.lineTo(x[i],y[i]) ;
    }
    ctx.stroke() ;
}

$('#upper').mousedown(function(e){
    x=[e.layerX];
    y=[e.layerY];
    dragging=true}) ;

$('#upper').mousemove(function(e){
    if (dragging){
        x.push(e.layerX);
        y.push(e.layerY);
        upper.clearRect(0,0,upper.canvas.width,upper.canvas.height) ;
        drawStroke(upper) ;
    }}) ;

$('#upper').mouseup(function(e){
    dragging = false ;
    upper.clearRect(0,0,upper.canvas.width,upper.canvas.height) ;
    drawStroke(lower) ;
}) ;

If you add some h1 tags above the canvas tags then draw on the canvas, dragging outside the bounding box, you will see a blue highlight. Is there a way to prevent this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初熏 2024-12-06 16:04:35

将以下 CSS 类应用到应该不可选择的元素。它也可以仅应用于正文(不过,最佳实践可能是仅禁用真正需要它的元素上的用户选择)。

.unselectable {
  user-select: none;
}

离题,但它对于应用程序来说可能也很有趣:如果您在 jsFiddle 中选择较大的 lineWidth 并非常缓慢地移动鼠标,您可以在草图中看到一些丑陋的效果(块)。请始终检查从 onmousemove(拖动时)坐标到最后一个坐标的距离是否太小。例如,仅当距离大于线距的约 1/6 时,才将坐标添加到草图。

Apply the following CSS class to the elements that should be unselectable. It can also just be applied to body (though, best practise might be to only disable user selection on elements that really need it).

.unselectable {
  user-select: none;
}

off-topic but it might also be interesing for the app: If you select a big lineWidth in the jsFiddle and move the mouse very slowly, you can see some ugly effects (blocks) in the sketches. Always check that the distance from the onmousemove (while dragging) coordinate to the last coordinate is not too small. For example, only add the coordinates to the sketch if the distance is more than about 1/6 of the linewith.

征﹌骨岁月お 2024-12-06 16:04:35

我不会使用document.onselectstart。您所要做的就是将其放在有问题的画布上:

canvas.onselectstart = function() { return false; 此外,

您可以通过将 addEventListener 的最后一个参数设置为 true 来捕获画布上发生的鼠标按下/移动/向上事件。即使鼠标离开画布,这也能让您的绘图完美继续。

I wouldn't use document.onselectstart. All you have to do is put this on the canvas in question instead:

canvas.onselectstart = function() { return false; };

Additionally you could capture the mouse down/move/up events that are occuring on your canvas by setting the last argument of addEventListener to true. this will let your drawing continue perfectly even if the mouse leaves the canvas.

梦魇绽荼蘼 2024-12-06 16:04:35

尝试这个脚本,如果拖动设置为 true,它将禁用选择。这样,您在不绘图时仍然可以选择文本。

document.onselectstart = function( e ) { 
  if (dragging) { 
    e.preventDefault();
    return false;
  }
};

Try this script, it would disable selection if dragging is set to true. This way you can still select text when you are not drawing.

document.onselectstart = function( e ) { 
  if (dragging) { 
    e.preventDefault();
    return false;
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文