如何使用 JQuery 在两个元素之间画一条线并刷新该线?

发布于 2024-10-12 16:10:21 字数 146 浏览 1 评论 0原文

我正在使用 JQuery-UI Draggables 和 Droppables 将元素克隆到 div 上。 使用 JQuery 在页面上的元素之间绘制线条的最佳方法是什么?

刷新页面上的行的最佳方法是什么?我将在页面上有多行,并且只想更新特定行而不是刷新每一行。

I am using JQuery-UI draggables and droppables to clone elements onto a div.
What is the best way to draw a line between elements on a page using JQuery.

What is the best way to refresh lines on the page? I will have multiple lines on the page and only want to update a particular line rather than refresh every line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-10-19 16:10:21

我现在有这个工作。

根据我的经验,不要使用 jquery.svg,这可能是在不学习另一个插件的情况下解决它的压力,但我发现它比它的价值更麻烦,并导致兼容性问题。

可以使用 HTML5 canvas 和 excanvas 兼容性脚本一个很棒的 html5 演练,但是由于 HTML5 画布的工作原理,如果需要绘制线条,则需要销毁并重新绘制画布上的所有线条被删除或者其位置需要更新。

我在其之间绘制线条的元素位于表示关系的父元素内。子元素代表开始和结束,因此我可以通过使用例如 $('.relationshipClass') 获取父元素的集合并询问集合元素的子元素以获取父元素的集合来重新绘制所有这些关系线上的点。
要使用此代码,您必须想出一种方法来轻松获取可重绘的线点。

标记:
漂亮又简单,一个 html

来保存在之间绘制的任何元素(最有可能是 JQuery UI 可拖动元素),以及一个 将在相同位置

 <div id="divCanvasId" class="divCanvasClass"></div>
 <canvas id="html5CanvasId"></canvas>

CSS:
不要使用 CSS 控制 元素的宽度,请参阅 关于画布拉伸的问题。将 放置在与

相同的位置并位于其后面(使用 z 索引),这将显示 < 后面的线条code>

并防止 阻止与

子级的任何拖放交互。
canvas
{
    background-color: #FFFFFF;
    position: absolute;
    z-index: -10;
    /* control height and width in code to prevent stretching */
}

