addInteractionSupport = (context, data, scales) => {
const { xScale, yScale } = scales;
const delaunay = d3.Delaunay.from(
data,
d => xScale(d.gdpPercap),
d => yScale(d.lifeExp)
);
const voronoi = delaunay.voronoi([0, 0, W, H]);
if (voronoiDrawOption == "true") {
context.globalAlpha = 0.5;
context.beginPath();
context.strokeStyle = 'rgba(0,0,0,0.3)';
voronoi.render(context);
context.stroke();
context.beginPath();
context.fillStyle = 'black';
delaunay.renderPoints(context, 2);
context.fill();
}
d3.select(context.canvas)
.on("touchmove mousemove", handleMove)
.on("touchend mouseleave", hideTooltip);
context.restore();
function handleMove(event) {
event.preventDefault();
const mousePoint = d3.pointer(event, this);
mutable feedback = [event.pageX, event.pageY, mousePoint[0], mousePoint[1]];
if (
mousePoint[0] > margin.left &&
mousePoint[0] < width - margin.right &&
mousePoint[1] > margin.top &&
mousePoint[1] < height - margin.bottom
) {
const x = mousePoint[0] - margin.left,
y = mousePoint[1] - margin.top;
const index = delaunay.find(x, y);
const country = data[index].country;
const continent = data[index].continent;
showTooltip(event, country, continent);
} else {
hideTooltip();
}
}
}