Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {PureComponent} from "react";
  2.  
  3. import {
  4.   View,
  5.   Layer,
  6.   Group,
  7.   Circle,
  8.   Ellipse,
  9.   Rectangle,
  10.   PointText,
  11.   Tool,
  12. } from 'react-paper-bindings'
  13.  
  14. interface Props {
  15. }
  16. interface State {
  17.   imageSize: number,
  18.   visible: boolean,
  19.   mounted: boolean,
  20. }
  21.  
  22.  
  23. const ReactLogo = ({ rotation, x, y }) => {
  24.   return (
  25.     <Group name={'reactLogo'} rotation={rotation}>
  26.       <Ellipse
  27.         center={[x, y]}
  28.         size={[70, 25]}
  29.         strokeWidth={2.5}
  30.         strokeColor={'#61DAFB'}
  31.       />
  32.       <Ellipse
  33.         center={[x, y]}
  34.         rotation={120}
  35.         size={[70, 25]}
  36.         strokeWidth={2.5}
  37.         strokeColor={'#61DAFB'}
  38.       />
  39.       <Ellipse
  40.         center={[x, y]}
  41.         rotation={240}
  42.         size={[70, 25]}
  43.         strokeWidth={2.5}
  44.         strokeColor={'#61DAFB'}
  45.       />
  46.       <Circle
  47.         center={[x, y]}
  48.         fillColor={'#61DAFB'}
  49.         radius={7}
  50.       />
  51.     </Group>
  52.   )
  53. };
  54. const Paper = ({ activeTool, width, height }) => {
  55.   return (
  56.     <View activeTool={activeTool} width={width} height={height}>
  57.       <Layer>
  58.         <Rectangle
  59.           center={[width/2, height/2]}
  60.           fillColor={'#222222'}
  61.           opacity={0.8}
  62.           size={[320, 120]}
  63.         />
  64.         <PointText
  65.           content={'Paper.js'}
  66.           fillColor={'white'}
  67.           fontFamily={'Courier New'}
  68.           fontSize={30}
  69.           fontWeight={'bold'}
  70.           justification={'center'}
  71.           point={[(width/2)+40, (height/2)+10]}
  72.         />
  73.         <ReactLogo
  74.           rotation={rotation}
  75.           x={(width/2)-100}
  76.           y={(height/2)}
  77.         />
  78.       </Layer>
  79.       <Tool
  80.         active={activeTool === 'move'}
  81.         name={'move'}
  82.         onMouseDown={props.moveToolMouseDown}
  83.         onMouseDrag={props.moveToolMouseDrag}
  84.         onMouseUp={props.moveToolMouseUp}
  85.       />
  86.       <Tool
  87.         active={activeTool === 'pen'}
  88.         name={'pen'}
  89.         onMouseDown={props.penToolMouseDown}
  90.         onMouseDrag={props.penToolMouseDrag}
  91.         onMouseUp={props.penToolMouseUp}
  92.       />
  93.       <Tool
  94.         active={activeTool === 'circle'}
  95.         name={'circle'}
  96.         onMouseDown={props.addCircle}
  97.       />
  98.       <Tool
  99.         active={activeTool === 'rectangle'}
  100.         name={'rectangle'}
  101.         onMouseDown={props.addRectangle}
  102.       />
  103.     </View>
  104.   )
  105. };
  106.  
  107. export default class Annotation extends PureComponent<Props, State> {
  108.   private _box: any;
  109.   private _request: any;
  110.   constructor(props: Props) {
  111.     super(props)
  112.     this.state = {
  113.       imageSize: 720,
  114.       visible: true,
  115.       mounted: false,
  116.     }
  117.     this._box = null
  118.     this._request = null
  119.   }
  120.  
  121.   resizeWindow = () => {
  122.     if (!this._request) {
  123.       this._request = requestAnimationFrame(this.resizePaper)
  124.     }
  125.   }
  126.  
  127.   resizePaper = () => {
  128.     this.forceUpdate()
  129.     this._request = null
  130.   }
  131.  
  132.   componentDidMount() {
  133.     this.setState({ mounted: true })
  134.     window.addEventListener('resize', this.resizeWindow)
  135.   }
  136.  
  137.   componentWillUnmount() {
  138.     if (this._request) {
  139.       cancelAnimationFrame(this._request)
  140.       this._request = null
  141.     }
  142.     window.removeEventListener('resize', this.resizeWindow)
  143.   }
  144.  
  145.   render() {
  146.       const box = this._box && this._box.getBoundingClientRect()
  147.       let mounted = this.state.mounted
  148.  
  149.       return <div className="Annotation" ref={ref => this._box = ref}>
  150.               {mounted &&
  151.               <Paper activeTool={'pen'} width={box.width} height={box.height} />
  152.               }
  153.       </div>
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement