简体   繁体   English

鼠标悬停时弹出包含画布上点坐标的标签

[英]Pop up label containing coordinate of a dot on canvas when mouse hovers

I am new to HTML and JavaScript and want to implement a feature such that on a canvas, I can draw dots by clicking on it, and when I hover the mouse above the dots, it will pop up a label containing the coordinate of the dot. 我是HTML和JavaScript的新手,想实现一个功能,以便在画布上单击可以绘制点,然后将鼠标悬停在这些点上方时,它将弹出一个包含点坐标的标签。 。 I have implemented the draw dots part, I just can't figure out how to implement the label pop-up part. 我已经实现了绘制点部分,但是我只是想不出如何实现标签弹出部分。

in HTML: 在HTML中:

<div id="2" style="position:absolute; top:100px; left:10px;">
    <canvas id="canvas_prime" width="700" height="500"></canvas>
</div>

in JS: 在JS中:

var mouseX, mouseY;
function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
        x: (evt.clientX - rect.left),
        y: -(evt.clientY - rect.top)};
}

function drawDot(canvas) {
    var context = canvas.getContext("2d");

    context.beginPath();
    context.arc(mouseX * 50, mouseY * 50, 4, 0, 2 * Math.PI, false);

    context.fillStyle = 'green';
    context.fill();

    context.lineWidth = 1;
    context.strokeStyle = 'yellow';
    context.stroke();

    drawLine_passive(canvas);
}

On canvas's mousemove event, check for reaching mouse on your dot's x, y position. 在画布的mousemove事件中,检查鼠标在点的x,y位置上是否到达。 If reached, append a popup div on your canvas's parent with position: absolute and coordinates as your dot's coordinates. 如果达到,则在画布的父级上添加一个弹出式div,其位置为:绝对值和坐标作为点的坐标。

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

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