拉斐爾對角變換對象和無限setIntervals (Raphael transform object diagonally and infinite setIntervals)


問題描述

拉斐爾對角變換對象和無限setIntervals (Raphael transform object diagonally and infinite setIntervals)

I'm working on a small animation where the user drags a circle and the circle returns back to the starting point. I figured out a way to have the circle return to the starting point. The only problem is that it will hit one of the sides of the frame before returning. Is it possible for it to go straight back (follow the path of a line drawn between the shape and starting point). 

The other problem is that my setInterval doesn't want to stop. If you try pulling it a second time it would pull it back before you release your mouse. It also seems to speed up after every time. I have tried using a while loop with a timer but the results weren't as good. Is this fixable? 

    var paper = Raphael(0, 0, 320, 200);
    //var path = paper.path("M10 10L40 40").attr({stoke:'#000000'});
    //var pathArray = path.attr("path");
    var circle = paper.circle(50, 50, 20);
    var newX;
    var newY;
    circle.attr("fill", "#f00");
    circle.attr("stroke", "#fff");



     var start = function () {
        this.attr({cx: 50, cy: 50});
        this.cx = this.attr("cx"),
        this.cy = this.attr("cy");
     },
     move = function (dx, dy) {
        var X = this.cx + dx,
        Y = this.cy + dy;
        this.attr({cx: X, cy: Y});
     },

     up = function () {
        setInterval(function () {
    if(circle.attr('cx') > 50){
        circle.attr({cx : (circle.attr('cx') ‑ 1)});
    } else if (circle.attr('cx') < 50){
        circle.attr({cx : (circle.attr('cx') + 1)});
    } 

    if(circle.attr('cy') > 50){
        circle.attr({cy : (circle.attr('cy') ‑ 1)});
    } else if (circle.attr('cy') < 50){
        circle.attr({cy : (circle.attr('cy') + 1)});
    } 
   path.attr({path: pathArray});
},2);
 };

circle.drag(move, start, up);

Here's the Jfiddle: http://jsfiddle.net/Uznp2/

Thanks alot :D


參考解法

方法 1:

I modified the "up" function to the one below

up = function () {
  //starting x, y of circle to go back to
  var interval = 1000;
  var startingPointX = 50;
  var startingPointY = 50;
  var centerX = this.getBBox().x + (this.attr("r")/2);
  var centerY = this.getBBox().y + (this.attr("r")/2);
  var transX = (centerX ‑ startingPointX) * ‑1;
  var transY = (centerY ‑ startingPointY) * ‑1;
  this.animate({transform: "...T"+transX+", "+transY}, interval);
};

and the "start" function as follows:

var start = function () {
  this.cx = this.attr("cx"),
  this.cy = this.attr("cy");
}

Is this the behavior you are looking for? Sorry if I misunderstood the question.

方法 2:

If the circle need to get back to its initial position post drag, we can achieve that via simple animation using transform attribute.

// Assuming that (50,50) is the location the circle prior to drag‑move (as seen in the code provided)
// The animation is set to execute in 1000 milliseconds, using the easing function of 'easeIn'.
up = function () {
   circle.animate({transform: 'T50,50'}, 1000, 'easeIn');
};

Hope this helps.

(by JamesMichael Sanchezenarcee)

參考文件

  1. Raphael transform object diagonally and infinite setIntervals (CC BY‑SA 3.0/4.0)

#raphael #animation #javascript #loops #transform






相關問題

如何沿路徑為 Raphael 對象設置動畫? (How to animate a Raphael object along a path?)

Raphaël.js:如何縮放圓形的填充圖像以適合圓形? (Raphaël.js: How to scale a circle's fill image to fit the circle?)

如何通過“raphael-svg-import”加載和顯示 SVG 數據? (How to load and display SVG data by "raphael-svg-import"?)

gRaphael 餅圖:添加動畫 (gRaphael Pie Chart : Add animation)

如何在服務器端 Java 中使用 JavaScript 圖表庫,如 D3.js 或 Raphaël (How to use a JavaScript chart library like D3.js or Raphaël in server-side Java)

raphael newbie:彈窗背景在跳躍 (raphael newbie: popup background is jumping)

如何製作流暢的圓弧動畫? (How to create smooth circular arcing animation?)

拉斐爾對角變換對象和無限setIntervals (Raphael transform object diagonally and infinite setIntervals)

Raphael JS 服務器端 (Raphael JS server-side)

Raphael.js:在我的情況下如何拖動路徑的一側? (Raphael.js : How to drag one side of the path in my case?)

如何用 raphael js 繪製曲線? (How to draw curves with raphael js?)

如何用拉斐爾垂直縮放? (How to vertically scale with Raphael?)







留言討論