-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>
D3是相當冷靜 - http://bl.ocks.org/mbostock/1705868 – thatOneGuy