Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在单击事件时在SVG上绘制矩形?_Javascript_Svg_Raphael - Fatal编程技术网

Javascript 如何在单击事件时在SVG上绘制矩形?

Javascript 如何在单击事件时在SVG上绘制矩形?,javascript,svg,raphael,Javascript,Svg,Raphael,我在应用程序中使用了raphaeljs。在这里,我需要在拉斐尔纸上的点击点上画一个小矩形。我需要用一条线连接这些矩形。有人能做这个的演示吗。我正在添加示例演示。请更新这个 我的演示: 现在,我需要在单击拉斐尔纸并用线连接它们时绘制samll矩形 - 单击CAMVAS绘制矩形 这应该可以做到 - 单击CAMVAS绘制矩形 这应该可以做到 - 单击CAMVAS绘制矩形 这应该可以做到 - 单击CAMVAS绘制矩形 var paper = Raphael("editor", 635,500),

我在应用程序中使用了
raphaeljs
。在这里,我需要在拉斐尔纸上的点击点上画一个小矩形。我需要用一条线连接这些矩形。有人能做这个的演示吗。我正在添加示例演示。请更新这个

我的演示:

现在,我需要在单击拉斐尔纸并用线连接它们时绘制samll矩形

-

单击CAMVAS绘制矩形
这应该可以做到

-

单击CAMVAS绘制矩形
这应该可以做到

-

单击CAMVAS绘制矩形
这应该可以做到

-

单击CAMVAS绘制矩形
var paper = Raphael("editor", 635,500),
        canvas= document.getElementById('editor').style.backgroundColor='gray';
var paper = Raphael("editor", 635,500),
canvas= document.getElementById('editor').style.backgroundColor='gray';

var offsetx = paper.canvas.offsetLeft;
var offsety = paper.canvas.offsetTop;

var prevRect = null;
var rWidth = 50;

paper.canvas.onmousedown = function(e) {
    var posX = e.pageX-offsetx;
    var posY = e.pageY-offsety;

    var rectX = posX - (rWidth/2)
    var rectY = posY - (rWidth/2)
    var c = paper.rect(rectX, rectY, rWidth, rWidth).attr({fill:"#fff"});

    if(prevRect) {
        var p = "M"+prevRect.x +" " +prevRect.y +"L"+posX+" "+posY
        var line = paper.path(p);
    }

    prevRect = {x: posX, y:posY};
}
<b>Click on CAMVAS to draw rectangle</b>
<div  id="editor"></div>