previewImageFoundation.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import BaseFoundation, { DefaultAdapter } from "../base/foundation";
  2. export interface PreviewImageAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  3. getContainer: () => HTMLDivElement;
  4. getImage: () => HTMLImageElement;
  5. setLoading: (loading: boolean) => void;
  6. setImageCursor: (canDrag: boolean) => void
  7. }
  8. export interface DragDirection {
  9. canDragVertical: boolean;
  10. canDragHorizontal: boolean
  11. }
  12. export interface ExtremeTranslate {
  13. x: number;
  14. y: number
  15. }
  16. export interface Offset {
  17. x: number;
  18. y: number
  19. }
  20. export interface Translate {
  21. x: number;
  22. y: number
  23. }
  24. interface CalcBoundingRectMouseOffset {
  25. offset: Offset;
  26. width: number;
  27. height: number;
  28. rotation?: number
  29. }
  30. export interface BoundingRectSize {
  31. width: number;
  32. height: number
  33. }
  34. export default class PreviewImageFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<PreviewImageAdapter<P, S>, P, S> {
  35. constructor(adapter: PreviewImageAdapter<P, S>) {
  36. super({ ...adapter });
  37. }
  38. startMouseClientPosition = { x: 0, y: 0 };
  39. originImageWidth = null;
  40. originImageHeight = null;
  41. containerWidth = 0;
  42. containerHeight = 0;
  43. init() {
  44. this._getContainerBoundingRectSize();
  45. }
  46. _isImageVertical = (): boolean => this.getProp("rotation") % 180 !== 0;
  47. _getContainerBoundingRectSize = () => {
  48. const containerDOM = this._adapter.getContainer();
  49. if (containerDOM) {
  50. this.containerWidth = containerDOM.clientWidth;
  51. this.containerHeight = containerDOM.clientHeight;
  52. }
  53. }
  54. _getAdaptationZoom = () => {
  55. let _zoom = 1;
  56. const containerDOM = this._adapter.getContainer();
  57. if (containerDOM && this.originImageWidth && this.originImageHeight) {
  58. const { rotation } = this.getProps();
  59. const { width: imageWidth, height: imageHeight } = this.calcBoundingRectSize(this.originImageWidth, this.originImageHeight, rotation);
  60. const reservedWidth = this.containerWidth - 80;
  61. const reservedHeight = this.containerHeight - 80;
  62. _zoom = Number(
  63. Math.min(reservedWidth / imageWidth, reservedHeight / imageHeight).toFixed(2)
  64. );
  65. }
  66. return _zoom;
  67. }
  68. _getInitialZoom = () => {
  69. const { ratio } = this.getProps();
  70. let _zoom = 1;
  71. if (ratio === 'adaptation') {
  72. _zoom = this._getAdaptationZoom();
  73. }
  74. return _zoom;
  75. }
  76. setLoading = (loading: boolean) => {
  77. this._adapter.setLoading(loading);
  78. }
  79. handleWindowResize = (): void => {
  80. this._getContainerBoundingRectSize();
  81. this.initializeImage();
  82. };
  83. handleLoad = (e: any): void => {
  84. if (e.target) {
  85. const { naturalWidth: w, naturalHeight: h } = e.target as any;
  86. this.originImageHeight = h;
  87. this.originImageWidth = w;
  88. this.setState({
  89. loading: false,
  90. } as any);
  91. // 图片初次加载,计算 zoom,zoom 改变不需要通过回调透出
  92. // When the image is loaded for the first time, zoom is calculated, and zoom changes do not need to be exposed through callbacks.
  93. this.initializeImage(false);
  94. }
  95. const { src, onLoad } = this.getProps();
  96. onLoad && onLoad(src);
  97. }
  98. handleError = (e: any): void => {
  99. const { onError, src } = this.getProps();
  100. this.setState({
  101. loading: false,
  102. } as any);
  103. onError && onError(src);
  104. }
  105. handleRatioChange = () => {
  106. this.initializeImage();
  107. }
  108. initializeImageZoom = (notify = true) => {
  109. const { currZoom } = this.getStates();
  110. const { onZoom } = this.getProps();
  111. const _zoom = this._getInitialZoom();
  112. if (currZoom !== _zoom) {
  113. onZoom(_zoom, notify);
  114. } else {
  115. this.changeZoom(_zoom);
  116. }
  117. }
  118. initializeTranslate = () => {
  119. this.setState({
  120. translate: {
  121. x: 0,
  122. y: 0
  123. }
  124. } as any);
  125. }
  126. initializeImage = (notify = true) => {
  127. this.initializeImageZoom(notify);
  128. this.initializeTranslate();
  129. }
  130. handleRightClickImage = (e: any) => {
  131. const { disableDownload } = this.getProps();
  132. if (disableDownload) {
  133. e.preventDefault();
  134. e.stopPropagation();
  135. return false;
  136. } else {
  137. return true;
  138. }
  139. };
  140. calcBoundingRectSize(width = 0, height = 0, rotation = 0) {
  141. const angleInRadians = rotation * Math.PI / 180;
  142. const sinTheta = Math.abs(Math.sin(angleInRadians));
  143. const cosTheta = Math.abs(Math.cos(angleInRadians));
  144. const boundingWidth = width * cosTheta + height * sinTheta;
  145. const boundingHeight = width * sinTheta + height * cosTheta;
  146. return {
  147. width: boundingWidth,
  148. height: boundingHeight
  149. };
  150. }
  151. getCanDragDirection = (width: number, height: number): DragDirection => {
  152. let canDragHorizontal = width > this.containerWidth;
  153. let canDragVertical = height > this.containerHeight;
  154. return {
  155. canDragVertical,
  156. canDragHorizontal,
  157. };
  158. };
  159. changeZoom = (newZoom: number, e?: WheelEvent): void => {
  160. const imageDOM = this._adapter.getImage();
  161. const { currZoom, translate, width, height } = this.getStates();
  162. const { rotation } = this.getProps();
  163. const changeScale = newZoom / (currZoom || 1);
  164. const newWidth = Math.floor(this.originImageWidth * newZoom);
  165. const newHeight = Math.floor(this.originImageHeight * newZoom);
  166. let newTranslateX = Math.floor(translate.x * changeScale);
  167. let newTranslateY = Math.floor(translate.y * changeScale);
  168. const imageBound = this.calcBoundingRectSize(width, height, rotation);
  169. const newImageBound = {
  170. width: imageBound.width * changeScale,
  171. height: imageBound.height * changeScale
  172. };
  173. if (e && imageDOM && e.target === imageDOM) {
  174. let angle = rotation % 360;
  175. angle < 0 && (angle = 360 + angle);
  176. switch (angle) {
  177. case 0:
  178. newTranslateX = (e.offsetX - 0.5 * width) * (1 - newZoom / currZoom) + translate.x;
  179. newTranslateY = (e.offsetY - 0.5 * height) * (1 - newZoom / currZoom) + translate.y;
  180. break;
  181. case 90:
  182. newTranslateX = (0.5 * height - e.offsetY) * (1 - newZoom / currZoom) + translate.x;
  183. newTranslateY = (e.offsetX - 0.5 * width) * (1 - newZoom / currZoom) + translate.y;
  184. break;
  185. case 180:
  186. newTranslateX = (0.5 * width - e.offsetX) * (1 - newZoom / currZoom) + translate.x;
  187. newTranslateY = (0.5 * height - e.offsetY) * (1 - newZoom / currZoom) + translate.y;
  188. break;
  189. case 270:
  190. newTranslateX = (e.offsetY - 0.5 * height) * (1 - newZoom / currZoom) + translate.x;
  191. newTranslateY = (0.5 * width - e.offsetX ) * (1 - newZoom / currZoom) + translate.y;
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. const newTranslate = this.getSafeTranslate(newImageBound.width, newImageBound.height, newTranslateX, newTranslateY);
  198. this.setState({
  199. translate: newTranslate,
  200. width: newWidth,
  201. height: newHeight,
  202. currZoom: newZoom,
  203. } as any);
  204. if (imageDOM) {
  205. const { canDragVertical, canDragHorizontal } = this.getCanDragDirection(newImageBound.width, newImageBound.height);
  206. const canDrag = canDragVertical || canDragHorizontal;
  207. this._adapter.setImageCursor(canDrag);
  208. }
  209. };
  210. getExtremeTranslate = (width: number, height: number): ExtremeTranslate => {
  211. return {
  212. x: (width - this.containerWidth) / 2,
  213. y: (height - this.containerHeight) / 2,
  214. };
  215. };
  216. getSafeTranslate = (width: number, height: number, translateX: number, translateY: number) => {
  217. const { x: extremeX, y: extremeY } = this.getExtremeTranslate(width, height);
  218. const { canDragVertical, canDragHorizontal } = this.getCanDragDirection(width, height);
  219. let newTranslateX = 0,
  220. newTranslateY = 0;
  221. if (canDragHorizontal) {
  222. newTranslateX = translateX > 0 ? Math.min(translateX, extremeX) : Math.max(translateX, -extremeX);
  223. }
  224. if (canDragVertical) {
  225. newTranslateY = translateY > 0 ? Math.min(translateY, extremeY) : Math.max(translateY, -extremeY);
  226. }
  227. return {
  228. x: newTranslateX,
  229. y: newTranslateY
  230. };
  231. }
  232. handleImageMove = (e: MouseEvent): void => {
  233. // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
  234. const mouseLeftPress = e.buttons === 1;
  235. if (mouseLeftPress) {
  236. this.moveImage(e);
  237. }
  238. };
  239. moveImage = (e: MouseEvent) => {
  240. const { clientX, clientY } = e;
  241. const { width, height, translate } = this.getStates();
  242. const { rotation } = this.getProps();
  243. const imageBound = this.calcBoundingRectSize(width, height, rotation);
  244. const { canDragVertical, canDragHorizontal } = this.getCanDragDirection(imageBound.width, imageBound.height);
  245. if (canDragVertical || canDragHorizontal) {
  246. let newTranslateX = canDragHorizontal ? translate.x + clientX - this.startMouseClientPosition.x : translate.x;
  247. let newTranslateY = canDragVertical ? translate.y + clientY - this.startMouseClientPosition.y : translate.y;
  248. const newTranslate = this.getSafeTranslate(imageBound.width, imageBound.height, newTranslateX, newTranslateY);
  249. this.setState({
  250. translate: newTranslate,
  251. } as any);
  252. this.startMouseClientPosition = {
  253. x: clientX,
  254. y: clientY
  255. };
  256. }
  257. };
  258. handleImageMouseDown = (e: any): void => {
  259. this.startMouseClientPosition = {
  260. x: e.clientX,
  261. y: e.clientY
  262. };
  263. };
  264. // 鼠标事件的 e.offset 是以 dom 旋转前左上角为零点的, 这个方法会转换为以旋转后元素的外接矩形左上角为零点的 offset
  265. calcBoundingRectMouseOffset = (calcBoundingRectMouseOffset: CalcBoundingRectMouseOffset) => {
  266. const {
  267. width,
  268. height,
  269. offset,
  270. rotation = 0
  271. } = calcBoundingRectMouseOffset;
  272. let degrees = rotation % 360;
  273. degrees = degrees >= 0 ? degrees : 360 + degrees;
  274. let boundOffsetX = 0,
  275. boundOffsetY = 0;
  276. switch (degrees) {
  277. case 0:
  278. boundOffsetX = offset.x;
  279. boundOffsetY = offset.y;
  280. break;
  281. case 90:
  282. boundOffsetX = height - offset.y;
  283. boundOffsetY = offset.x;
  284. break;
  285. case 180:
  286. boundOffsetX = width - offset.x;
  287. boundOffsetY = height - offset.y;
  288. break;
  289. case 270:
  290. boundOffsetX = offset.y;
  291. boundOffsetY = width - offset.x;
  292. break;
  293. default:
  294. break;
  295. }
  296. return {
  297. x: boundOffsetX,
  298. y: boundOffsetY
  299. };
  300. }
  301. }