Skip to main content
100%

Canvas Animation Using D3.js II

✓ Published0🌍 Public
11Cr18Ni9
Last edited Nov 13, 2019
Created on Nov 6, 2019

This example demonstrates an interactive canvas visualization using D3.js, where moving the mouse highlights circles that are generated and animated on a canvas. It uses an offscreen canvas with the same dimensions as the main one, rendered ahead of time, so that the code can detect hovered circles via `ctx.getImageData(mouseX, mouseY, 1, 1)`. The interface includes a dat.GUI control to adjust the circle count, which regenerates the data and redraws the canvas, alongside a stats.js monitor for rendering frequency. Clicking a button reveals the offscreen canvas as an image via `toDataURL()`.

AI-generated description

Inspired by:

This Demo is based on Previous One and add interactive feature on canvas.

  • Moving mouse on canvas hovered circle will be highlighed. I create an offscreen canvas that has the same dimmension as the presentation canvas. Offscreen canvas will be rendered ahead of the presentation one so that we can detect which circle the mouse hovered by ctx.getImageData(mouseX, mouseY, 1, 1).

  • Click on canvasImg will reveal the current offscreen canvas in an <img> node.

  • Once count is changed that will generate new circles and update canvas. The performance monitor will show the rendering frequency.

mit Licensed

Similar vizzes