SImageItem.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 { SObjectItem } from "./SObjectItem";
  27. import { SPainter, SRect, SSize, SColor, SPoint } from "@persagy-web/draw";
  28. import { SImageShowType, STextOrigin } from "..";
  29. import { SGraphItem } from "../SGraphItem";
  30. /**
  31. * 图片item
  32. *
  33. * @author 张宇(taohuzy@163.com)
  34. */
  35. export class SImageItem extends SObjectItem {
  36. /** 图片dom */
  37. img: CanvasImageSource | undefined;
  38. /** 展示模式 */
  39. private _showType: SImageShowType = SImageShowType.Full;
  40. get showType(): SImageShowType {
  41. return this._showType;
  42. }
  43. set showType(v: SImageShowType) {
  44. this._showType = v;
  45. this.computeImgSize();
  46. this.update();
  47. }
  48. /** 边框色 */
  49. private _strokeColor: SColor = SColor.Transparent;
  50. get strokeColor(): SColor {
  51. return this._strokeColor;
  52. }
  53. set strokeColor(v: SColor) {
  54. this._strokeColor = v;
  55. this.update();
  56. }
  57. /** 边框宽度 */
  58. private _lineWidth: number = 1;
  59. get lineWidth(): number {
  60. return this._lineWidth;
  61. }
  62. set lineWidth(v: number) {
  63. this._lineWidth = v;
  64. this.update();
  65. }
  66. /** 原点位置 */
  67. private _originPosition: STextOrigin = STextOrigin.LeftTop;
  68. get originPosition(): STextOrigin {
  69. return this._originPosition;
  70. }
  71. set originPosition(v: STextOrigin) {
  72. this._originPosition = v;
  73. this.update();
  74. }
  75. /** 图片加载是否完成 */
  76. isLoadOver: boolean = false;
  77. /** 图片的宽度 */
  78. private imgWidth: number = this.width;
  79. /** 图片的高度 */
  80. private imgHeight: number = this.height;
  81. /** 图片地址 */
  82. private _url: string = "";
  83. get url(): string {
  84. return this._url;
  85. }
  86. set url(v: string) {
  87. this._url = v;
  88. this.img = new Image();
  89. this.img.onload = (e): void => {
  90. // @ts-ignore
  91. const imgSrc = e.path[0].src;
  92. if (this.isUrlIdentical(imgSrc)) {
  93. this.isLoadOver = true;
  94. this.computeImgSize();
  95. this.$emit("imgLoadOver");
  96. this.update();
  97. }
  98. };
  99. this.img.onerror = (e): void => {
  100. // @ts-ignore
  101. const imgSrc = e.path[0].src;
  102. if (this.isUrlIdentical(imgSrc)) {
  103. this.isLoadOver = false;
  104. this.$emit("imgLoadOver");
  105. this.update();
  106. console.log("加载图片错误!", e);
  107. }
  108. };
  109. this.img.src = v;
  110. }
  111. /**
  112. * 构造函数
  113. *
  114. * @param parent 指向父Item
  115. * @param url 图片地址
  116. */
  117. constructor(parent: SGraphItem | null, url?: string) {
  118. super(parent);
  119. if (url) this.url = url;
  120. } // Constructor
  121. /**
  122. * 根据显示模式计算图片的宽高
  123. */
  124. computeImgSize(): void {
  125. if (this.isLoadOver) {
  126. // 要绘制图片的宽度
  127. let width = 0;
  128. // 要绘制图片的宽度
  129. let height = 0;
  130. // 图片item的宽高比
  131. let itemAspectRatio: number = this.width / this.height;
  132. // 原始图片的宽高比
  133. let imgAspectRatio: number =
  134. // @ts-ignore
  135. (this.img.width as number) / (this.img.height as number);
  136. // 原始图片的高宽比
  137. let imgHwRatio: number =
  138. // @ts-ignore
  139. (this.img.height as number) / (this.img.width as number);
  140. if (this.showType == SImageShowType.Full) {
  141. width = this.width;
  142. height = this.height;
  143. } else if (this.showType == SImageShowType.Equivalency) {
  144. if (itemAspectRatio > imgAspectRatio) {
  145. height = this.height;
  146. width = imgAspectRatio * height;
  147. } else if (itemAspectRatio < imgAspectRatio) {
  148. width = this.width;
  149. height = width * imgHwRatio;
  150. } else {
  151. width = this.width;
  152. height = this.height;
  153. }
  154. } else if (this.showType == SImageShowType.AutoFit) {
  155. // @ts-ignore
  156. this.width = this.img.width as number;
  157. // @ts-ignore
  158. this.height = this.img.height as number;
  159. width = this.width;
  160. height = this.height;
  161. }
  162. this.imgWidth = width;
  163. this.imgHeight = height;
  164. // 设置原点位置(默认左上角)
  165. if (this.originPosition == STextOrigin.Centrum) {
  166. this.origin = new SPoint(this.width / 2, this.height / 2);
  167. }
  168. }
  169. } // Function computeImgSize()
  170. /**
  171. * 判断当前地址和回调地址是否相同
  172. * @param imgUrl 图片回调地址
  173. *
  174. */
  175. private isUrlIdentical(imgUrl: string): boolean {
  176. if (this.url.indexOf("://") == -1) {
  177. const reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;
  178. if (reg.test(this.url)) {// url是base64地址
  179. if(this.url == imgUrl) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. } else {// url是相对地址
  185. if (this.url == this.GetUrlRelativePath(imgUrl)) {
  186. return true;
  187. } else {
  188. return false;
  189. }
  190. }
  191. } else {// url是绝对地址
  192. if(this.url == imgUrl) {
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. } // Function isUrlIdentical()
  199. /**
  200. * 截取绝对路径中的相对路径
  201. * @param url 绝对路径
  202. *
  203. */
  204. private GetUrlRelativePath(url: string): string {
  205. var arrUrl = url.split("//");
  206. var start = arrUrl[1].indexOf("/");
  207.   var relUrl = arrUrl[1].substring(start);
  208. return relUrl;
  209. } // Function GetUrlRelativePath()
  210. /**
  211. * Item对象边界区域
  212. *
  213. * @return SRect
  214. */
  215. boundingRect(): SRect {
  216. return new SRect(-this.origin.x, -this.origin.y, this.width, this.height);
  217. } // Function boundingRect()
  218. /**
  219. * 宽高发发生变化
  220. *
  221. * @param oldSize 改之前的大小
  222. * @param newSize 改之后大小
  223. * */
  224. protected onResize(oldSize: SSize, newSize: SSize): void {
  225. this.computeImgSize();
  226. this.update();
  227. } // Function onResize()
  228. /**
  229. * Item绘制操作
  230. *
  231. * @param painter 绘画类
  232. */
  233. onDraw(painter: SPainter): void {
  234. painter.translate(-this.origin.x, -this.origin.y);
  235. if (this.isLoadOver) {
  236. // @ts-ignore
  237. painter.drawImage(this.img, 0, 0, this.imgWidth, this.imgHeight);
  238. }
  239. if (this.selected) {
  240. painter.shadow.shadowBlur = 10;
  241. painter.shadow.shadowColor = new SColor(`#00000033`);
  242. painter.shadow.shadowOffsetX = 5;
  243. painter.shadow.shadowOffsetY = 5;
  244. } else {
  245. painter.shadow.shadowColor = SColor.Transparent;
  246. }
  247. painter.pen.lineWidth = this.lineWidth;
  248. painter.pen.color = this.strokeColor;
  249. painter.brush.color = SColor.Transparent;
  250. painter.drawRect(0, 0, this.width, this.height);
  251. } // Function onDraw()
  252. } // Class SImageItem