Posts

Showing posts with the label requestanimationframe

Optimizing Data Visualization Performance with NextJS, D3 and Canvas

Image
Introduction In the previous article, I guided you on how to use D3 and Canvas to Visualize Data. Now we will continue to dive deeper to understand some solutions that help optimize performance, aiming for a State-of-the-art" approach for building Real-time Data Visualization on the Web today. Some techniques I will apply to optimize performance are as follows: Data Buffering (Data Layer Throttling): utilizing requestAnimationFrame to ensure smooth processing and rendering according to the screen refresh rate, such as 60Hz or 120Hz. Utilizing Context Alpha Optimization: getContext('2d', { alpha: false }). When you turn off the alpha channel (transparency), the GPU will not have to calculate color blending with elements behind the canvas, significantly reducing the load on the graphics chip. Offscreen Canvas: Using an implicit Canvas to calculate beforehand before pushing to the main screen. The issue with drawing directly on the Canvas is that if you have to draw too much,...

Optimizing Konva Performance with rAF

Image
Introduction requestAnimationFrame (rAF) is a powerful browser API that helps achieve smoother interface changes by synchronizing rendering with the screen's refresh rate. Using rAF helps optimize performance, minimize lag (jank), and save energy by pausing calculations when the browser tab is inactive. Prerequisites In this article, I will continue to use the example from the previous article to optimize, so I will not provide the complete code but only the updated part; please refer to the previous article for the full content. Detail Update the OptimizedCanvas.tsx file as follows: import Konva from 'konva' import React , {useCallback, useEffect, useMemo, useRef, useState} from 'react' import {Layer, Stage} from 'react-konva' import useImage from 'use-image' import {ShapeItem} from './ShapeItem' import type {ShapeData, ViewRect} from './type' import {generateData, HEIGHT, IMAGE_URL, TOTAL_OBJECTS, WIDTH} from ...