RapalJS单击它时绘制元素的边界框

时间:2013-03-26 11:02:34

标签: jquery raphael

所以我尝试对元素/集使用getBBox()方法,并使用x,y,width和height属性来定义rect。但是元素附加了一个拖动事件,每次触发拖动事件时,它都会绘制一个新的边界框。

我尝试在我的拖动函数之后使用element.remove来摆脱元素,但我似乎得到一个元素未定义错误。

foo.click(function(){
    console.log(foo.getBBox());
    var herpaderp = drawBBox(foo.getBBox());
    console.log(herpaderp);
    dragsymbolsoncanvas(foo,herpaderp);
});
function dragsymbolsoncavas(foo,herpaderp){
    function dragger(){
        this.dx = this.dy = 0;
    };
    function mover(s){
        return function(dx, dy){
            if(this.data("candrag")=="true"){
                (s||this).translate(dx-this.dx,dy-this.dy);
                this.dx = dx;
                this.dy = dy;
            }
        }
    };
    foo.drag(mover(foo), dragger);
    herpaderp.remove();
};

1 个答案:

答案 0 :(得分:4)

关于JSFiddle的示例: http://jsfiddle.net/hu8dd/

var Paper = Raphael("test",500,500);
var foo = Paper.circle(100,100,50).attr({fill: "#aa5555"});

var onstart = function(){
            if(this.rect == undefined){
               var coords = this.getBBox();
               this.rect = Paper.rect(coords.x, coords.y, coords.width, coords.height)
                                .attr({fill: "none", stroke: "#aaaaaa", "stroke-width": 1});
            }else{
               this.rect.show();
            }

        };
        var onmove = function(dx,dy){
                    this.transform(this.trans+'T'+(dx)+','+(dy));
                    this.rect.transform(this.rtrans+'T'+(dx)+','+(dy));

        };
        var onend = function(){
            this.rect.hide();
            this.trans = this.transform();
            this.rtrans = this.rect.transform();
        }

        foo.drag(onmove, onstart, onend);