Raphael.js attr function sets wrong value

前端 未结 3 1086
梦毁少年i
梦毁少年i 2021-01-21 17:40

I\'m implementing a drag and drop system with Raphael.js. For this, i\'m storing the original x and y position on mousedown, and if there is a collision on mouseup, I want to re

3条回答
  •  野性不改
    2021-01-21 17:46

    I'm not sure why this is happening. I tried even capturing the co-ordinates before onstart using onmousedown event, even that didnt work. Also different methods provided by Raphael to get the co-ordinates using getBBox(), accessing x and y directly, didnt help.

    So what I thought is, we should Maintain and Track the coordinates manually. So I have used your original_x and original_y variables which captures the position of the after you create and set with some transform value. Below is the code of the same

    Here is the working fiddle.

    this.raph = R.path(svgPath).attr({
        stroke: "hsb(0, 1, 1)",
        fill: "#fff", 
        opacity: 1.0, 
        cx: 100, 
        cy: 900 
    }).transform("t" + x + "," + y); 
    
    this.raph.original_x = x;
    this.raph.original_y = y;
    
    
    //comment the lines in start method which captures original_x and original_y
    //this.original_x = Raphael.parseTransformString(this.attr("transform"))[0][1];
    //this.original_y = Raphael.parseTransformString(this.attr("transform"))[0][2];
    

    More info regarding tracking the co-ordinates:

    We will have one more coordinate say updated_x and updated_y, which will be updated in the move method. onFinish/onUp method, we can have the check whether we should update the new position or not. Here, it just asks whether new position should be updated or not and based on our input, it sets the final result.

    Check this fiddle:

    this.start = function() {
        if (this.reference.static) return;
        //this.original_x = Raphael.parseTransformString(this.attr("transform"))[0][1];
        //this.original_y = Raphael.parseTransformString(this.attr("transform"))[0][2];
        this.animate({r: 70, opacity: 0.25}, 500, ">");
        this.updated_x = this.original_x;
        this.updated_y = this.original_y;
    };
    
    this.move = function(dx, dy) {
        //var ts = Raphael.parseTransformString(this.attr("transform"));
        this.updated_x = this.original_x + dx;
        this.updated_y = this.original_y + dy;
        //ts[0][1] = this.original_x + dx; 
        //ts[0][2] = this.original_y + dy; 
        this.attr({transform: 't' + this.updated_x + ',' + this.updated_y});
    };
    
    this.up = function() {
    
        if(confirm("Shall I update the new position??")) {
            this.original_x = this.updated_x;
            this.original_y = this.updated_y;
        }
    
        var transformString = "t" + this.original_x + "," + this.original_y;
        this.attr("transform", transformString);    
        this.attr({fill: "#fff"});
        this.animate({r: 50, opacity: 1}, 500, ">");
    };
    

提交回复
热议问题