var width = 960, height = 500, centered; var svg = d3.select('svg') .attr('width', width) .attr('height', height); // Add background svg.append('rect') .attr('class', 'background') .attr('width', width) .attr('height', height); var g = svg.append('g'); var effectLayer = g.append('g') .classed('effect-layer', true); var dummyText = g.append('text') .classed('dummy-text', true) .attr('x', 10) .attr('y', 30) .style('opacity', 0); var bigText = g.append('text') .classed('big-text', true) .attr('x', 20) .attr('y', 45); var BASE_FONT = "'Helvetica Neue', Helvetica, Arial, sans-serif"; function textArt(text){ // Use random font var fontFamily = BASE_FONT; bigText .style('font-family', fontFamily) .text(text); // Use dummy text to compute actual width of the text // getBBox() will return bounding box dummyText .style('font-family', fontFamily) .text(text); var bbox = dummyText.node().getBBox(); var textWidth = bbox.width; var textHeight = bbox.height; var xGap = 3; var yGap = 1; // Generate the positions of the text in the background var xPtr = 0; var yPtr = 0; var positions = []; var rowCount = 0; while(yPtr < height){ while(xPtr < width){ var point = { text: text, index: positions.length, x: xPtr, y: yPtr }; var dx = point.x - width/2 + textWidth/2; var dy = point.y - height/2; point.distance = dx*dx + dy*dy; positions.push(point); xPtr += textWidth + xGap; } rowCount++; xPtr = rowCount%2===0 ? 0 : -textWidth/2; xPtr += Math.random() * 10; yPtr += textHeight + yGap; } var selection = effectLayer.selectAll('text') .data(positions, function(d){return d.text+'/'+d.index;}); // Clear old ones selection.exit().transition() .style('opacity', 0) .remove(); // Create text but set opacity to 0 selection.enter().append('text') .text(function(d){return d.text;}) .attr('x', function(d){return d.x;}) .attr('y', function(d){return d.y;}) .style('font-family', fontFamily) .style('fill', '#777') .style('opacity', 0); selection .style('font-family', fontFamily) .attr('x', function(d){return d.x;}) .attr('y', function(d){return d.y;}); // Create transtion to increase opacity from 0 to 0.1-0.5 // Add delay based on distance from the center of the and a bit more randomness. selection.transition() .delay(function(d){ return d.distance * 0.01 + Math.random()*1000; }) .style('opacity', function(d){ return 0.1 + Math.random()*0.4; }); }