简体   繁体   English

如何从显示在另一个画布元素上方的一个画布元素中删除

[英]how to remove one canvas element from displaying on top of another canvas element

I have 2 canvases. 我有2张画布。 One, the main canvas. 一,主画布。 Upon which all is drawn, Second, the speech bubble canvas (balloon). 在其上绘制所有内容,其次是语音气泡画布(气球)。 Which displays information about specific regions on my main canvas upon client clicks. 当客户单击时,它将在我的主画布上显示有关特定区域的信息。

I was playing around with my canvas after introducing the speech bubble and came across an issue. 介绍演讲泡沫后,我在玩我的画布,遇到一个问题。

This is a simple code that shows how the speech bubble is introduced:- 这是一个简单的代码,显示了如何引入气泡:-

http://jsfiddle.net/m1erickson/AJvkN/ http://jsfiddle.net/m1erickson/AJvkN/

My canvas is a timeline, and is scrollable; 我的画布是一个时间轴,并且可以滚动; has historical events plotted on it. 上面有历史事件。 Once a user clicks on an event a speech bubble appears. 用户单击事件后,就会出现气泡。

Now what I don't want to happen is, when a client clicks on the canvas, a speech bubble appears and then scrolls, the speech bubble moves to a new position on the scrolled image, however still showing information about the previous location. 现在,我不想发生的事情是,当客户单击画布时,会出现一个气泡,然后滚动,该气泡将移动到滚动图像上的新位置,但是仍然显示有关先前位置的信息。

For this we have the hideballoon () which assigns css property left : -200. 为此,我们有hideballoon() ,它向左分配css属性:-200。 However this still causes inconsistencies. 但是,这仍然导致不一致。 For example if I drag the canvas from left to right, the balloon doesn't disappear with the scroll, but reappears in a new position. 例如,如果我从左向右拖动画布,则气球不会随着滚动而消失,而是重新出现在新位置。

there is a .remove() function $("#balloon").remove() 有一个.remove()函数$("#balloon").remove()

http://api.jquery.com/remove/ http://api.jquery.com/remove/

This successfully removes the balloon however, the issue with this is:- it removes the balloon completely, and no future clicks will pop up any more speech bubbles. 这成功删除了气球,但问题是:-完全删除了气球,以后不会再有任何点击声弹出。 This is not what I want. 这不是我想要的。 I want something dynamic. 我想要一些动态的东西。

Click on event >> speech bubble appears >> scroll canvas >> speech bubble disappears >> click on canvas >> speech bubble pertaining to new click appears back >> and so on and so forth. 单击事件>>出现气泡>>滚动画布>>气泡消失>>单击画布>>与新单击有关的气泡回到>> >>依此类推。

[Edited] [编辑]

Use .show() and .hide() to keep the balloon out of your way when its not needed 使用.show()和.hide()可以在不需要气球时将气球挡住

When the user scrolls the window then just hide the balloon. 当用户滚动窗口时,只需隐藏气球即可。

I assume you're scrolling the window instead of the canvas. 我假设您正在滚动窗口而不是画布。 If you're scrolling the canvas, just use $("#canvas").scroll( ... ) instead. 如果要滚动画布,则只需使用$(“#canvas”)。scroll(...)。

So when you need the balloon: 因此,当您需要气球时:

        // move the balloon canvas to the target
        $("#balloon").css({left:offsetX+X, top:offsetY+Y});

        // and show it
        $("#balloon").show();

And hide the balloon when the user clicks on it or when the window scrolls: 当用户单击气球或窗口滚动时,隐藏气球:

    // listen for clicks on the balloon and then hide the balloon
    $("#balloon").click(function(e){ $("#balloon").hide(); });

    // listen for scrolls and then hide the balloon
    $(window).scroll(function(e){
        $("#balloon").hide(); 
    });

Here's working sample code and a Fiddle: http://jsfiddle.net/m1erickson/uWHkv/ 这是工作示例代码和小提琴: http : //jsfiddle.net/m1erickson/uWHkv/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ width:2000px; background-color: ivory; padding:10px;padding-top:100px; }
    #canvas{border:1px solid red;}
    #balloon{ position:absolute; left:-200px; }
</style>

<script>
$(function(){

    // get reference to our drawing canvas
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    // get reference to our balloon canvas
    var balloon=document.getElementById("balloon");
    var popCtx=balloon.getContext("2d");

    // get the position of canvas relative to window
    var canvasOffset=$("#canvas").offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;

    // define some targets and their basic info
    var count=1;
    var circles=[];
    for(var x=50;x<1900;x+=50){
        circles.push({
            x:x,  y:120, radius:20,
            color:"blue",  
            info:"I'm #"+(count++)});
    }

    // draw the target circles on the canvas
    ctx.fillStyle="yellow";
    ctx.font="16px verdana";
    for(var i=0;i<circles.length;i++){
        drawCircle(circles[i]);
        ctx.beginPath();
        ctx.fillText(i+1,circles[i].x-8,circles[i].y+5);
    }

    // listen for clicks on the canvas and show the balloon
    $("#canvas").click(function(e){ 

        // get the mouseclick position
        mouseX=parseInt(e.clientX-offsetX);
        mouseY=parseInt(e.clientY-offsetY);

        // account for the window scrolling
        var scrollX=$(window).scrollLeft();
        var scrollY=$(window).scrollTop();

        // see if we clicked on any targets
        for(var i=0;i<circles.length;i++){
            var circle=circles[i];
            var dx=(circle.x-scrollX)-mouseX;
            var dy=(circle.y-scrollY)-mouseY;
            var radius=circle.radius;
            // true if we clicked in the target circle
            if(dx*dx+dy*dy<=radius*radius){
                drawBalloon(circles[i].x+radius,circles[i].y-100,circles[i].info);
            }
        }
    });


    // listen for clicks on the balloon and then hide the balloon
    $("#balloon").click(function(e){ $("#balloon").hide(); });

    // listen for scrolls and then hide the balloon
    $(window).scroll(function(e){
        $("#balloon").hide(); 
    });


    function drawCircle(circle){
        ctx.save();
        ctx.beginPath();
        ctx.fillStyle=circle.color;
        ctx.strokeStyle="black";
        ctx.lineWidth=3;
        ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();
        ctx.restore();
    }


    function drawBalloon(X,Y,theInfo){
        popCtx.save();
        popCtx.fillStyle="#FD0";
        popCtx.strokeStyle="#000";
        // draw the balloon
        popCtx.beginPath();
        popCtx.moveTo(52,02);
        popCtx.quadraticCurveTo(02,02,02,42);
        popCtx.quadraticCurveTo(02,77,27,77);
        popCtx.quadraticCurveTo(27,102,07,102);
        popCtx.quadraticCurveTo(37,102,42,77);
        popCtx.quadraticCurveTo(102,77,102,42);
        popCtx.quadraticCurveTo(102,02,52,02);
        popCtx.lineWidth=3;
        popCtx.stroke();
        popCtx.fill();
        // draw theInfo
        popCtx.font="10pt arial";
        popCtx.fillStyle="black";
        popCtx.fillText(theInfo,10,50);
        popCtx.restore();
        // move the balloon canvas to the target
        $("#balloon").css({left:offsetX+X, top:offsetY+Y});
        $("#balloon").show();
    }

}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=1950 height=300></canvas>
    <canvas id="balloon" width=105 height=105></canvas>
</body>
</html>

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

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