import {TradeItem} from “…/…/…/store/orderClusterSlice”; import {OrderFeedProps} from…
- User
import {TradeItem} from “…/…/…/store/orderClusterSlice”; import {OrderFeedProps} from “./OrderFeed.props”;
у меня в проекте идет отрисовка через canvasRef.current?.getContext("2d"); CanvasRenderingContext2D, нужно сделать отрисовку более производительнее на WebGL какая из библиотек на react будет самаяя быстрая и произвожительная
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 theme = useTheme(); 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, ); orderFeedDrawer.drawChain( context, canvasSize, trades, camera, cupParams.aggregatedPriceStep, cupParams.cellHeight, cupParams.quantityDivider, cupParams.priceDivider, !settings.minQuantity ? 0 : parseFloat(settings.minQuantity), dpiScale, theme, settings.volumeAsDollars, ); } };
useEffect(() => { if (!workerRef.current) return; draw(event.data.trades, event.data.camera) }, [workerRef.current, canvasRef.current, canvasSize, symbol ]);
return <Box ref={containerRef} className={${styles.canvasWrapper} scroll-block}> ; };
export default OrderFeed;
const orderFeedDrawer = { clear: (ctx: WebGLRenderingContext, 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 startY = (fullHeightRowsCount % 2 ? fullHeightRowsCount - 1 : fullHeightRowsCount) / 2 * cupCellHeight; const halfCupCellHeight = cupCellHeight / 2;
ctx.beginPath(); ctx.strokeStyle = orderFeedOptions(theme).line.color; ctx.lineWidth = (dpiScale) => 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; });
- Assistant
- User
const drawPix = useCallback((g) => { console.log(g);
g.clear(); g.beginFill(0xff3300); g.lineStyle(4, 0xffd900, 1); g.moveTo(50, 50); g.lineTo(250, 50); g.lineTo(100, 100); g.lineTo(50, 50); g.endFill(); g.lineStyle(2, 0x0000ff, 1); g.beginFill(0xff700b, 1); g.drawRect(50, 150, 120, 120); g.lineStyle(2, 0xff00ff, 1); g.beginFill(0xff00bb, 0.25); g.drawRoundedRect(150, 100, 300, 100, 15); g.endFill(); g.lineStyle(0); g.beginFill(0xffff0b, 0.5); g.drawCircle(470, 90, 60); g.endFill();}, []);
return ( <Stage width={300} height={300} options={{ backgroundColor: 0xeef1f5 }}> ); };
JSX element class does not support attributes because it does not have a 'props' property.ts(2607) (property) draw: (g: any) => void
- Assistant
It looks like you are trying to use the
drawattribute in the<Graphics>component provided byreact-pixi-fiber. However, thedrawattribute expects a function that takes aGraphicsobject as an argument and performs the drawing operations.Here's the updated code:
import React from "react"; import { Stage, Graphics } from "@inlet/react-pixi-fiber"; const drawPix = (g) => { g.clear(); g.beginFill(0xff3300); g.lineStyle(4, 0xffd900, 1); g.moveTo(50, 50); g.lineTo(250, 50); g.lineTo(100, 100); g.lineTo(50, 50); g.endFill(); g.lineStyle(2, 0x0000ff, 1); g.beginFill(0xff700b, 1); g.drawRect(50, 150, 120, 120); g.lineStyle(2, 0xff00ff, 1); g.beginFill(0xff00bb, 0.25); g.drawRoundedRect(150, 100, 300, 100, 15); g.endFill(); g.lineStyle(0); g.beginFill(0xffff0b, 0.5); g.drawCircle(470, 90, 60); g.endFill(); }; const MyComponent = () => { return ( <Stage width={300} height={300} options={{ backgroundColor: 0xeef1f5 }}> <Graphics draw={drawPix} /> </Stage> ); }; export default MyComponent;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.