import * as React from 'react'; import './index.scss'; import Squares from './squares' // import SnakeArrow from 'react-snake-arrow' interface Props{ data:Array, style:number,//0:银灰,1:灰,2:红 cols:number, rows:number, grids:wh } interface wh{ width:number, height:number } interface xy{ x:number, y:number, col:number, row:number, } interface sted{ start:xy, end:xy, type:number,//0:bu旋转,1:y对称旋转,2:x对称旋转。 fill?:string } const color = ['#d2dadf','#939faa','#ffcaca'] // const rotate = ['','rotateX(180deg)','rotateY(180deg)','rotateX(180deg)'] export default class ArrowSvg extends React.Component{ squares:any refs:any str:string = '' state={str:''} constructor(p:Props){ super(p); window.addEventListener('resize',this.renderArrow,false) // window.onresize = this.renderArrow; } componentWillUnmount(){ window.removeEventListener('resize',this.renderArrow,false) } renderArrow = () => { this.forceUpdate(); } componentDidUpdate(){ const {data,style,rows,cols} = this.props; let tempW = this.refs.svgContainer.offsetWidth; let tempH = this.refs.svgContainer.offsetHeight; this.squares._fill = color[style]; this.squares._width = tempW; this.squares._height = tempH; this.squares._size = [rows,cols]; // this.str=`${tempW}${tempH}` this.squares._innerWidth = tempW / rows * 0.7; this.squares._innerHeight = tempH / cols * 0.7; this.squares.drawArrows(data); //console.log(this.state.str,`${tempW}${tempH}`) if(this.state.str!=`${tempW}${tempH}`){ this.setState({str:`${tempW}${tempH}`}) } } componentDidMount(){ const {style,rows,cols,data} = this.props; let tempW = this.refs.svgContainer.offsetWidth; let tempH = this.refs.svgContainer.offsetHeight; this.squares = new Squares({ container: this.refs.svgContainer, showAnchor: false, // 是否显示锚点 defalut true anchors: { // 锚点样式,标准的svg attr即可 start: { fill: 'red', r: 3 }, end: { fill: 'blue', r: 3 } }, fill: color[style], // 箭头填充色(会自动做成渐变) animate: false, // 是否开启箭头绘制动效 (todo...) width: tempW, // m * n 总宽度- height: tempH, // m * n 总高度- innerWidth: tempW / rows * 0.7, // 小方块内容区域大小,用于锚点的定位- innerHeight: tempH / cols * 0.7, // 小方块内容区域大小,用于锚点的定位- size: [rows,cols], // m * n m行,n列- arrows:data }) this.squares.drawArrows(data) } render(){ return
} }