2013-12-10 213 views
0

基本上我想沿着路徑滾動一個對象。我見過幾個線程尋找類似的解決方案,不使用paper.js,但我想知道是否可以使用paper.js。或者有人可以給我一個工作的對象跟蹤svg曲線,因爲我無法得到任何工作。我最終希望有一個div鏈。沿着路徑滾動Paper.js

// vars 
var point1 = [0, 100]; 
var point2 = [120, 100]; 
var point3 = [120, 150]; 

// draw the line 
var path = new Path(); 
path.add(new Point(point1), new Point(point2), new Point(point3)); 
path.strokeColor = "#FFF"; 
path.closed = true; 

// draw the circle 
var circle = new Path.Circle(0,100,4); 
circle.strokeColor = "#FFF"; 

// target to move to 
var target = point2; 

// how many frame does it take to reach a target 
var steps = 200; 

// defined vars for onFrame 
var dX  = 0; 
var dY  = 0; 

// position circle on path 
circle.position.x = target[0]; 
circle.position.y = target[1]; 

function onFrame(event) { 

    //check if cricle reached its target 
    if (Math.round(circle.position.x) == target[0] && Math.round(circle.position.y) == target[1]) { 
     switch(target) { 
      case point1: 
       target = point2; 
       break; 
      case point2: 
       target = point3; 
       break; 
      case point3: 
       target = point1; 
       break; 
     } 

     // calculate the dX and dY 
     dX = (target[0] - circle.position.x)/steps; 
     dY = (target[1] - circle.position.y)/steps; 

    } 

    // do the movement 
//circle.position.x += dX; 
//circle.position.y += dY; 
} 

這裏是的jsfiddle: http://jsfiddle.net/J9xgY/12/

謝謝!

+0

你的問題還不清楚 - 你能解釋一下你的意思嗎?「用滾動條跟蹤路徑比例?」另外,請記住,paper.js不是移動外部'div's的最佳工具,而是在'canvas'元素中顯示圖形。 –

+0

基本上我想沿路徑滾動一個對象。 – user2517605

+0

你想沿三角形路徑移動圓圈嗎? –

回答

0

您可以沿着路徑找到一個點,其中path.getPointAt(offset)其中offset是沿路徑長度測量的。如果您可以計算滑塊沿其軌道的位置,則可以將其乘以path.length以獲得偏移量。

您可以用HTML滑塊或用帆布元素做到這一點,如下所示:

// vars 
var point1 = [0, 100]; 
var point2 = [120, 100]; 
var point3 = [120, 150]; 

// draw the line 
var path = new Path(); 
path.add(new Point(point1), new Point(point2), new Point(point3)); 
path.strokeColor = "#FFF"; 
path.closed = true; 

// draw the circle 
var circle = new Path.Circle(0,100,4); 
circle.strokeColor = "#FFF"; 

// slider 
var sliderLine = new Path(new Point(10,30.5), new Point(210, 30.5)); 
sliderLine.strokeColor = '#FFF'; 
var sliderKnob = new Path.Circle(new Point(10,30.5), 5); 
sliderKnob.fillColor = '#FFF'; 

var sliderHit = false; 

function onMouseDown(event) { 
    if (event.item == sliderKnob) sliderHit = true; 
} 

function onMouseDrag(event) { 
    if (sliderHit === true) { 
     if (event.point.x > 10 && event.point.x < 210) { 
      sliderKnob.position.x = event.point.x; 
     } 
     else if (event.point.x < 11) { 
      sliderKnob.position.x = 10; 
     } 
     else if (event.point.x > 209) { 
      sliderKnob.position.x = 210; 
     } 

     // Get offset and set circle position 
     var percent = (sliderKnob.position.x - 10)/200; 
     circle.position = path.getPointAt(path.length * percent);    
    } 

} 

function onMouseUp(event) { 
    sliderHit = false; 
} 

的jsfiddle:http://jsfiddle.net/J9xgY/13/

單擊並拖動實心圓沿線待着圓三角形。

+0

謝謝!雖然我想用鼠標滾輪來控制它,但我可以從這裏拿走它。 – user2517605

+0

@ user2517605你在製作一個與鼠標滾輪一起運行的老虎機遊戲嗎?我想看一看 –