D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ezzaouia
Full window
Github gist
fisheye
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> .rect-shadow { box-shadow: 0 0 5px #999; } body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> // Feel free to change or delete any of the code you see in this editor! var svg = d3.select("body").append("svg") .attr("width", 960) .attr("height", 500) .append('g') .classed('group', true) .attr('transform', `translate(${20}, ${20})`) let xScale = d3.scaleLinear().domain([0, 300]).range([0, window.innerWidth - 30]).clamp(true) let rects = svg.selectAll('rect') .data(d3.range(300)) rects.exit().remove(); rects.enter() .append("rect") .attr("y", 95) .attr("x", (d, i) => xScale(i)) .attr("width", 1) .style('fill', 'rgb(81, 170, 232)') .attr("height", 117); svg.append('rect') .attr("y", 95) .attr("x", xScale(0)) .attr("width", xScale.range()[1]) .style('opacity', 0) .attr("height", 117) .on('mousemove', () => { let x = d3.event.x - 20; console.log('x', x) d3.selectAll('rect') .attr('x', (d, i) => fisheye(d, x)) }) .on('mouseleave', () => { d3.selectAll('rect') .attr('x', (d, i) => xScale(i)) }) let distortion = 100; function fisheye(_, a) { let x = xScale(_), left = x < a, range = d3.extent(xScale.range()), min = range[0], max = range[1], m = left ? a - min : max - a; if (m === 0) m = max - min; return (left ? -1 : 1) * m * (distortion + 1) / (distortion + (m / Math.abs(x - a))) + a; } </script> </body>
https://d3js.org/d3.v4.min.js