SBaseImageEdit.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SPainter, SRect, SSize, SColor, SPoint } from "@persagy-web/draw";
  27. import { SImageShowType, STextOrigin } from "@persagy-web/graph";
  28. import { SGraphItem, SAnchorItem, SLineStyle } from "@persagy-web/graph";
  29. import { SItemStatus } from "@persagy-web/big";
  30. import { SGraphEdit } from "..";
  31. import { Marker } from "../type/Marker";
  32. import { ItemOrder } from '@persagy-web/big/lib';
  33. /**
  34. * 图片编辑类
  35. *
  36. * @author 韩耀龙 <han_yao_long@163.com>
  37. */
  38. export class SBaseImageEdit extends SGraphEdit {
  39. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //属性
  41. /**编辑相关操作的数据 */
  42. data: Marker
  43. /** 起始锚点 */
  44. startItem: SGraphItem | null = null;
  45. /** 结束锚点 */
  46. endItem: SGraphItem | null = null;
  47. /** 图片 */
  48. img: CanvasImageSource | undefined;
  49. /** 展示模式 */
  50. private _showType: SImageShowType = SImageShowType.Full;
  51. get showType(): SImageShowType {
  52. return this._showType;
  53. } //Get showType
  54. set showType(v: SImageShowType) {
  55. this._showType = v;
  56. this.computeImgSize();
  57. this.update();
  58. } //Set showType
  59. /** 边框色 */
  60. private _strokeColor: SColor = SColor.Transparent;
  61. get strokeColor(): SColor {
  62. return this._strokeColor;
  63. } //Get strokeColor
  64. set strokeColor(v: SColor) {
  65. this._strokeColor = v;
  66. this.update();
  67. } //Set strokeColor
  68. /** 边框宽度 */
  69. private _lineWidth: number = 1;
  70. get lineWidth(): number {
  71. return this._lineWidth;
  72. } //Get lineWidth
  73. set lineWidth(v: number) {
  74. this._lineWidth = v;
  75. this.update();
  76. } //Set lineWidth
  77. /** 原点位置 */
  78. private _originPosition: STextOrigin = STextOrigin.LeftTop;
  79. get originPosition(): STextOrigin {
  80. return this._originPosition;
  81. } //Get originPosition
  82. set originPosition(v: STextOrigin) {
  83. this._originPosition = v;
  84. this.update();
  85. } //Set originPosition
  86. /** 线条样式 */
  87. private _lineStyle: SLineStyle = SLineStyle.Solid;
  88. get lineStyle(): SLineStyle {
  89. return this._lineStyle;
  90. } //Get lineStyle
  91. set lineStyle(v: SLineStyle) {
  92. this._lineStyle = v;
  93. this.update();
  94. } //Set lineStyle
  95. /** 图片加载是否完成 */
  96. isLoadOver: boolean = false;
  97. /** 图片的宽度 */
  98. private imgWidth: number = this.width;
  99. /** 图片的高度 */
  100. private imgHeight: number = this.height;
  101. /** 图片地址 */
  102. private _url: string = "";
  103. get url(): string {
  104. return this._url;
  105. } //Get url
  106. set url(v: string) {
  107. this._url = v;
  108. this.img = new Image();
  109. this.img.onload = (e): void => {
  110. const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  111. if (isiOS) {
  112. this.isLoadOver = true;
  113. this.computeImgSize();
  114. this.$emit("imgLoadOver");
  115. this.update();
  116. } else {
  117. // @ts-ignore
  118. const imgSrc = e.path[0].src;
  119. if (this.isUrlIdentical(imgSrc)) {
  120. this.isLoadOver = true;
  121. this.computeImgSize();
  122. this.$emit("imgLoadOver");
  123. this.update();
  124. }
  125. }
  126. };
  127. this.img.onerror = (e): void => {
  128. // @ts-ignore
  129. const imgSrc = e.path[0].src;
  130. if (this.isUrlIdentical(imgSrc)) {
  131. this.isLoadOver = false;
  132. this.$emit("imgLoadOver");
  133. this.update();
  134. console.log("加载图片错误!", e);
  135. }
  136. };
  137. this.img.src = v;
  138. } //Set url
  139. /** 锚点 list */
  140. anchorList: SAnchorItem[] = [];
  141. /** 宽度 */
  142. private _width: number = 64;
  143. /** 原点 */
  144. origin = new SPoint();
  145. get width(): number {
  146. return this._width;
  147. }//Get width
  148. set width(v: number) {
  149. if (v > 0) {
  150. if (v != this._width) {
  151. let w = this._width;
  152. this._width = v;
  153. this.onResize(
  154. new SSize(w, this._height),
  155. new SSize(this._width, this._height)
  156. );
  157. }
  158. }
  159. } //Set width
  160. /** 高度 */
  161. private _height: number = 64;
  162. get height(): number {
  163. return this._height;
  164. } //Get height
  165. set height(v: number) {
  166. if (v > 0) {
  167. if (v != this._height) {
  168. let h = this._height;
  169. this._height = v;
  170. this.onResize(
  171. new SSize(this._width, h),
  172. new SSize(this._width, this._height)
  173. );
  174. }
  175. }
  176. } //Set height
  177. /**编辑状态 */
  178. protected _status: SItemStatus = SItemStatus.Normal;
  179. get status(): SItemStatus {
  180. return this._status;
  181. } //Get status
  182. set status(value: SItemStatus) {
  183. const oldStatus = this._status;
  184. const newStatus = value;
  185. this._status = value;
  186. //状态变更触发事件
  187. this.$emit('StatusChange', oldStatus, newStatus)
  188. this.update();
  189. } //Set status
  190. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  191. // 函数
  192. /**
  193. * 构造函数
  194. *
  195. * @param parent 指向父对象
  196. */
  197. constructor(parent: SGraphItem | null, data: Marker) {
  198. super(parent);
  199. this.zOrder = ItemOrder.imageOrder;
  200. this.isTransform = false;
  201. this.url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAQKADAAQAAAABAAAAQAAAAABGUUKwAAAJW0lEQVR4Ae1Ze3CU1RW/537fbjZkKRJoNWBbHB20tuKIVGesdrRYGoeig6Vg6XSG0c0mtNkkkA1BpjNNR2kgDx7ZBLJJlHFsbccWWutQ8QF08MFD2w4VaXm0FUhweAQI2U2yu993b393k2/58iCbRBn/+e7M5p5z7rmP73fvPefcE8ac4iDgIOAg4CDgIOAg4CDgIOAg4CDgIOAg4CDgIOAg4CDgIDB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VTLrpmMfwPtotI5NwOnYZUYngUq5++P57dxw+fNhl75fUxxsQj6iwh7KqB7ZZT+NJk1i//OQgPQjGBgAxI1xXs2WoARNRMxfpvXXYiJLw+qoPoLPYFwj+OyOTL7YnRwb2pajI7aHIUnzVacZ5kHM6IZBsefPt/X9Fds32fujrKRHrE+sK1VVcHjjWaPixATDMDK4sbcdDM2defmPP/qTFVm4J1vkLsW7xJ1j73p5YPI7vQXiSH1tDkUfunur1voJMUoBMsUQIdh4fuL+5rvaBpOeJsaQ3sfTRNh7WswDHvTAlUwSxTiRabusnG4b5zAFQ73+VCPAVlianvXgxPikjU5tl7b4vsOJGuIDXOKMX7OtqrKk56ysKPkacHyEhtzHN1UWm+SWl4/V6zUuxiF0d3063ENcWNm9Y84a9wV8UPGvn09HXxAvYJ42KxPRYt7kTr8jq/ED5nSTNHRpRGXIA/Rau+uiCzpMQ0/D8Xi6N2BZTmnVwm4MyvSqLBKvx4MQMqTLO0v6zzz0SOu0JwD8o7oaFX2oNFj8fUX3cWFiLJbNqTeoNjaE1/7B4Vav3Pq7BXW0XIsvwQXj7s50yO2uPXQc0V/EC8o5KrDJI+5Ar3wfjCdsqx7e1R1OWXinIQ0fL8dm7q6qqVNDzqUpaAJjUzuDIpnar998XlMrR9Ztd12zHj2JWGwDoAV1ZEAw+Z/SwZ9j5qIoGU6DCqcCD0SRL/0qNV3+SQQYCSf8rcnY7adpyG58iMdKlFOMQDgIOAg4CDgIOAg4CDgIOAg4CDgIOAg4CDgIOAg4CDgIOAoMQ+D+N/M18Xo047gAAAABJRU5ErkJggg==";
  202. this.data = data;
  203. this.name = data.name;
  204. this.moveTo(data.pos.x, data.pos.y);
  205. if (data.size) {
  206. this.width = data.size.width;
  207. this.height = data.size.height;
  208. }
  209. if (data.style && data.style.default) {
  210. if (data.style.default.zorder) {
  211. this.zOrder = data.style.default.zorder;
  212. }
  213. // 图片url
  214. if (data.style.default.url) {
  215. this.url = data.style.default.url;
  216. }
  217. // 线宽
  218. if (data.style.default.lineWidth) {
  219. this.lineWidth = data.style.default.lineWidth;
  220. }
  221. // 线颜色
  222. if (data.style.default.strokeColor) {
  223. this.strokeColor = new SColor(data.style.default.strokeColor)
  224. }
  225. // 线样式
  226. if (data.style.default.lineStyle) {
  227. this.lineStyle = data.style.default.lineStyle
  228. }
  229. this.origin = new SPoint(this.width / 2, this.height / 2);
  230. }
  231. }
  232. /**
  233. * 将类中得数据转换为方便存储格式的方法
  234. *
  235. * @return 针对 item 类型保持相应的格式
  236. */
  237. toData(): any {
  238. if (this.data.size) {
  239. this.data.size.width = this.width;
  240. this.data.size.height = this.height;
  241. } else {
  242. const width = this.width;
  243. const height = this.height;
  244. this.data.size = {
  245. width,
  246. height,
  247. }
  248. }
  249. this.data.pos.x = this.pos.x;
  250. this.data.pos.y = this.pos.y;
  251. this.data.style.default.zorder = this.zOrder;
  252. this.data.style.default.url = this.url;
  253. this.data.style.default.lineWidth = this.lineWidth;
  254. this.data.style.default.strokeColor = this.strokeColor.value;
  255. this.data.style.default.lineStyle = this.lineStyle;
  256. return this.data
  257. } // Function toData()
  258. /**
  259. * 根据显示模式计算图片的宽高
  260. */
  261. computeImgSize(): void {
  262. if (this.isLoadOver) {
  263. // 要绘制图片的宽度
  264. let width = 0;
  265. // 要绘制图片的宽度
  266. let height = 0;
  267. // 图片 item 的宽高比
  268. let itemAspectRatio: number = this.width / this.height;
  269. // 原始图片的宽高比
  270. let imgAspectRatio: number =
  271. // @ts-ignore
  272. (this.img.width as number) / (this.img.height as number);
  273. // 原始图片的高宽比
  274. let imgHwRatio: number =
  275. // @ts-ignore
  276. (this.img.height as number) / (this.img.width as number);
  277. if (this.showType == SImageShowType.Full) {
  278. width = this.width;
  279. height = this.height;
  280. } else if (this.showType == SImageShowType.Equivalency) {
  281. if (itemAspectRatio > imgAspectRatio) {
  282. height = this.height;
  283. width = imgAspectRatio * height;
  284. } else if (itemAspectRatio < imgAspectRatio) {
  285. width = this.width;
  286. height = width * imgHwRatio;
  287. } else {
  288. width = this.width;
  289. height = this.height;
  290. }
  291. } else if (this.showType == SImageShowType.AutoFit) {
  292. // @ts-ignore
  293. this.width = this.img.width as number;
  294. // @ts-ignore
  295. this.height = this.img.height as number;
  296. width = this.width;
  297. height = this.height;
  298. }
  299. this.imgWidth = width;
  300. this.imgHeight = height;
  301. // 设置原点位置(默认左上角)
  302. if (this.originPosition == STextOrigin.Center) {
  303. this.origin = new SPoint(this.width / 2, this.height / 2);
  304. }
  305. }
  306. this.origin = new SPoint(this.width / 2, this.height / 2);
  307. this.update();
  308. } // Function computeImgSize()
  309. /**
  310. * 判断当前地址和回调地址是否相同
  311. *
  312. * @param imgUrl 图片回调地址
  313. * @return 当前地址和回调地址是否相同
  314. */
  315. private isUrlIdentical(imgUrl: string): boolean {
  316. if (this.url.indexOf("://") == -1) {
  317. // eslint-disable-next-line max-len
  318. const reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;
  319. if (reg.test(this.url)) {
  320. return this.url == imgUrl;
  321. } else {
  322. return this.url == this.GetUrlRelativePath(imgUrl);
  323. }
  324. } else {
  325. return this.url == imgUrl;
  326. }
  327. } // Function isUrlIdentical()
  328. /**
  329. * 截取绝对路径中的相对路径
  330. *
  331. * @param url 绝对路径
  332. * @return 相对路径
  333. */
  334. private GetUrlRelativePath(url: string): string {
  335. const arrUrl = url.split("//");
  336. const start = arrUrl[1].indexOf("/");
  337. const relUrl = arrUrl[1].substring(start);
  338. return relUrl;
  339. } // Function GetUrlRelativePath()
  340. /**
  341. * 大小改变
  342. *
  343. * @param oldSize 旧数据
  344. * @param newSize 新数据
  345. */
  346. resize(oldSize: SRect, newSize: SRect): void {
  347. this.width = newSize.width;
  348. this.height = newSize.height;
  349. this.update()
  350. } // Function resize()
  351. /**
  352. * Item 对象边界区域
  353. *
  354. * @return 边界区域
  355. */
  356. boundingRect(): SRect {
  357. return new SRect(
  358. -this.origin.x,
  359. -this.origin.y,
  360. this.width * 1,
  361. this.height * 1
  362. );
  363. } // Function boundingRect()
  364. /**
  365. * 宽高发发生变化
  366. *
  367. * @param oldSize 改之前的大小
  368. * @param newSize 改之后大小
  369. */
  370. protected onResize(oldSize: SSize, newSize: SSize): void {
  371. this.computeImgSize();
  372. this.update();
  373. } // Function onResize()
  374. /**
  375. * Item 绘制操作
  376. *
  377. * @param painter 绘画类
  378. */
  379. onDraw(painter: SPainter): void {
  380. painter.translate(-this.origin.x, -this.origin.y);
  381. if (this.isLoadOver) {
  382. // @ts-ignore
  383. painter.drawImage(this.img, 0, 0, this.imgWidth, this.imgHeight);
  384. }
  385. if (this.selected) {
  386. painter.shadow.shadowBlur = 10;
  387. painter.shadow.shadowColor = new SColor(`#00000033`);
  388. painter.shadow.shadowOffsetX = 5;
  389. painter.shadow.shadowOffsetY = 5;
  390. } else {
  391. painter.shadow.shadowColor = SColor.Transparent;
  392. }
  393. if (this.lineStyle == SLineStyle.Dashed) {
  394. painter.pen.lineDash = [
  395. painter.toPx(this.lineWidth * 3),
  396. painter.toPx(this.lineWidth * 7)
  397. ];
  398. } else if (this.lineStyle == SLineStyle.Dotted) {
  399. painter.pen.lineDash = [
  400. painter.toPx(2 * this.lineWidth),
  401. painter.toPx(2 * this.lineWidth)
  402. ];
  403. }
  404. painter.pen.lineWidth = this.lineWidth;
  405. painter.pen.color = this.strokeColor;
  406. painter.brush.color = SColor.Transparent;
  407. painter.drawRect(0, 0, this.width, this.height);
  408. } // Function onDraw()
  409. } // Class SBaseImageEdit