使用jQuery和Canvas从html元素ID到另一个html元素画一条线

use*_*200 4 html javascript css jquery canvas

是否可以仅通过引用元素ID来用html和jquery画一条线?我的文字中有一个重要的词,想在这个词和描述它的图像之间划一条界线。我已经看到可以在画布之间绘制元素,但是它们的样式位置设置为绝对。由于我的元素是文本中的单词,因此无法将其设置为绝对。例

<p>This is my text with this very <span id="important_word">important</span> word</p>
...
<img src="important.jpg" id="important_img"/>
Run Code Online (Sandbox Code Playgroud)

现在我要在跨度和img之间画一条线。可能吗?

提前致谢!

Phi*_*l H 5

由于这不时出现,因此我付出了一些努力。它不是jquery,因此您可以在某种程度上进行简化。仅供参考,这个答案也发布在另一个问题的答案中,但要求是相同的。使用该问题的html和CSS,这里有一个jsbin演示http://jsbin.com/guken/3/

示例输出

方法是创建一个浮动画布元素(粉红色阴影),并将其放置在DOM的其余部分下面(使用z-index)。然后,我计算两个框的边界上与框中心之间的线相对应的点。红色和蓝色方块实际上是随行尾移动的div,可用于注释,如源,目标等。

在该jsbin中,您可以单击一个元素,然后准备一行以单击下一个元素。它会检测到选定元素的悬停,如果悬停在目标上,则会捕捉到目标。

我不会在此处粘贴所有代码,但是在客户端DOM坐标中从一个x,y位置到另一位置绘制一条线的位置是:

var lineElem;
function drawLineXY(fromXY, toXY) {
    if(!lineElem) {
        lineElem = document.createElement('canvas');
        lineElem.style.position = "absolute";
        lineElem.style.zIndex = -100;
        document.body.appendChild(lineElem);
    }
    var leftpoint, rightpoint;
    if(fromXY.x < toXY.x) {
      leftpoint = fromXY;
      rightpoint = toXY;
    } else {
      leftpoint = toXY;
      rightpoint = fromXY;
    }

    var lineWidthPix = 4;
    var gutterPix = 10;
    var origin = {x:leftpoint.x-gutterPix, 
                  y:Math.min(fromXY.y, toXY.y)-gutterPix};
    lineElem.width = Math.max(rightpoint.x - leftpoint.x, lineWidthPix) + 
      2.0*gutterPix;
    lineElem.height = Math.abs(fromXY.y - toXY.y) + 2.0*gutterPix;
    lineElem.style.left = origin.x;
    lineElem.style.top = origin.y;
    var ctx = lineElem.getContext('2d');
    // Use the identity matrix while clearing the canvas
    ctx.save();
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, lineElem.width, lineElem.height);
    ctx.restore();
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#09f';
    ctx.beginPath();
    ctx.moveTo(fromXY.x - origin.x, fromXY.y - origin.y);
    ctx.lineTo(toXY.x - origin.x, toXY.y - origin.y);
    ctx.stroke();
}
Run Code Online (Sandbox Code Playgroud)

由于该示例仅是一行,并且我们始终可以存储已经准备好创建更多行的行,因此它使用了全局变量lineElem。第一次绘制线条时,它会创建一个canvas元素,将其插入DOM并将其分配给lineElem。完成此构造后,它随后重用canvas元素,更改大小并为新的坐标对重绘。

为防止线条被画布的边缘截断,有一个装订线设置可填充画布的宽度和高度。剩下的只是使客户端DOM坐标与在画布本身上绘制的坐标之间的坐标转换正确。

唯一的非直截了当的位是计算沿线的框的边界上的点的坐标。这并不完美,但这是一个合理的开始。关键在于to从源(from)点的角度计算目标()点的角度,并查看与已知的角形盒角如何比较:

function getNearestPointOutside(from, to, boxSize) {
    // which side does it hit? 
    // get the angle of to from from.
    var theta = Math.atan2(boxSize.y, boxSize.x);
    var phi = Math.atan2(to.y - from.y, to.x - from.x);
    var nearestPoint = {};
    if(Math.abs(phi) < theta) { // crosses +x
        nearestPoint.x = from.x + boxSize.x/2.0;
        nearestPoint.y = from.y + ((to.x === from.x) ? from.y : 
        ((to.y - from.y)/(to.x - from.x) * boxSize.x/2.0));
    } else if(Math.PI-Math.abs(phi) < theta) { // crosses -x
        nearestPoint.x = from.x - boxSize.x/2.0;
        nearestPoint.y = from.y + ((to.x === from.x) ? from.y : 
        (-(to.y - from.y)/(to.x - from.x) * boxSize.x/2.0));
    } else if(to.y > from.y) { // crosses +y
        nearestPoint.y = from.y + boxSize.y/2.0;
        nearestPoint.x = from.x + ((to.y === from.y) ? 0 : 
        ((to.x - from.x)/(to.y - from.y) * boxSize.y/2.0));
    } else { // crosses -y
        nearestPoint.y = from.y - boxSize.y/2.0;
        nearestPoint.x = from.x - ((to.y === from.y) ? 0 :
        ((to.x - from.x)/(to.y - from.y) * boxSize.y/2.0));
    }
    return nearestPoint;
}
Run Code Online (Sandbox Code Playgroud)

Theta是与第一个方框角的夹角,而phi是实际的线夹角。

要获取框在客户坐标中的位置,您需要使用elem.getBoundingClientRect(),除其他外,它会产生左,上,宽,高,我用它来找到框的中心:

function getCentreOfElement(el) {
    var bounds = el.getBoundingClientRect();
    return {x:bounds.left + bounds.width/2.0,
            y:bounds.top + bounds.height/2.0};
}
Run Code Online (Sandbox Code Playgroud)

将所有内容放在一起,您可以从一个元素到另一个元素画一条线。