UNPKG

react-pixi-plot

Version:

A React component rendering a zoomable and draggable PIXI.js scene. Intended to render 2d plots

33 lines 1.27 kB
import React from 'react'; import * as PIXI from 'pixi.js'; import { Sprite } from 'react-pixi-fiber'; import { PixiPlotContext } from '../../PlotContext'; class RescalingPIXI extends React.PureComponent { constructor(props) { super(props); this.state = { scale: this.getScale(), }; } getScale() { const { x, y } = this.props.parentScale; const { pixelWidth, pixelHeight, texture } = this.props; const nextXScale = pixelWidth / (x * texture.width); const nextYScale = pixelHeight / (y * texture.height); return new PIXI.Point(nextXScale, nextYScale); } componentDidUpdate(prevProps) { if (prevProps.parentScale !== this.props.parentScale) { this.setState({ scale: this.getScale(), }); } } render() { const { scale } = this.state; return React.createElement(Sprite, Object.assign({}, this.props, { scale: scale })); } } const RescalingSprite = props => React.createElement(PixiPlotContext.Consumer, null, ({ state }) => React.createElement(RescalingPIXI, Object.assign({ parentScale: state.zoomableScale }, props))); export default RescalingSprite; //# sourceMappingURL=RescalingSprite.js.map