2015-06-05 35 views
-2

我正在試圖使粒子沿着水平「8」形狀的路徑移動,這就像「∞」一樣。使粒子以水平8形狀行進

我只能通過遵循this tutorial使粒子在一個圓圈中行進。

但我不知道如何使它在水平8形狀的旅行。

這裏是我的代碼:

var cvs = document.createElement('canvas'), 
 
    context = cvs.getContext("2d"); 
 
document.body.appendChild(cvs); 
 

 
var numDots = 200, 
 
    n = numDots, 
 
    currDot, 
 
    maxRad = 250, 
 
    minRad = 150, 
 
    radDiff = maxRad - minRad, 
 
    dots = [], 
 
    PI = Math.PI, 
 
    centerPt = { 
 
    x: 0, 
 
    y: 0 
 
    }; 
 

 
resizeHandler(); 
 
window.onresize = resizeHandler; 
 

 
while (n--) { 
 
    currDot = {}; 
 
    currDot.radius = minRad + Math.random() * radDiff; 
 
    currDot.ang = (1 - Math.random() * 2) * PI; 
 
    currDot.speed = (1 - Math.random() * 2) * 0.025; 
 
    currDot.intensity = Math.round(Math.random() * 255); 
 
    currDot.fillColor = "rgb(" + currDot.intensity + "," + currDot.intensity + "," + currDot.intensity + ")"; 
 
    dots.push(currDot); 
 
} 
 

 
function drawPoints() { 
 
    n = numDots; 
 
    var _centerPt = centerPt, 
 
    _context = context, 
 
    dX = 0, 
 
    dY = 0; 
 

 
    _context.clearRect(0, 0, cvs.width, cvs.height); 
 

 
    //draw dots 
 
    while (n--) { 
 
    currDot = dots[n]; 
 
    dX = _centerPt.x + Math.sin(currDot.ang) * currDot.radius; 
 
    dY = _centerPt.y + Math.cos(currDot.ang) * currDot.radius; 
 

 
    currDot.ang += currDot.speed; 
 

 
    //console.log(currDot); 
 
    _context.fillStyle = currDot.fillColor; 
 
    _context.fillRect(dX, dY, 10, 10); 
 

 
    } //draw dot 
 
    window.requestAnimationFrame(drawPoints); 
 
} 
 

 
function resizeHandler() { 
 
    var box = cvs.getBoundingClientRect(); 
 
    var w = box.width; 
 
    var h = box.height; 
 
    cvs.width = w; 
 
    cvs.height = h; 
 
    centerPt.x = Math.round(w/4); 
 
    centerPt.y = Math.round(h/2); 
 
} 
 

 
drawPoints();
body, 
 
html { 
 
    margin: 0; 
 
    padding: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    overflow: hidden; 
 
} 
 
canvas { 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
    background: #000000; 
 
}
<canvas></canvas>

+0

D3是相當冷靜 - http://bl.ocks.org/mbostock/1705868 – thatOneGuy

回答

0

可以使用不同的半徑和不同速度使用正弦和餘弦:

對於 「永恆」 符號(∞)的關係餘弦和正弦之間的比例是1:2。半徑也比橢圓更像圓圈,但不一定是相同的關係。 1:1.5往往效果更好:

x = centerX + radiusX * Math.cos(angle) 
y = centerY + radiusY * Math.sin(angle * 2); 

var cx = 200, cy = 75,       // center of path 
 
    rx = 150, ry = 50,       // radius for x and y 
 
    angleStep = 0.05,       // "speed" 
 
    angle = 0,         // current angle 
 
    o = document.querySelector("div").style; // object to move (demo) 
 

 
(function loop() { 
 

 
    var x = cx + rx * Math.cos(angle), 
 
     y = cy + ry * Math.sin(angle * 2); 
 
    
 
    o.transform = o.webkitTransform = "translate(" + x + "px," + y + "px)"; 
 
    
 
    angle += angleStep; 
 
    angle = angle % (2*Math.PI); 
 
    
 
    requestAnimationFrame(loop) 
 
})();
div {border-radius:50%;background:#78f;width:20px;height:20px}
<div></div>