【问题标题】:AjaxToolKit: Balloon Popup Extender on html5 CanvasAjaxToolKit:html5 画布上的气球弹出扩展器
【发布时间】:2013-04-29 16:23:48
【问题描述】:

我在我的项目中实现了一个画布绘图。我想显示带有与画布上特定鼠标点击相关的信息的语音气泡。假设单击我的画布的特定区域会弹出一个弹出窗口,其中提供有关画布上该区域的文本信息。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js"></script>


<ajaxToolkit:BalloonPopupExtender ID="BalloonPopupExtender1" 
    runat="server"  
    TargetControlID="myCanvas" 
    BalloonPopupControlID="Panel1"
    Position="TopLeft" 
    BalloonStyle="Cloud"
    BalloonSize="Small"
    CustomCssUrl="CustomStyle/BalloonPopupOvalStyle.css"
    CustomClassName="oval"
    UseShadow="true" 
    ScrollBars="Auto"
    DisplayOnMouseOver="true"
    DisplayOnFocus="false"
    DisplayOnClick="true" />

<canvas id="myCanvas" width="915" height="850" style="border: 2px double #000000;"></canvas>
<script type="text/javascript">

但是这不起作用,我相信这是因为 myCanvas 不是 asp.net/server 端元素,因此 runat ="server" 不是正确的方法。

我目前正在关注这些博客:-

http://www.c-sharpcorner.com/UploadFile/364074/balloonpopupextendercontrol-in-ajax/

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/BalloonPopup/BalloonPopupExtender.aspx

知道如何在点击时在画布上实现弹出窗口吗?

【问题讨论】:

    标签: javascript asp.net c#-4.0 html5-canvas ajaxcontroltoolkit


    【解决方案1】:

    鉴于您想要响应对特定画布坐标的点击,您可能必须在客户端工作。这是在客户端弹出信息的代码。

    这段代码将在 separate 画布元素内绘制一个信息气球。画布被移出屏幕,直到需要它为止。当然,您可以以任何您想要的方式设置此弹出气球的样式,因为它是一个画布元素!

    <canvas id="balloon" width=105 height=105></canvas>
    
    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});
    } 
    

    如果你不习惯客户端javascript,这里是如何获取目标填充画布相对于窗口的位置

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

    下面是如何监听目标填充画布上的点击。

    // listen for clicks on the canvas
    $("#canvas").click(function(e){ 
    
        // get the mouseclick position
        mouseX=parseInt(e.clientX-offsetX);
        mouseY=parseInt(e.clientY-offsetY);
    
        // see if we clicked on any targets, show the balloon
        for(var i=0;i<circles.length;i++){
            var circle=circles[i];
            var dx=circle.x-mouseX;
            var dy=circle.x-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);
            }
        }
    });
    

    要测试是否点击了画布上的任何目标位置,我们可以像这样测试目标周围的圆形区域:

        var dx=targetCircle.x-mouseX;
        var dy=targetCircle.x-mouseY;
        var radius=targetCircle.radius;
        // true if we clicked in the target circle
        if(dx*dx+dy*dy<=radius*radius){
            // we hit a target, display the balloon
        }
    

    这是工作示例代码和小提琴:http://jsfiddle.net/m1erickson/AJvkN/

    <!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{ 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 circles=[];
        circles.push({x:40,  y:30, radius:15,color:"red",  info:"I'm red."});
        circles.push({x:150, y:150,radius:25,color:"green",info:"I'm centered."});
        circles.push({x:110, y:85, radius:40,color:"blue", info:"I'm big."});
    
        // draw the target circles on the canvas
        for(var i=0;i<circles.length;i++){
            drawCircle(circles[i]);
        }
    
        // 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);
    
            // see if we clicked on any targets
            for(var i=0;i<circles.length;i++){
                var circle=circles[i];
                var dx=circle.x-mouseX;
                var dy=circle.x-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 hideBalloon(e){ $("#balloon").css({left:-200}); });
    
    
        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});
        }
    
    }); // end $(function(){});
    </script>
    
    </head>
    
    <body>
        <p>Click circles to popup an info balloon</p>
        <p>When info balloon appears, click it do dismiss it</p>
        <canvas id="canvas" width=300 height=300></canvas>
        <canvas id="balloon" width=105 height=105></canvas>
    </body>
    </html>
    

    【讨论】:

    • 太棒了。我确实有办法弄清楚画布点击和坐标。基本上我的画布是一个历史事件的时间线。现在,当用户单击一个事件时,我想显示有关该事件的更多信息。谢谢!
    • 嘿,MarkE,我无法按照您向我展示的方式重新设计我的气球:- jsfiddle.net/wCnhv/2 我正在尝试效仿:- scriptol.com/html5/canvas/speech-bubble.php 但失败得很惨。你能帮我吗?我想制作一个更大的气球,以便在其中容纳更多数据。
    • 这个模型,似乎不适用于我的模型。 jsfiddle.net/PpLMb/1我的问题是,我想让我的泡泡更大,这样我就可以在里面放更多的信息。我尝试使用行尾字符(\n\r)......但这也不起作用。 :((
    • 另外,当我尝试将您的模型包含在我之前的模型中时,对话气泡只显示了一部分。
    • 我调整了我的小提琴,让它对你来说更简单。气泡可在垂直和水平方向调整大小。您传入必须调整大小以反映新气泡大小的气泡画布。气泡“耀斑”正好是气泡高度的一半,因此当您定位气泡时,它应该与您的命中区域对齐。画布文本没有换行符,因此您必须测量累积的文本长度...如果下一个单词将超出右边缘,您应该向下移动 Y 位置并开始一个新的填充文本。您可以使用 context.measureText("abc def").width 来测量您的文本宽度。
    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2010-10-29
    • 2012-08-10
    • 2021-11-27
    相关资源
    最近更新 更多