简体   繁体   English

悬停在画布弧上后,如何将div附加(显示)到画布弧上?

[英]How to append(show) a div to a canvas arc after hovering on the canvas arc?

Simply, my program is to put annotations on image. 简而言之,我的程序是在图像上添加注释。 when the user clicks with the mouse on specific point on the image, it draws a canvas arc containing a number(that is working). 当用户在图像的特定点上单击鼠标时,它将绘制一个包含数字的画布弧(有效)。 What i wanna add is when the user clicks on the image, it draws the canvas arc containing a number and shows a div containing two editable text areas and when the mouse leaves, the 2 text areas go hidden with respect to save the position of each annotation with the values in the 2 text areas. 我要添加的是用户单击图像时,它绘制包含数字的画布弧,并显示一个包含两个可编辑文本区域的div,并且当鼠标离开时,相对于两个文本区域而言,它们将被隐藏以保存每个文本区域的位置带2个文本区域中的值的注释。

here is my code: 这是我的代码:

<canvas width="1600" height="1600" 
style="background-image:url('dermatome.jpg');" id="special">
    <div id="re">
        <textarea id="text1" rows="4" cols="50" hidden></textarea>
        <textarea id="text2" rows="4" cols="50" hidden></textarea>
    </div>
</canvas>

    <script>

     var count = 1;

     jQuery(document).ready(function(){
     $("#special").click(function(e){ 

        //get mouse coordinates
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop; 


        var ctx= this.getContext("2d"); 
        ctx.beginPath();

        ctx.arc(x, y, 20,0, 2*Math.PI);
        ctx.fillStyle = "rgb(200,0,0)";
        ctx.fill();
        ctx.font = '17pt Calibri';
        ctx.fillStyle = 'white';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle'
        ctx.fillText(count, x, y);
        count++;

        $(this).mouseover(function() {
        $('#re').show();    })         });         })         </script>

What i wanna add is when the user clicks on the image [ #1 ], it draws the canvas arc containing a number [ #2 ] and shows a div containing two editable text areas [ #3 ] and when the mouse leaves [ #4 ], the 2 text areas go hidden [ #5 ] with respect to save the position of each annotation with the values in the 2 text areas [ #6 ]. 我要添加的是用户单击图像[ #1 ]时,它绘制包含数字[ #2 ]的画布弧,并显示一个包含两个可编辑文本区域[ #3 ]的div,以及当鼠标离开[ #4 ]时],则将两个文本区域隐藏起来[ #5 ],以将每个注释的位置与两个文本区域[ #6 ]中的值一起保存。

  1. Listen for clicks on the canvas using #special's .click . 使用.click.click监听画布上的点击。

  2. Use your "numbered arc" code from your question. 使用问题中的“编号弧”代码。

  3. Canvas cannot be a parent so wrap #special & #re in a new container div (#wrapper). 画布不能是父项,因此请将#special&#re包装在新的容器div(#wrapper)中。

    • Set #wrapper as position:relative and #special & #re as position:absolute . 将#wrapper设置为position:relative并将#special和#re设置为position:absolute
    • On #special's click, set #re's left & top positions to the click position. 在#special的点击上,将#re的lefttop位置设置为点击位置。
    • Use #re's .show to toggle its visibility to visible. 使用.show.show将其可见性切换为可见。
    • Use #text1's & #text2's .val to fill / clear their text contents as desired. 使用#text1和#text2的.val根据需要填充/清除其文本内容。 If you want to re-show the users previous texts, fetch those previous texts from the appropriate javascript object and refill the text using .val(annotations[n].text1) and .val(annotations[n].text2) . 如果要重新显示用户先前的文本,请从相应的javascript对象中获取这些先前的文本,然后使用.val(annotations[n].text1) and .val(annotations[n].text2)重新填充文本。
  4. "...when the mouse leaves" ... hmmmmmm, leaves what? “……当鼠标离开时……”……嗯,离开了什么? Perhaps help the user understand better by adding a "Save" button with the 2 textareas. 也许通过在两个文本区域中添加一个“保存”按钮来帮助用户更好地理解。

  5. Use #re's .hide to toggle its visibility to hidden. 使用.hide.hide将其可见性切换为隐藏。

  6. Use a javascript object to save the click position and the textarea values. 使用javascript对象保存点击位置和textarea值。

     var annotations=[]; annotations.push({ x:x, // == x from #special's click handler y:y, // == y from #special's click handler text1: $('#text1').val(), text2: $('#text2').val(), }); 

If you want the user be able to click on a previous numbered circle, you can test the mouseclick position vs each [x,y] in annotations[] using this mouse-in-circle formula: 如果您希望用户能够单击上一个编号的圆圈,则可以使用此圆形鼠标公式来测试mouseclick位置与annotations []中每个[x,y]的关系:

var dx=mouseX-annotations[n].x;
var dy=mouseY-annotations[n].y;
if(dx*dx+dy*dy<20*20){  // 20 is the radius of your circle ;-)
    // the mouse is inside circle#n
}

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

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