image.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. import { Nullable } from "babylonjs/types";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { Tools } from "babylonjs/Misc/tools";
  4. import { Control } from "./control";
  5. import { Measure } from "../measure";
  6. /**
  7. * Class used to create 2D images
  8. */
  9. export class Image extends Control {
  10. private static _WorkingCanvas: Nullable<HTMLCanvasElement> = null;
  11. private _domImage: HTMLImageElement;
  12. private _imageWidth: number;
  13. private _imageHeight: number;
  14. private _loaded = false;
  15. private _stretch = Image.STRETCH_FILL;
  16. private _source: Nullable<string>;
  17. private _autoScale = false;
  18. private _sourceLeft = 0;
  19. private _sourceTop = 0;
  20. private _sourceWidth = 0;
  21. private _sourceHeight = 0;
  22. private _cellWidth: number = 0;
  23. private _cellHeight: number = 0;
  24. private _cellId: number = -1;
  25. private _populateNinePatchSlicesFromImage = false;
  26. private _sliceLeft: number;
  27. private _sliceRight: number;
  28. private _sliceTop: number;
  29. private _sliceBottom: number;
  30. private _detectPointerOnOpaqueOnly: boolean;
  31. /**
  32. * Observable notified when the content is loaded
  33. */
  34. public onImageLoadedObservable = new Observable<Image>();
  35. /**
  36. * Observable notified when _sourceLeft, _sourceTop, _sourceWidth and _sourceHeight are computed
  37. */
  38. public onSVGAttributesComputedObservable = new Observable<Image>();
  39. /**
  40. * Gets a boolean indicating that the content is loaded
  41. */
  42. public get isLoaded(): boolean {
  43. return this._loaded;
  44. }
  45. /**
  46. * Gets or sets a boolean indicating if nine patch slices (left, top, right, bottom) should be read from image data
  47. */
  48. public get populateNinePatchSlicesFromImage(): boolean {
  49. return this._populateNinePatchSlicesFromImage;
  50. }
  51. public set populateNinePatchSlicesFromImage(value: boolean) {
  52. if (this._populateNinePatchSlicesFromImage === value) {
  53. return;
  54. }
  55. this._populateNinePatchSlicesFromImage = value;
  56. if (this._populateNinePatchSlicesFromImage && this._loaded) {
  57. this._extractNinePatchSliceDataFromImage();
  58. }
  59. }
  60. /**
  61. * Gets or sets a boolean indicating if pointers should only be validated on pixels with alpha > 0.
  62. * Beware using this as this will comsume more memory as the image has to be stored twice
  63. */
  64. public get detectPointerOnOpaqueOnly(): boolean {
  65. return this._detectPointerOnOpaqueOnly;
  66. }
  67. public set detectPointerOnOpaqueOnly(value: boolean) {
  68. if (this._detectPointerOnOpaqueOnly === value) {
  69. return;
  70. }
  71. this._detectPointerOnOpaqueOnly = value;
  72. }
  73. /**
  74. * Gets or sets the left value for slicing (9-patch)
  75. */
  76. public get sliceLeft(): number {
  77. return this._sliceLeft;
  78. }
  79. public set sliceLeft(value: number) {
  80. if (this._sliceLeft === value) {
  81. return;
  82. }
  83. this._sliceLeft = value;
  84. this._markAsDirty();
  85. }
  86. /**
  87. * Gets or sets the right value for slicing (9-patch)
  88. */
  89. public get sliceRight(): number {
  90. return this._sliceRight;
  91. }
  92. public set sliceRight(value: number) {
  93. if (this._sliceRight === value) {
  94. return;
  95. }
  96. this._sliceRight = value;
  97. this._markAsDirty();
  98. }
  99. /**
  100. * Gets or sets the top value for slicing (9-patch)
  101. */
  102. public get sliceTop(): number {
  103. return this._sliceTop;
  104. }
  105. public set sliceTop(value: number) {
  106. if (this._sliceTop === value) {
  107. return;
  108. }
  109. this._sliceTop = value;
  110. this._markAsDirty();
  111. }
  112. /**
  113. * Gets or sets the bottom value for slicing (9-patch)
  114. */
  115. public get sliceBottom(): number {
  116. return this._sliceBottom;
  117. }
  118. public set sliceBottom(value: number) {
  119. if (this._sliceBottom === value) {
  120. return;
  121. }
  122. this._sliceBottom = value;
  123. this._markAsDirty();
  124. }
  125. /**
  126. * Gets or sets the left coordinate in the source image
  127. */
  128. public get sourceLeft(): number {
  129. return this._sourceLeft;
  130. }
  131. public set sourceLeft(value: number) {
  132. if (this._sourceLeft === value) {
  133. return;
  134. }
  135. this._sourceLeft = value;
  136. this._markAsDirty();
  137. }
  138. /**
  139. * Gets or sets the top coordinate in the source image
  140. */
  141. public get sourceTop(): number {
  142. return this._sourceTop;
  143. }
  144. public set sourceTop(value: number) {
  145. if (this._sourceTop === value) {
  146. return;
  147. }
  148. this._sourceTop = value;
  149. this._markAsDirty();
  150. }
  151. /**
  152. * Gets or sets the width to capture in the source image
  153. */
  154. public get sourceWidth(): number {
  155. return this._sourceWidth;
  156. }
  157. public set sourceWidth(value: number) {
  158. if (this._sourceWidth === value) {
  159. return;
  160. }
  161. this._sourceWidth = value;
  162. this._markAsDirty();
  163. }
  164. /**
  165. * Gets or sets the height to capture in the source image
  166. */
  167. public get sourceHeight(): number {
  168. return this._sourceHeight;
  169. }
  170. public set sourceHeight(value: number) {
  171. if (this._sourceHeight === value) {
  172. return;
  173. }
  174. this._sourceHeight = value;
  175. this._markAsDirty();
  176. }
  177. /**
  178. * Gets or sets a boolean indicating if the image can force its container to adapt its size
  179. * @see http://doc.babylonjs.com/how_to/gui#image
  180. */
  181. public get autoScale(): boolean {
  182. return this._autoScale;
  183. }
  184. public set autoScale(value: boolean) {
  185. if (this._autoScale === value) {
  186. return;
  187. }
  188. this._autoScale = value;
  189. if (value && this._loaded) {
  190. this.synchronizeSizeWithContent();
  191. }
  192. }
  193. /** Gets or sets the streching mode used by the image */
  194. public get stretch(): number {
  195. return this._stretch;
  196. }
  197. public set stretch(value: number) {
  198. if (this._stretch === value) {
  199. return;
  200. }
  201. this._stretch = value;
  202. this._markAsDirty();
  203. }
  204. /**
  205. * Gets or sets the internal DOM image used to render the control
  206. */
  207. public set domImage(value: HTMLImageElement) {
  208. this._domImage = value;
  209. this._loaded = false;
  210. if (this._domImage.width) {
  211. this._onImageLoaded();
  212. } else {
  213. this._domImage.onload = () => {
  214. this._onImageLoaded();
  215. };
  216. }
  217. }
  218. public get domImage(): HTMLImageElement {
  219. return this._domImage;
  220. }
  221. private _onImageLoaded(): void {
  222. this._imageWidth = this._domImage.width;
  223. this._imageHeight = this._domImage.height;
  224. this._loaded = true;
  225. if (this._populateNinePatchSlicesFromImage) {
  226. this._extractNinePatchSliceDataFromImage();
  227. }
  228. if (this._autoScale) {
  229. this.synchronizeSizeWithContent();
  230. }
  231. this.onImageLoadedObservable.notifyObservers(this);
  232. this._markAsDirty();
  233. }
  234. private _extractNinePatchSliceDataFromImage() {
  235. if (!Image._WorkingCanvas) {
  236. Image._WorkingCanvas = document.createElement('canvas');
  237. }
  238. const canvas = Image._WorkingCanvas;
  239. const context = canvas.getContext('2d')!;
  240. const width = this._domImage.width;
  241. const height = this._domImage.height;
  242. canvas.width = width;
  243. canvas.height = height;
  244. context.drawImage(this._domImage, 0, 0, width, height);
  245. const imageData = context.getImageData(0, 0, width, height);
  246. // Left and right
  247. this._sliceLeft = -1;
  248. this._sliceRight = -1;
  249. for (var x = 0; x < width; x++) {
  250. const alpha = imageData.data[x * 4 + 3];
  251. if (alpha > 127 && this._sliceLeft === -1) {
  252. this._sliceLeft = x;
  253. continue;
  254. }
  255. if (alpha < 127 && this._sliceLeft > -1) {
  256. this._sliceRight = x;
  257. break;
  258. }
  259. }
  260. // top and bottom
  261. this._sliceTop = -1;
  262. this._sliceBottom = -1;
  263. for (var y = 0; y < height; y++) {
  264. const alpha = imageData.data[y * width * 4 + 3];
  265. if (alpha > 127 && this._sliceTop === -1) {
  266. this._sliceTop = y;
  267. continue;
  268. }
  269. if (alpha < 127 && this._sliceTop > -1) {
  270. this._sliceBottom = y;
  271. break;
  272. }
  273. }
  274. }
  275. /**
  276. * Gets or sets image source url
  277. */
  278. public set source(value: Nullable<string>) {
  279. if (this._source === value) {
  280. return;
  281. }
  282. this._loaded = false;
  283. this._source = value;
  284. if (value) {
  285. this._svgCheck(value);
  286. }
  287. this._domImage = document.createElement("img");
  288. this._domImage.onload = () => {
  289. this._onImageLoaded();
  290. };
  291. if (value) {
  292. Tools.SetCorsBehavior(value, this._domImage);
  293. this._domImage.src = value;
  294. }
  295. }
  296. /**
  297. * Checks for svg document with icon id present
  298. */
  299. private _svgCheck(value: string) {
  300. if ((value.search(/.svg#/gi) !== -1) && (value.indexOf("#") === value.lastIndexOf("#"))) {
  301. var svgsrc = value.split('#')[0];
  302. var elemid = value.split('#')[1];
  303. // check if object alr exist in document
  304. var svgExist = <HTMLObjectElement> document.body.querySelector('object[data="' + svgsrc + '"]');
  305. if (svgExist) {
  306. if (svgExist.contentDocument) {
  307. // svg object alr exists
  308. this._getSVGAttribs(svgExist, elemid);
  309. } else {
  310. // wait for object to load
  311. svgExist.addEventListener("load", () => {
  312. this._getSVGAttribs(svgExist, elemid);
  313. });
  314. }
  315. } else {
  316. // create document object
  317. var svgImage = document.createElement("object");
  318. svgImage.data = svgsrc;
  319. svgImage.type = "image/svg+xml";
  320. svgImage.width = "0%";
  321. svgImage.height = "0%";
  322. document.body.appendChild(svgImage);
  323. // when the object has loaded, get the element attribs
  324. svgImage.onload = () => {
  325. var svgobj = <HTMLObjectElement> document.body.querySelector('object[data="' + svgsrc + '"]');
  326. if (svgobj) {
  327. this._getSVGAttribs(svgobj, elemid);
  328. }
  329. };
  330. }
  331. }
  332. }
  333. /**
  334. * Sets sourceLeft, sourceTop, sourceWidth, sourceHeight automatically
  335. * given external svg file and icon id
  336. */
  337. private _getSVGAttribs(svgsrc: HTMLObjectElement, elemid: string) {
  338. var svgDoc = svgsrc.contentDocument;
  339. // get viewbox width and height, get svg document width and height in px
  340. if (svgDoc && svgDoc.documentElement) {
  341. var vb = svgDoc.documentElement.getAttribute("viewBox");
  342. var docwidth = Number(svgDoc.documentElement.getAttribute("width"));
  343. var docheight = Number(svgDoc.documentElement.getAttribute("height"));
  344. // get element bbox and matrix transform
  345. var elem = <SVGGraphicsElement> <unknown> svgDoc.getElementById(elemid);
  346. if (elem instanceof SVGElement && vb && docwidth && docheight) {
  347. var vb_width = Number(vb.split(" ")[2]);
  348. var vb_height = Number(vb.split(" ")[3]);
  349. var elem_bbox = elem.getBBox();
  350. var elem_matrix_a = 1;
  351. var elem_matrix_d = 1;
  352. var elem_matrix_e = 0;
  353. var elem_matrix_f = 0;
  354. if (elem.transform && elem.transform.baseVal.consolidate()) {
  355. elem_matrix_a = elem.transform.baseVal.consolidate().matrix.a;
  356. elem_matrix_d = elem.transform.baseVal.consolidate().matrix.d;
  357. elem_matrix_e = elem.transform.baseVal.consolidate().matrix.e;
  358. elem_matrix_f = elem.transform.baseVal.consolidate().matrix.f;
  359. }
  360. // compute source coordinates and dimensions
  361. this.sourceLeft = ((elem_matrix_a * elem_bbox.x + elem_matrix_e) * docwidth) / vb_width;
  362. this.sourceTop = ((elem_matrix_d * elem_bbox.y + elem_matrix_f) * docheight) / vb_height;
  363. this.sourceWidth = (elem_bbox.width * elem_matrix_a) * (docwidth / vb_width);
  364. this.sourceHeight = (elem_bbox.height * elem_matrix_d) * (docheight / vb_height);
  365. this.onSVGAttributesComputedObservable.notifyObservers(this);
  366. }
  367. }
  368. }
  369. /**
  370. * Gets or sets the cell width to use when animation sheet is enabled
  371. * @see http://doc.babylonjs.com/how_to/gui#image
  372. */
  373. get cellWidth(): number {
  374. return this._cellWidth;
  375. }
  376. set cellWidth(value: number) {
  377. if (this._cellWidth === value) {
  378. return;
  379. }
  380. this._cellWidth = value;
  381. this._markAsDirty();
  382. }
  383. /**
  384. * Gets or sets the cell height to use when animation sheet is enabled
  385. * @see http://doc.babylonjs.com/how_to/gui#image
  386. */
  387. get cellHeight(): number {
  388. return this._cellHeight;
  389. }
  390. set cellHeight(value: number) {
  391. if (this._cellHeight === value) {
  392. return;
  393. }
  394. this._cellHeight = value;
  395. this._markAsDirty();
  396. }
  397. /**
  398. * Gets or sets the cell id to use (this will turn on the animation sheet mode)
  399. * @see http://doc.babylonjs.com/how_to/gui#image
  400. */
  401. get cellId(): number {
  402. return this._cellId;
  403. }
  404. set cellId(value: number) {
  405. if (this._cellId === value) {
  406. return;
  407. }
  408. this._cellId = value;
  409. this._markAsDirty();
  410. }
  411. /**
  412. * Creates a new Image
  413. * @param name defines the control name
  414. * @param url defines the image url
  415. */
  416. constructor(public name?: string, url: Nullable<string> = null) {
  417. super(name);
  418. this.source = url;
  419. }
  420. /**
  421. * Tests if a given coordinates belong to the current control
  422. * @param x defines x coordinate to test
  423. * @param y defines y coordinate to test
  424. * @returns true if the coordinates are inside the control
  425. */
  426. public contains(x: number, y: number): boolean {
  427. if (!super.contains(x, y)) {
  428. return false;
  429. }
  430. if (!this._detectPointerOnOpaqueOnly || !Image._WorkingCanvas) {
  431. return true;
  432. }
  433. const canvas = Image._WorkingCanvas;
  434. const context = canvas.getContext("2d")!;
  435. const width = this._currentMeasure.width | 0;
  436. const height = this._currentMeasure.height | 0;
  437. const imageData = context.getImageData(0, 0, width, height).data;
  438. x = (x - this._currentMeasure.left) | 0;
  439. y = (y - this._currentMeasure.top) | 0;
  440. const pickedPixel = imageData[(x + y * this._currentMeasure.width) * 4 + 3];
  441. return pickedPixel > 0;
  442. }
  443. protected _getTypeName(): string {
  444. return "Image";
  445. }
  446. /** Force the control to synchronize with its content */
  447. public synchronizeSizeWithContent() {
  448. if (!this._loaded) {
  449. return;
  450. }
  451. this.width = this._domImage.width + "px";
  452. this.height = this._domImage.height + "px";
  453. }
  454. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  455. if (this._loaded) {
  456. switch (this._stretch) {
  457. case Image.STRETCH_NONE:
  458. break;
  459. case Image.STRETCH_FILL:
  460. break;
  461. case Image.STRETCH_UNIFORM:
  462. break;
  463. case Image.STRETCH_NINE_PATCH:
  464. break;
  465. case Image.STRETCH_EXTEND:
  466. if (this._autoScale) {
  467. this.synchronizeSizeWithContent();
  468. }
  469. if (this.parent && this.parent.parent) { // Will update root size if root is not the top root
  470. this.parent.adaptWidthToChildren = true;
  471. this.parent.adaptHeightToChildren = true;
  472. }
  473. break;
  474. }
  475. }
  476. super._processMeasures(parentMeasure, context);
  477. }
  478. private _prepareWorkingCanvasForOpaqueDetection() {
  479. if (!this._detectPointerOnOpaqueOnly) {
  480. return;
  481. }
  482. if (!Image._WorkingCanvas) {
  483. Image._WorkingCanvas = document.createElement('canvas');
  484. }
  485. const canvas = Image._WorkingCanvas;
  486. const width = this._currentMeasure.width;
  487. const height = this._currentMeasure.height;
  488. const context = canvas.getContext("2d")!;
  489. canvas.width = width;
  490. canvas.height = height;
  491. context.clearRect(0, 0, width, height);
  492. }
  493. private _drawImage(context: CanvasRenderingContext2D, sx: number, sy: number, sw: number, sh: number, tx: number, ty: number, tw: number, th: number) {
  494. context.drawImage(this._domImage,
  495. sx, sy, sw, sh,
  496. tx, ty, tw, th);
  497. if (!this._detectPointerOnOpaqueOnly) {
  498. return;
  499. }
  500. const canvas = Image._WorkingCanvas!;
  501. context = canvas.getContext("2d")!;
  502. context.drawImage(this._domImage,
  503. sx, sy, sw, sh,
  504. tx - this._currentMeasure.left, ty - this._currentMeasure.top, tw, th);
  505. }
  506. public _draw(context: CanvasRenderingContext2D): void {
  507. context.save();
  508. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  509. context.shadowColor = this.shadowColor;
  510. context.shadowBlur = this.shadowBlur;
  511. context.shadowOffsetX = this.shadowOffsetX;
  512. context.shadowOffsetY = this.shadowOffsetY;
  513. }
  514. let x, y, width, height;
  515. if (this.cellId == -1) {
  516. x = this._sourceLeft;
  517. y = this._sourceTop;
  518. width = this._sourceWidth ? this._sourceWidth : this._imageWidth;
  519. height = this._sourceHeight ? this._sourceHeight : this._imageHeight;
  520. }
  521. else {
  522. let rowCount = this._domImage.naturalWidth / this.cellWidth;
  523. let column = (this.cellId / rowCount) >> 0;
  524. let row = this.cellId % rowCount;
  525. x = this.cellWidth * row;
  526. y = this.cellHeight * column;
  527. width = this.cellWidth;
  528. height = this.cellHeight;
  529. }
  530. this._prepareWorkingCanvasForOpaqueDetection();
  531. this._applyStates(context);
  532. if (this._loaded) {
  533. switch (this._stretch) {
  534. case Image.STRETCH_NONE:
  535. this._drawImage(context, x, y, width, height,
  536. this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  537. break;
  538. case Image.STRETCH_FILL:
  539. this._drawImage(context, x, y, width, height,
  540. this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  541. break;
  542. case Image.STRETCH_UNIFORM:
  543. var hRatio = this._currentMeasure.width / width;
  544. var vRatio = this._currentMeasure.height / height;
  545. var ratio = Math.min(hRatio, vRatio);
  546. var centerX = (this._currentMeasure.width - width * ratio) / 2;
  547. var centerY = (this._currentMeasure.height - height * ratio) / 2;
  548. this._drawImage(context, x, y, width, height,
  549. this._currentMeasure.left + centerX, this._currentMeasure.top + centerY, width * ratio, height * ratio);
  550. break;
  551. case Image.STRETCH_EXTEND:
  552. this._drawImage(context, x, y, width, height,
  553. this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  554. break;
  555. case Image.STRETCH_NINE_PATCH:
  556. this._renderNinePatch(context);
  557. break;
  558. }
  559. }
  560. context.restore();
  561. }
  562. private _renderCornerPatch(context: CanvasRenderingContext2D, x: number, y: number, width: number, height: number, targetX: number, targetY: number): void {
  563. this._drawImage(context, x, y, width, height, this._currentMeasure.left + targetX, this._currentMeasure.top + targetY, width, height);
  564. }
  565. private _renderNinePatch(context: CanvasRenderingContext2D): void {
  566. let height = this._imageHeight;
  567. let leftWidth = this._sliceLeft;
  568. let topHeight = this._sliceTop;
  569. let bottomHeight = this._imageHeight - this._sliceBottom;
  570. let rightWidth = this._imageWidth - this._sliceRight;
  571. let left = 0;
  572. let top = 0;
  573. if (this._populateNinePatchSlicesFromImage) {
  574. left = 1;
  575. top = 1;
  576. height -= 2;
  577. leftWidth -= 1;
  578. topHeight -= 1;
  579. bottomHeight -= 1;
  580. rightWidth -= 1;
  581. }
  582. const centerWidth = this._sliceRight - this._sliceLeft + 1;
  583. const targetCenterWidth = this._currentMeasure.width - rightWidth - this.sliceLeft + 1;
  584. const targetTopHeight = this._currentMeasure.height - height + this._sliceBottom;
  585. // Corners
  586. this._renderCornerPatch(context, left, top, leftWidth, topHeight, 0, 0);
  587. this._renderCornerPatch(context, left, this._sliceBottom, leftWidth, height - this._sliceBottom, 0, targetTopHeight);
  588. this._renderCornerPatch(context, this._sliceRight, top, rightWidth, topHeight, this._currentMeasure.width - rightWidth, 0);
  589. this._renderCornerPatch(context, this._sliceRight, this._sliceBottom, rightWidth, height - this._sliceBottom, this._currentMeasure.width - rightWidth, targetTopHeight);
  590. // Center
  591. this._drawImage(context, this._sliceLeft, this._sliceTop, centerWidth, this._sliceBottom - this._sliceTop + 1,
  592. this._currentMeasure.left + leftWidth, this._currentMeasure.top + topHeight, targetCenterWidth, targetTopHeight - topHeight + 1);
  593. // Borders
  594. this._drawImage(context, left, this._sliceTop, leftWidth, this._sliceBottom - this._sliceTop,
  595. this._currentMeasure.left, this._currentMeasure.top + topHeight, leftWidth, targetTopHeight - topHeight);
  596. this._drawImage(context, this._sliceRight, this._sliceTop, leftWidth, this._sliceBottom - this._sliceTop,
  597. this._currentMeasure.left + this._currentMeasure.width - rightWidth, this._currentMeasure.top + topHeight, leftWidth, targetTopHeight - topHeight);
  598. this._drawImage(context, this._sliceLeft, top, centerWidth, topHeight,
  599. this._currentMeasure.left + leftWidth, this._currentMeasure.top, targetCenterWidth, topHeight);
  600. this._drawImage(context, this._sliceLeft, this._sliceBottom, centerWidth, bottomHeight,
  601. this._currentMeasure.left + leftWidth, this._currentMeasure.top + targetTopHeight, targetCenterWidth, bottomHeight);
  602. }
  603. public dispose() {
  604. super.dispose();
  605. this.onImageLoadedObservable.clear();
  606. this.onSVGAttributesComputedObservable.clear();
  607. }
  608. // Static
  609. /** STRETCH_NONE */
  610. public static readonly STRETCH_NONE = 0;
  611. /** STRETCH_FILL */
  612. public static readonly STRETCH_FILL = 1;
  613. /** STRETCH_UNIFORM */
  614. public static readonly STRETCH_UNIFORM = 2;
  615. /** STRETCH_EXTEND */
  616. public static readonly STRETCH_EXTEND = 3;
  617. /** NINE_PATCH */
  618. public static readonly STRETCH_NINE_PATCH = 4;
  619. }