Javascript方法:
创建实用方法:示例代码位于 JQuery 插件 内,其中包含:

  • 用于重置的辅助函数画布(更改宽度将删除所有
  • 在两个元素之间绘制线条的辅助函数
  • 在所有需要的元素之间绘制线条的函数

添加新线条或调整线条位置时,会破坏现有线条并画出所有的线条。
您可以将下面的代码放入常规函数中或作为插件保留。

Javascript代码:
注意匿名后未进行测试。

$(document).ready(function () {
    $.fn.yourExt = {

        _readjustHTML5CanvasHeight: function () {
            //clear the canvas by readjusting the width/height
            var html5Canvas = $('#html5CanvasId');
            var canvasDiv = $('#divCanvasId');

            if (html5Canvas.length > 0) {
                html5Canvas[0].width = canvasDiv.width();
                html5Canvas[0].height = canvasDiv.height();
            }
        }
        ,
        //uses HTML5 <canvas> to draw line representing relationship
        //IE support with excanvas.js
        _drawLineBetweenElements: function (sourceElement, targetElement) {

            //draw from/to the centre, not the top left
            //don't use .position()
            //that will be relative to the parent div and not the page
            var sourceX = sourceElement.offset().left + sourceElement.width() / 2;
            var sourceY = sourceElement.offset().top + sourceElement.height() / 2;

            var targetX = targetElement.offset().left + sourceElement.width() / 2;
            var targetY = targetElement.offset().top + sourceElement.height() / 2;

            var canvas = $('#html5CanvasId');

            //you need to draw relative to the canvas not the page
            var canvasOffsetX = canvas.offset().left;
            var canvasOffsetY = canvas.offset().top;

            var context = canvas[0].getContext('2d');

            //draw line
            context.beginPath();
            context.moveTo(sourceX - canvasOffsetX, sourceY - canvasOffsetY);
            context.lineTo(targetX - canvasOffsetX, targetY - canvasOffsetY);
            context.closePath();
            //ink line
            context.lineWidth = 2;
            context.strokeStyle = "#000"; //black
            context.stroke();
        }
        ,

        drawLines: function () {
        //reset the canvas
        $().yourExt._readjustHTML5CanvasHeight();

        var elementsToDrawLinesBetween;
        //you must create an object that holds the start and end of the line
        //and populate a collection of them to iterate through
        elementsToDrawLinesBetween.each(function (i, startEndPair) {
            //dot notation used, you will probably have a different method
            //to access these elements
            var start = startEndPair.start;
            var end = startEndPair.end;

            $().yourExt._drawLineBetweenElements(start, end);
        });
    }

您现在可以从事件处理程序(例如 JQuery UI 的拖动事件)调用这些函数画线。

I now have this working.

In my experience, don't use jquery.svg, it may have been the pressure to solve it without learning my way around another plugin, but I found it more hassle than it was worth and caused compatibilty issues.

It's possible to solve using the HTML5 canvas and excanvas compatibility script, and a great html5 walkthrough, BUT because of how the HTML5 canvas works, it requires that all the linse on the canvas are destroyed and redrawn if a line needs to be removed or its position needs to be updated.

The elements that I draw lines between are inside a parent element that represents a relationship. The child elements represent a start and end, so I can redraw all of these relationships by getting a collection of the parents using e.g. $('.relationshipClass') and interrogating the set's elements' children to get the points of the line.
To use this code you will have to come up with an approach to easily get the line points available to redraw.

Markup:
Nice and simple, a html <div> to hold any elements that are drawn between (most probably JQuery UI draggables), and a <canvas> that will be in the same position

 <div id="divCanvasId" class="divCanvasClass"></div>
 <canvas id="html5CanvasId"></canvas>

CSS:
Don't control the <canvas> element's width with CSS, see Question on Canvas streching. Position the <canvas> in the same position as the <div> and behind it (with the z-index), this will show the lines up behind the <div> and prevent the <canvas> from blocking any drag and drop interactions with the <div>'s children.

canvas
{
    background-color: #FFFFFF;
    position: absolute;
    z-index: -10;
    /* control height and width in code to prevent stretching */
}

Javascript approach:
Create utility methods: the example code is inside a JQuery plugin that contains:

  • A helper function to reset the canvas (changing the width will delete all of
  • A helper function to draw lines between two elements
  • A function that draws lines between all the elements that require one

When you add a new line or adjust the position of a line, you destroy the existing lines and draw all of the lines.
You can put the code below into conventional functions or leave as a plugin.

Javascript code:
N.B. not tested after anonymisation.

$(document).ready(function () {
    $.fn.yourExt = {

        _readjustHTML5CanvasHeight: function () {
            //clear the canvas by readjusting the width/height
            var html5Canvas = $('#html5CanvasId');
            var canvasDiv = $('#divCanvasId');

            if (html5Canvas.length > 0) {
                html5Canvas[0].width = canvasDiv.width();
                html5Canvas[0].height = canvasDiv.height();
            }
        }
        ,
        //uses HTML5 <canvas> to draw line representing relationship
        //IE support with excanvas.js
        _drawLineBetweenElements: function (sourceElement, targetElement) {

            //draw from/to the centre, not the top left
            //don't use .position()
            //that will be relative to the parent div and not the page
            var sourceX = sourceElement.offset().left + sourceElement.width() / 2;
            var sourceY = sourceElement.offset().top + sourceElement.height() / 2;

            var targetX = targetElement.offset().left + sourceElement.width() / 2;
            var targetY = targetElement.offset().top + sourceElement.height() / 2;

            var canvas = $('#html5CanvasId');

            //you need to draw relative to the canvas not the page
            var canvasOffsetX = canvas.offset().left;
            var canvasOffsetY = canvas.offset().top;

            var context = canvas[0].getContext('2d');

            //draw line
            context.beginPath();
            context.moveTo(sourceX - canvasOffsetX, sourceY - canvasOffsetY);
            context.lineTo(targetX - canvasOffsetX, targetY - canvasOffsetY);
            context.closePath();
            //ink line
            context.lineWidth = 2;
            context.strokeStyle = "#000"; //black
            context.stroke();
        }
        ,

        drawLines: function () {
        //reset the canvas
        $().yourExt._readjustHTML5CanvasHeight();

        var elementsToDrawLinesBetween;
        //you must create an object that holds the start and end of the line
        //and populate a collection of them to iterate through
        elementsToDrawLinesBetween.each(function (i, startEndPair) {
            //dot notation used, you will probably have a different method
            //to access these elements
            var start = startEndPair.start;
            var end = startEndPair.end;

            $().yourExt._drawLineBetweenElements(start, end);
        });
    }

You can now call these functions from event handlers (e.g. JQuery UI's drag event) to draw lines.

二智少女 2024-10-19 16:10:21

制作一个高度为 1px 的 hr 或 div 设置背景,并在需要时使用 jquery 为其宽度设置动画。

Make a hr or div with 1px height set background, and animate its width using jquery when needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文