javascript 在 RaphaelJS 中绘制连接线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3582344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:36:59  来源:igfitidea点击:

Draw a Connection Line in RaphaelJS

javascriptsvglineraphael

提问by Louis

How would I go about drawing a connection line in Raphael where the mousedown initiates the starting point of the line, the mousemove moves the end point without moving the start point and the mouseup leaves it as it is?

我将如何在 Raphael 中绘制一条连接线,其中 mousedown 启动线的起点,mousemove 移动终点而不移动起点,而 mouseup 保持原样?

回答by Davy Meers

Have a look at the source of http://www.warfuric.com/taitems/RaphaelJS/arrow_test.htm.

查看http://www.warfuric.com/taitems/RaphaelJS/arrow_test.htm的来源。

This might get you started.

这可能会让你开始。

EDIT

编辑

I made a quick example that will give you a head start (still need some work though: validation of parameters, adding comments, etcetera).

我做了一个简单的例子,它会给你一个良好的开端(虽然仍然需要一些工作:参数验证、添加注释等)。

Note: you still have to adapt the path to raphael.js

注意:你仍然需要调整 raphael.js 的路径

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="edit-Type" edit="text/html; charset=utf-8">


<!-- Update the path to raphael js -->
<script type="text/javascript"src="path/to/raphael1.4.js"></script>
<script type='text/javascript'
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<style type='text/css'>
svg {
    border: solid 1px #000;
}
</style>

</head>
<body>
<div id="raphaelContainer"></div>
<script type='text/javascript'> 
    //<![CDATA[ 

function Line(startX, startY, endX, endY, raphael) {
    var start = {
        x: startX,
        y: startY
    };
    var end = {
        x: endX,
        y: endY
    };
    var getPath = function() {
        return "M" + start.x + " " + start.y + " L" + end.x + " " + end.y;
    };
    var redraw = function() {
        node.attr("path", getPath());
    }

    var node = raphael.path(getPath());
    return {
        updateStart: function(x, y) {
            start.x = x;
            start.y = y;
            redraw();
            return this;
        },
        updateEnd: function(x, y) {
            end.x = x;
            end.y = y;
            redraw();
            return this;
        }
    };
};



$(document).ready(function() {
    var paper = Raphael("raphaelContainer",0, 0, 300, 400);
    $("#raphaelContainer").mousedown(

    function(e) {
        x = e.offsetX;
        y = e.offsetY;
        line = Line(x, y, x, y, paper);
        $("#raphaelContainer").bind('mousemove', function(e) {
            x = e.offsetX;
            y = e.offsetY;
            line.updateEnd(x, y);
        });
    });

    $("#raphaelContainer").mouseup(

    function(e) {
        $("#raphaelContainer").unbind('mousemove');
    });
});
    //]]> 
    </script>
</body>
</html>

See example: http://jsfiddle.net/rRtAq/9358/

参见示例:http: //jsfiddle.net/rRtAq/9358/

回答by Mathieu Rodic

You can add your own linemethod to the Paper class...

您可以将自己的line方法添加到 Paper 类...

Raphael.fn.line = function(startX, startY, endX, endY){
    return this.path('M' + startX + ' ' + startY + ' L' + endX + ' ' + endY);
};

...which you can use later, like any other known method from the Paper class (circle, etc.):

...您可以稍后使用,就像 Paper 类(圆圈等)中的任何其他已知方法一样:

var paper = Raphael('myPaper', 0, 0, 300, 400);
paper.line(50, 50, 250, 350);
paper.line(250, 50, 50, 150).attr({stroke:'red'});

(see http://jsfiddle.net/f4hSM/)

(见http://jsfiddle.net/f4hSM/