import {GPU} from “gpu.js”; export interface CanvasSize { width: number; height: number;…
- User
import {GPU} from “gpu.js”; export interface CanvasSize { width: number; height: number; } const OrderFeed = ({workerRef, symbol, settings}: OrderFeedProps) => { const cupParams = useSelector((state: AppState) => state.cupSlice); const [dpiScale, setDpiScale] = useState(Math.ceil(window.devicePixelRatio)); const [canvasSize, setCanvasSize] = useState({height: 0, width: 0}); const containerRef = useRef<HTMLDivElement|null>(null); const canvasRef = useRef<HTMLCanvasElement|null>(null);
const draw = (trades: TradeItem[], camera: number) => { if (null === canvasRef || !Array.isArray(trades)) return;
const context = canvasRef.current?.getContext(“2d”);
if (context) {
orderFeedDrawer.clear(context, canvasSize); orderFeedDrawer.drawLine( context, canvasSize, trades, camera, cupParams.aggregatedPriceStep, cupParams.cellHeight, cupParams.quantityDivider, !settings.minQuantity ? 0 : parseFloat(settings.minQuantity), dpiScale, theme, ); } };
return <Box ref={containerRef} className={${styles.canvasWrapper} scroll-block}> ; };
export default OrderFeed;
const clamp = (value: number, min: number, max: number): number => { return Math.min(Math.max(value, min), max); };
const orderFeedDrawer = { clear: (ctx: CanvasRenderingContext2D, size: CanvasSize) => { ctx.clearRect(0, 0, size.width, size.height); }, drawLine: ( ctx: CanvasRenderingContext2D, canvasSize: CanvasSize, trades: TradeItem[], camera: number, aggregatedPriceStep: number, cupCellHeight: number, quantityDivider: number, minQuantity: number, dpiScale: number, theme: Theme, ) => { let startX = canvasSize.width - 10 * dpiScale; const fullHeightRowsCount = parseInt((canvasSize.height / cupCellHeight).toFixed(0)); const halfCupCellHeight = cupCellHeight / 2; const startY = (fullHeightRowsCount % 2 ? fullHeightRowsCount - 1 : fullHeightRowsCount) / 2 * cupCellHeight;
ctx.beginPath(); ctx.strokeStyle = orderFeedOptions(theme).line.color; ctx.lineWidth = 2 * dpiScale; trades.forEach(trade => { const yModifier = (trade.price - (trade.price - (trade?.secondPrice || trade.price)) / 2 - camera) / aggregatedPriceStep * cupCellHeight + halfCupCellHeight; const width = 1.75 * clamp((trade.quantity / quantityDivider < minQuantity ? 0 : trade.radius), 5, 50) * dpiScale; ctx.lineTo(startX - width / 2, startY - yModifier); startX = startX - width; }); ctx.stroke();},
Помимо CanvasRenderingContext2D есть еще WebGLRenderingContext. Есть библиотека https://github.com/gpujs/gpu.js/ через которую можно инициализировать canvas с WebGL контекстом. Перепиши синтаксис на библиотеку “gpu.js”: “^2.16.0”,
- Assistant
- User
какие есть библиотеки по WebGLRenderingContext лучше чем gpu.js
- Assistant
GPU.js is a popular library for general-purpose computing on GPUs using JavaScript. However, if you're looking for alternative libraries for WebGL rendering, here are a few options:
-
Three.js: Three.js is a widely-used JavaScript library for creating and rendering 3D scenes in the browser. It provides a high-level API for working with WebGL and abstracts away many of the complexities of low-level WebGL programming.
-
Babylon.js: Babylon.js is another powerful JavaScript framework for building 3D games and experiences in the browser. It offers a comprehensive set of features and tools for WebGL rendering, including physics simulation, animations, and advanced rendering techniques.
-
regl: regl is a functional WebGL library that focuses on performance and efficiency. It provides a simple, declarative API for creating and managing WebGL resources, and it's designed to work well with functional programming paradigms.
-
PIXI.js: PIXI.js is a 2D rendering library that uses WebGL for hardware-accelerated rendering. It's optimized for creating interactive 2D graphics and animations, and it offers a rich set of features for working with sprites, textures, and filters.
-