screenshot.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { useEffect, useLayoutEffect, useRef, useState } from 'react';
  8. import { Observable, Subscription } from 'rxjs';
  9. import { Viewport } from '../../mol-canvas3d/camera/util';
  10. import { PluginContext } from '../../mol-plugin/context';
  11. import { ViewportScreenshotHelper } from '../../mol-plugin/util/viewport-screenshot';
  12. import { shallowEqual } from '../../mol-util/object';
  13. import { useBehavior } from '../hooks/use-behavior';
  14. export interface ScreenshotPreviewProps {
  15. plugin: PluginContext,
  16. suspend?: boolean,
  17. cropFrameColor?: string,
  18. borderColor?: string,
  19. borderWidth?: number,
  20. customBackground?: string
  21. }
  22. const _ScreenshotPreview = (props: ScreenshotPreviewProps) => {
  23. const { plugin, cropFrameColor } = props;
  24. const helper = plugin.helpers.viewportScreenshot;
  25. if (!helper) return null;
  26. const [currentCanvas, setCurrentCanvas] = useState<HTMLCanvasElement | null>(null);
  27. const canvasRef = useRef<HTMLCanvasElement | null>(null);
  28. const propsRef = useRef(props);
  29. useEffect(() => {
  30. propsRef.current = props;
  31. }, Object.values(props));
  32. useEffect(() => {
  33. if (currentCanvas !== canvasRef.current) {
  34. setCurrentCanvas(canvasRef.current);
  35. }
  36. });
  37. useEffect(() => {
  38. let isDirty = false;
  39. const subs: Subscription[] = [];
  40. function subscribe<T>(xs: Observable<T> | undefined, f: (v: T) => any) {
  41. if (!xs) return;
  42. subs.push(xs.subscribe(f));
  43. }
  44. function preview() {
  45. const p = propsRef.current;
  46. if (!p.suspend && canvasRef.current) {
  47. drawPreview(helper!, canvasRef.current, p.customBackground, p.borderColor, p.borderWidth);
  48. }
  49. if (!canvasRef.current) isDirty = true;
  50. }
  51. const interval = setInterval(() => {
  52. if (isDirty) {
  53. isDirty = false;
  54. preview();
  55. }
  56. }, 1000 / 8);
  57. subscribe(plugin.events.canvas3d.settingsUpdated, () => isDirty = true);
  58. subscribe(plugin.canvas3d?.didDraw, () => isDirty = true);
  59. subscribe(plugin.state.data.behaviors.isUpdating, v => {
  60. if (!v) isDirty = true;
  61. });
  62. subscribe(helper.behaviors.values, () => isDirty = true);
  63. subscribe(helper.behaviors.cropParams, () => isDirty = true);
  64. let resizeObserver: any = void 0;
  65. if (typeof ResizeObserver !== 'undefined') {
  66. resizeObserver = new ResizeObserver(() => isDirty = true);
  67. }
  68. const canvas = canvasRef.current;
  69. resizeObserver?.observe(canvas);
  70. preview();
  71. return () => {
  72. clearInterval(interval);
  73. subs.forEach(s => s.unsubscribe());
  74. resizeObserver?.unobserve(canvas);
  75. };
  76. }, [helper]);
  77. useLayoutEffect(() => {
  78. if (canvasRef.current) {
  79. drawPreview(helper, canvasRef.current, props.customBackground, props.borderColor, props.borderWidth);
  80. }
  81. }, [...Object.values(props)]);
  82. return <>
  83. <div style={{ position: 'relative', width: '100%', height: '100%' }}>
  84. <canvas ref={canvasRef} onContextMenu={e => { e.preventDefault(); e.stopPropagation(); }} style={{ display: 'block', width: '100%', height: '100%' }}></canvas>
  85. <ViewportFrame plugin={plugin} canvas={currentCanvas} color={cropFrameColor} />
  86. </div>
  87. </>;
  88. };
  89. export const ScreenshotPreview = React.memo(_ScreenshotPreview, (prev, next) => shallowEqual(prev, next));
  90. declare const ResizeObserver: any;
  91. function drawPreview(helper: ViewportScreenshotHelper, target: HTMLCanvasElement, customBackground?: string, borderColor?: string, borderWidth?: number) {
  92. const { canvas, width, height } = helper.getPreview()!;
  93. const ctx = target.getContext('2d');
  94. if (!ctx) return;
  95. const w = target.clientWidth;
  96. const h = target.clientHeight;
  97. target.width = w;
  98. target.height = h;
  99. ctx.clearRect(0, 0, w, h);
  100. const frame = getViewportFrame(width, height, w, h);
  101. if (customBackground) {
  102. ctx.fillStyle = customBackground;
  103. ctx.fillRect(frame.x, frame.y, frame.width, frame.height);
  104. } else if (helper.values.transparent) {
  105. // must be an odd number
  106. const s = 13;
  107. for (let i = 0; i < frame.width; i += s) {
  108. for (let j = 0; j < frame.height; j += s) {
  109. ctx.fillStyle = (i + j) % 2 ? '#ffffff' : '#bfbfbf';
  110. const x = frame.x + i, y = frame.y + j;
  111. const w = i + s > frame.width ? frame.width - i : s;
  112. const h = j + s > frame.height ? frame.height - j : s;
  113. ctx.fillRect(x, y, w, h);
  114. }
  115. }
  116. }
  117. ctx.drawImage(canvas, frame.x, frame.y, frame.width, frame.height);
  118. if (borderColor && borderWidth) {
  119. const w = borderWidth;
  120. ctx.rect(frame.x, frame.y, frame.width, frame.height);
  121. ctx.rect(frame.x + w, frame.y + w, frame.width - 2 * w, frame.height - 2 * w);
  122. ctx.fillStyle = borderColor;
  123. ctx.fill('evenodd');
  124. }
  125. }
  126. function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: { plugin: PluginContext, canvas: HTMLCanvasElement | null, color?: string }) {
  127. const helper = plugin.helpers.viewportScreenshot;
  128. if (!helper || !canvas) return null;
  129. const params = useBehavior(helper.behaviors.values);
  130. const cropParams = useBehavior(helper.behaviors.cropParams);
  131. const crop = useBehavior(helper.behaviors.relativeCrop!);
  132. const cropFrameRef = useRef<Viewport>({ x: 0, y: 0, width: 0, height: 0 });
  133. useBehavior(params?.resolution.name === 'viewport' ? plugin.canvas3d?.resized : void 0);
  134. const [drag, setDrag] = React.useState<string>('');
  135. const [start, setStart] = useState([0, 0]);
  136. const [current, setCurrent] = useState([0, 0]);
  137. const { width, height } = helper.getSizeAndViewport();
  138. const frame = getViewportFrame(width, height, canvas.clientWidth, canvas.clientHeight);
  139. const cropFrame: Viewport = {
  140. x: frame.x + Math.floor(frame.width * crop.x),
  141. y: frame.y + Math.floor(frame.height * crop.y),
  142. width: Math.ceil(frame.width * crop.width),
  143. height: Math.ceil(frame.height * crop.height)
  144. };
  145. const rectCrop = toRect(cropFrame);
  146. const rectFrame = toRect(frame);
  147. if (drag === 'move') {
  148. rectCrop.l += current[0] - start[0];
  149. rectCrop.r += current[0] - start[0];
  150. rectCrop.t += current[1] - start[1];
  151. rectCrop.b += current[1] - start[1];
  152. } else if (drag) {
  153. if (drag.indexOf('left') >= 0) {
  154. rectCrop.l += current[0] - start[0];
  155. } else if (drag.indexOf('right') >= 0) {
  156. rectCrop.r += current[0] - start[0];
  157. }
  158. if (drag.indexOf('top') >= 0) {
  159. rectCrop.t += current[1] - start[1];
  160. } else if (drag.indexOf('bottom') >= 0) {
  161. rectCrop.b += current[1] - start[1];
  162. }
  163. }
  164. if (rectCrop.l > rectCrop.r) {
  165. const t = rectCrop.l;
  166. rectCrop.l = rectCrop.r;
  167. rectCrop.r = t;
  168. }
  169. if (rectCrop.t > rectCrop.b) {
  170. const t = rectCrop.t;
  171. rectCrop.t = rectCrop.b;
  172. rectCrop.b = t;
  173. }
  174. const pad = 40;
  175. rectCrop.l = Math.min(rectFrame.r - pad, Math.max(rectFrame.l, rectCrop.l));
  176. rectCrop.r = Math.max(rectFrame.l + pad, Math.min(rectFrame.r, rectCrop.r));
  177. rectCrop.t = Math.min(rectFrame.b - pad, Math.max(rectFrame.t, rectCrop.t));
  178. rectCrop.b = Math.max(rectFrame.t + pad, Math.min(rectFrame.b, rectCrop.b));
  179. cropFrame.x = rectCrop.l;
  180. cropFrame.y = rectCrop.t;
  181. cropFrame.width = rectCrop.r - rectCrop.l + 1;
  182. cropFrame.height = rectCrop.b - rectCrop.t + 1;
  183. cropFrameRef.current = cropFrame;
  184. const onMove = (e: MouseEvent) => {
  185. e.preventDefault();
  186. setCurrent([e.pageX, e.pageY]);
  187. };
  188. const onTouchMove = (e: TouchEvent) => {
  189. e.preventDefault();
  190. const t = e.touches[0];
  191. setCurrent([t.pageX, t.pageY]);
  192. };
  193. const onTouchStart = (e: React.TouchEvent) => {
  194. e.preventDefault();
  195. setDrag(e.currentTarget.getAttribute('data-drag')! as any);
  196. const t = e.touches[0];
  197. const p = [t.pageX, t.pageY];
  198. setStart(p);
  199. setCurrent(p);
  200. window.addEventListener('touchend', onTouchEnd);
  201. window.addEventListener('touchmove', onTouchMove);
  202. };
  203. const onStart = (e: React.MouseEvent<HTMLElement>) => {
  204. e.preventDefault();
  205. setDrag(e.currentTarget.getAttribute('data-drag')! as any);
  206. const p = [e.pageX, e.pageY];
  207. setStart(p);
  208. setCurrent(p);
  209. window.addEventListener('mouseup', onEnd);
  210. window.addEventListener('mousemove', onMove);
  211. };
  212. const onEnd = () => {
  213. window.removeEventListener('mouseup', onEnd);
  214. window.removeEventListener('mousemove', onMove);
  215. finish();
  216. };
  217. const onTouchEnd = () => {
  218. window.removeEventListener('touchend', onTouchEnd);
  219. window.removeEventListener('touchmove', onTouchMove);
  220. finish();
  221. };
  222. function finish() {
  223. const cropFrame = cropFrameRef.current;
  224. if (cropParams.auto) {
  225. helper?.behaviors.cropParams.next({ ...cropParams, auto: false });
  226. }
  227. helper?.behaviors.relativeCrop.next({
  228. x: (cropFrame.x - frame.x) / frame.width,
  229. y: (cropFrame.y - frame.y) / frame.height,
  230. width: cropFrame.width / frame.width,
  231. height: cropFrame.height / frame.height
  232. });
  233. setDrag('');
  234. const p = [0, 0];
  235. setStart(p);
  236. setCurrent(p);
  237. }
  238. const contextMenu = (e: React.MouseEvent) => {
  239. e.preventDefault();
  240. e.stopPropagation();
  241. };
  242. const d = 4;
  243. const border = `3px solid ${color}`;
  244. const transparent = 'transparent';
  245. return <>
  246. <div data-drag='move' style={{ position: 'absolute', left: cropFrame.x, top: cropFrame.y, width: cropFrame.width, height: cropFrame.height, border, cursor: 'move' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  247. <div data-drag='left' style={{ position: 'absolute', left: cropFrame.x - d, top: cropFrame.y + d, width: 4 * d, height: cropFrame.height - d, background: transparent, cursor: 'w-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  248. <div data-drag='right' style={{ position: 'absolute', left: rectCrop.r - 2 * d, top: cropFrame.y, width: 4 * d, height: cropFrame.height - d, background: transparent, cursor: 'w-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  249. <div data-drag='top' style={{ position: 'absolute', left: cropFrame.x - d, top: cropFrame.y - d, width: cropFrame.width + 2 * d, height: 4 * d, background: transparent, cursor: 'n-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  250. <div data-drag='bottom' style={{ position: 'absolute', left: cropFrame.x - d, top: rectCrop.b - 2 * d, width: cropFrame.width + 2 * d, height: 4 * d, background: transparent, cursor: 'n-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  251. <div data-drag='top, left' style={{ position: 'absolute', left: rectCrop.l - d, top: rectCrop.t - d, width: 4 * d, height: 4 * d, background: transparent, cursor: 'nw-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  252. <div data-drag='bottom, right' style={{ position: 'absolute', left: rectCrop.r - 2 * d, top: rectCrop.b - 2 * d, width: 4 * d, height: 4 * d, background: transparent, cursor: 'nw-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  253. <div data-drag='top, right' style={{ position: 'absolute', left: rectCrop.r - 2 * d, top: rectCrop.t - d, width: 4 * d, height: 4 * d, background: transparent, cursor: 'ne-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  254. <div data-drag='bottom, left' style={{ position: 'absolute', left: rectCrop.l - d, top: rectCrop.b - 2 * d, width: 4 * d, height: 4 * d, background: transparent, cursor: 'ne-resize' }} onMouseDown={onStart} onTouchStart={onTouchStart} draggable={false} onContextMenu={contextMenu} />
  255. </>;
  256. }
  257. function toRect(viewport: Viewport) {
  258. return { l: viewport.x, t: viewport.y, r: viewport.x + viewport.width - 1, b: viewport.y + viewport.height - 1 };
  259. }
  260. function getViewportFrame(srcWidth: number, srcHeight: number, w: number, h: number): Viewport {
  261. const a0 = srcWidth / srcHeight;
  262. const a1 = w / h;
  263. if (a0 <= a1) {
  264. const t = h * a0;
  265. return { x: Math.round((w - t) / 2), y: 0, width: Math.round(t), height: h };
  266. } else {
  267. const t = w / a0;
  268. return { x: 0, y: Math.round((h - t) / 2), width: w, height: Math.round(t) };
  269. }
  270. }