Skip to content

Instantly share code, notes, and snippets.

@lxchurbakov
Last active July 31, 2024 06:21
Show Gist options
  • Save lxchurbakov/50e9f8e25215cc0a26dd7fa2df146327 to your computer and use it in GitHub Desktop.
Save lxchurbakov/50e9f8e25215cc0a26dd7fa2df146327 to your computer and use it in GitHub Desktop.
HTML5 Canvas
const canvas = canvasRef.current;
const pixelRatio = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.style.width = rect.width + 'px';
canvas.style.height = rect.height + 'px';
canvas.width = rect.width * pixelRatio;
canvas.height = rect.height * pixelRatio;
const context = canvas.getContext('2d');
context.scale(pixelRatio, pixelRatio);
const render = () => {
context.clearRect(0, 0, rect.width, rect.height);
//
requestAnimationFrame(render);
};
requestAnimationFrame(render);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment