var colors = [
\t '#FFAA5C',
\t '#DA727E',
\t '#AC6C82',
\t '#685C79',
\t '#455C7B'
]
var line = d3.svg.line()
\t .x((d) => d.x)
\t .y((d) => d.y)
\t .interpolate('linear');
var svg = d3.select('body').append('svg');
var svgW = d3.select('svg').node().clientWidth;
var svgH = d3.select('svg').node().clientHeight;
var w = svgW/4;
var h = svgH/4;
var data = [ \t {x: -w/2, y: -h/4}, {x: 0, y: -h/2}, {x: w/2, y: -h/4},
\t \t \t \t \t \t \t \t {x: w/Math.PI, y:h/2.5}, {x: -w/4, y: h/2.5} ];
var getPath = (d, i) => {
\t var path = [];
\t
\t var startPoint = { x: 0, y: 0 };
\t // point 1
\t path.push(startPoint);
\t // point 2
\t path.push(d);
\t
\t // point 3
\t path.push(data[i + 1] || data[0]);
\t
\t // point 4
\t path.push(startPoint);
\t
\t return path;
}
var g = svg.append('g')
\t .attr('transform', 'translate(' + svgW/2 + ',' + svgH/2 + ')');
g.selectAll('path')
\t .data(data)
.enter().append('path')
\t .attr({
\t \t fill: (d, i) => colors[i]
\t });
transition();
function transition() {
\t
\t data.map((d) => {
\t \t d.x = d3.random.normal(d['x'], 5)();
\t \t d.y = d3.random.normal(d['y'], 5)();
\t \t return d;
\t });
\t
\t g.selectAll('path')
\t \t .data(data)
\t \t .transition()
\t \t .ease("linear")
\t \t .duration(50)
\t \t .attr('d', (d, i) => line(getPath(d, i)))
\t \t .call(endall, function(){window.requestAnimationFrame(transition)});
\t
}
function endall(transition, callback) {
if (transition.size() === 0) { callback() }
var n = 0;
transition
.each(function() { ++n; })
.each("end", function() { if (!--n) callback.apply(this, arguments); });
}
body {
\t background-color: #181818;
\t display: flex;
\t justify-content: center;
\t align-items: center;
\t height: 100vh;
\t width: 100%;
}
svg {
\t overflow: visible;
\t width: 100%;
\t height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
有趣的實現。另外,我完全認爲線性是默認的緩動功能。感謝您深思熟慮的迴應。 – Himmel
乾杯@Himmel我的靈感來自於演示的設計。太好了! –