SImageItem.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { SObjectItem } from "./SObjectItem";
  2. import { SPainter, SRect, SSize } from "@saga-web/draw/lib";
  3. import { SImageShowType } from "../enums/SImageShowType";
  4. import { SGraphItem } from "../SGraphItem";
  5. /**
  6. * 图片item
  7. *
  8. * @author 张宇(taohuzy@163.com)
  9. */
  10. export class SImageItem extends SObjectItem {
  11. /** 图片dom */
  12. img: CanvasImageSource | undefined;
  13. /** 展示模式 */
  14. private _showType: SImageShowType = SImageShowType.Full;
  15. get showType(): SImageShowType {
  16. return this._showType;
  17. }
  18. set showType(v: SImageShowType) {
  19. this._showType = v;
  20. this.update();
  21. }
  22. /** 图片地址 */
  23. private _url: string = "";
  24. get url(): string {
  25. return this._url;
  26. }
  27. set url(v: string) {
  28. this._url = v;
  29. this.img = new Image();
  30. this.img.src = v;
  31. this.img.onload = (): void => {
  32. this.update();
  33. };
  34. }
  35. /**
  36. * 构造函数
  37. *
  38. * @param parent 指向父Item
  39. * @param url 图片地址
  40. */
  41. constructor(parent: SGraphItem | null, url?: string) {
  42. super(parent);
  43. if (url) this.url = url;
  44. } // Constructor
  45. /**
  46. * Item对象边界区域
  47. *
  48. * @return SRect
  49. */
  50. boundingRect(): SRect {
  51. return new SRect(
  52. -this.width / 2,
  53. -this.height / 2,
  54. this.width,
  55. this.height
  56. );
  57. } // Function boundingRect()
  58. /**
  59. * 宽高发发生变化
  60. *
  61. * @param oldSize 改之前的大小
  62. * @param newSize 改之后大小
  63. * */
  64. protected onResize(oldSize: SSize, newSize: SSize): void {
  65. this.update();
  66. } // Function onResize()
  67. /**
  68. * Item绘制操作
  69. *
  70. * @param painter 绘画类
  71. */
  72. onDraw(painter: SPainter): void {
  73. if (this.img) {
  74. // 要绘制图片的宽度
  75. let width: number = 0;
  76. // 要绘制图片的宽度
  77. let height: number = 0;
  78. // 图片item的宽高比
  79. let itemAspectRatio: number = this.width / this.height;
  80. // 原始图片的宽高比
  81. let imgAspectRatio: number =
  82. (this.img.width as number) / (this.img.height as number);
  83. // 原始图片的高宽比
  84. let imgHwRatio: number =
  85. (this.img.height as number) / (this.img.width as number);
  86. if (this.showType == SImageShowType.Full) {
  87. width = this.width;
  88. height = this.height;
  89. } else if (this.showType == SImageShowType.Equivalency) {
  90. if (itemAspectRatio > imgAspectRatio) {
  91. height = this.height;
  92. width = imgAspectRatio * height;
  93. } else if (itemAspectRatio < imgAspectRatio) {
  94. width = this.width;
  95. height = width * imgHwRatio;
  96. } else {
  97. width = this.width;
  98. height = this.height;
  99. }
  100. } else if (this.showType == SImageShowType.AutoFit) {
  101. this.width = this.img.width as number;
  102. this.height = this.img.height as number;
  103. width = this.width;
  104. height = this.height;
  105. }
  106. painter.drawImage(this.img, -width / 2, -height / 2, width, height);
  107. }
  108. } // Function onDraw()
  109. } // Class SImageItem