Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. import React, {useEffect, useState} from 'react';
  2. import {fromEvent} from 'rxjs';
  3. import {takeUntil, mergeMap, map} from 'rxjs/operators';
  4. import {disableBodyScroll} from 'body-scroll-lock';
  5.  
  6. import Header from './Header';
  7.  
  8. const canvasStyle = {
  9. border: 'solid 5px black',
  10. backgroundColor: '#eceff1',
  11. };
  12. const divStyle = {
  13. marginTop: '5%',
  14. width: '100%',
  15. };
  16. const btnStyle = {
  17. marginLeft: '1%',
  18. marginTop: '1%',
  19. };
  20. const whiteboardColor = {
  21. position: 'relative',
  22. float: 'left',
  23. width: '35px',
  24. height: '35px',
  25. margin: '1%',
  26. border: 'solid 1px black',
  27. cursor: 'pointer',
  28. borderRadius: '50%',
  29. };
  30. const blackPen = {
  31. backgroundColor: 'black',
  32. };
  33. const redPen = {
  34. backgroundColor: 'red',
  35. };
  36. const bluePen = {
  37. backgroundColor: 'blue',
  38. };
  39. const greenPen = {
  40. backgroundColor: 'green',
  41. };
  42. const yellowPen = {
  43. backgroundColor: 'yellow',
  44. };
  45.  
  46. const getWindowDimensions = () => {
  47. const {innerWidth: width, innerHeight: height} = window;
  48. return {
  49. width,
  50. height,
  51. };
  52. };
  53.  
  54. const useWindowDimensions = () => {
  55. const [windowDimensions, setWindowDimensions] = useState (
  56. getWindowDimensions ()
  57. );
  58.  
  59. useEffect (() => {
  60. const handleResize = () => {
  61. setWindowDimensions (getWindowDimensions ());
  62. };
  63.  
  64. window.addEventListener ('resize', handleResize);
  65. return () => window.removeEventListener ('resize', handleResize);
  66. }, []);
  67.  
  68. return windowDimensions;
  69. };
  70.  
  71. const Whiteboard = () => {
  72. const [x, setX] = useState (null);
  73. const [y, setY] = useState (null);
  74. const {height, width} = useWindowDimensions ();
  75.  
  76. const startPaint = (reset = false) => {
  77. const canvas = document.querySelector ('#my-canvas');
  78. const ctx = canvas.getContext ('2d');
  79. if (width < 800) {
  80. canvas.height = 300;
  81. canvas.width = 300;
  82. } else {
  83. canvas.height = 500;
  84. canvas.width = 900;
  85. }
  86. ctx.lineWidth = 5;
  87. ctx.lineJoin = 'round';
  88. ctx.lineCap = 'round';
  89. ctx.strokeStyle = 'black';
  90. const changeColor = col => {
  91. ctx.strokeStyle = col;
  92. };
  93. const pos = {x: 0, y: 0};
  94.  
  95. //change colors
  96. fromEvent (document.getElementById ('red-pen'), 'click').subscribe (() => {
  97. changeColor ('red');
  98. });
  99. fromEvent (document.getElementById ('blue-pen'), 'click').subscribe (() => {
  100. changeColor ('blue');
  101. });
  102. fromEvent (
  103. document.getElementById ('green-pen'),
  104. 'click'
  105. ).subscribe (() => {
  106. changeColor ('green');
  107. });
  108. fromEvent (
  109. document.getElementById ('yellow-pen'),
  110. 'click'
  111. ).subscribe (() => {
  112. changeColor ('yellow');
  113. });
  114. fromEvent (
  115. document.getElementById ('black-pen'),
  116. 'click'
  117. ).subscribe (() => {
  118. changeColor ('black');
  119. });
  120.  
  121. //mouse movements
  122. const setPosition = e => {
  123. pos.x = e.clientX - canvas.offsetLeft;
  124. pos.y = e.clientY - canvas.offsetTop;
  125. return [pos.x, pos.y];
  126. };
  127.  
  128. const paint = e => {
  129. ctx.beginPath ();
  130. if (x && y) {
  131. ctx.moveTo (x, y);
  132. ctx.lineTo (x, y);
  133. } else {
  134. ctx.moveTo (pos.x, pos.y);
  135. setPosition (e);
  136. ctx.lineTo (pos.x, pos.y);
  137. }
  138. ctx.stroke ();
  139. ctx.closePath ();
  140. };
  141.  
  142. const move$ = fromEvent (canvas, 'mousemove');
  143. const up$ = fromEvent (canvas, 'mouseup');
  144. const down$ = fromEvent (canvas, 'mousedown').pipe (
  145. map (e => {
  146. const vals = setPosition (e);
  147. setX (vals[0]);
  148. setY (vals[1]);
  149. })
  150. );
  151.  
  152. const paints$ = down$.pipe (mergeMap (() => move$.pipe (takeUntil (up$))));
  153.  
  154. let subscription = paints$.subscribe (e => {
  155. setX (null);
  156. setY (null);
  157. paint (e);
  158. });
  159.  
  160. //touch pad movements
  161. const setPositionTouch = e => {
  162. let touch = e.touches[0];
  163. pos.x = touch.clientX - canvas.offsetLeft;
  164. pos.y = touch.clientY - canvas.offsetTop;
  165. return [pos.x, pos.y];
  166. };
  167.  
  168. const paintTouch = e => {
  169. ctx.beginPath ();
  170. if (x && y) {
  171. ctx.moveTo (x, y);
  172. ctx.lineTo (x, y);
  173. } else {
  174. ctx.moveTo (pos.x, pos.y);
  175. setPositionTouch (e);
  176. ctx.lineTo (pos.x, pos.y);
  177. }
  178. ctx.stroke ();
  179. ctx.closePath ();
  180. };
  181.  
  182. const moveTouch$ = fromEvent (canvas, 'touchmove');
  183. const upTouch$ = fromEvent (canvas, 'touchend');
  184. const downTouch$ = fromEvent (canvas, 'touchstart').pipe (
  185. map (e => {
  186. const vals = setPositionTouch (e);
  187. setX (vals[0]);
  188. setY (vals[1]);
  189. })
  190. );
  191.  
  192. const paintsTouch$ = downTouch$.pipe (
  193. mergeMap (() => moveTouch$.pipe (takeUntil (upTouch$)))
  194. );
  195.  
  196. let subscriptionTouch = paintsTouch$.subscribe (e => {
  197. setX (null);
  198. setY (null);
  199. paintTouch (e);
  200. });
  201.  
  202. if (reset) {
  203. ctx.clearRect (0, 0, canvas.width, canvas.height);
  204. subscription.unsubscribe ();
  205. subscription = paints$.subscribe (e => {
  206. setX (null);
  207. setY (null);
  208. paint (e);
  209. });
  210.  
  211. subscriptionTouch.unsubscribe ();
  212. subscriptionTouch = paintsTouch$.subscribe (e => {
  213. setX (null);
  214. setY (null);
  215. paintTouch (e);
  216. });
  217. }
  218. };
  219.  
  220. const onReset = () => {
  221. startPaint (true);
  222. };
  223.  
  224. useEffect (() => {
  225. startPaint ();
  226. disableBodyScroll (document.querySelector ('my-canvas'));
  227. }, []);
  228.  
  229. return (
  230. <div>
  231. <Header />
  232. <div className="container">
  233. <div style={divStyle}>
  234. <canvas id="my-canvas" style={canvasStyle} />
  235. </div>
  236. <div>
  237. <p>
  238. Select Your Marker
  239. {' '}
  240. </p>
  241. <div
  242. id="black-pen"
  243. className="whiteboard-color"
  244. style={{...whiteboardColor, ...blackPen}}
  245. />
  246. <div
  247. id="red-pen"
  248. className="whiteboard-color"
  249. style={{...whiteboardColor, ...redPen}}
  250. />
  251. <div
  252. id="blue-pen"
  253. className="whiteboard-color"
  254. style={{...whiteboardColor, ...bluePen}}
  255. />
  256. <div
  257. id="yellow-pen"
  258. className="whiteboard-color"
  259. style={{...whiteboardColor, ...yellowPen}}
  260. />
  261. <div
  262. id="green-pen"
  263. className="whiteboard-color"
  264. style={{...whiteboardColor, ...greenPen}}
  265. />
  266. </div>
  267. <button
  268. onClick={onReset}
  269. id="reset"
  270. className="btn red accent-1"
  271. style={btnStyle}
  272. >
  273. reset
  274. </button>
  275. <p>
  276. code for this whiteboard tool can be found <a href="#">here</a>
  277. </p>
  278. </div>
  279. </div>
  280. );
  281. };
  282.  
  283. export default Whiteboard;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement