image.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var DOMImage = Image;
  3. module BABYLON.GUI {
  4. export class Image extends Control {
  5. private _domImage: HTMLImageElement;
  6. private _imageWidth: number;
  7. private _imageHeight: number;
  8. private _loaded = false;
  9. private _stretch = Image.STRETCH_FILL;
  10. private _source: string;
  11. private _autoScale = false;
  12. public get autoScale(): boolean {
  13. return this._autoScale;
  14. }
  15. public set autoScale(value: boolean) {
  16. if (this._autoScale === value) {
  17. return;
  18. }
  19. this._autoScale = value;
  20. if (value && this._loaded) {
  21. this.synchronizeSizeWithContent();
  22. }
  23. }
  24. public get stretch(): number {
  25. return this._stretch;
  26. }
  27. public set stretch(value: number) {
  28. if (this._stretch === value) {
  29. return;
  30. }
  31. this._stretch = value;
  32. this._markAsDirty();
  33. }
  34. public set domImage(value: HTMLImageElement) {
  35. this._domImage = value;
  36. this._loaded = false;
  37. if (this._domImage.width) {
  38. this._onImageLoaded();
  39. } else {
  40. this._domImage.onload = () => {
  41. this._onImageLoaded();
  42. }
  43. }
  44. }
  45. public get domImage(): HTMLImageElement {
  46. return this._domImage;
  47. }
  48. private _onImageLoaded(): void {
  49. this._imageWidth = this._domImage.width;
  50. this._imageHeight = this._domImage.height;
  51. this._loaded = true;
  52. if (this._autoScale) {
  53. this.synchronizeSizeWithContent();
  54. }
  55. this._markAsDirty();
  56. }
  57. public set source(value: string) {
  58. if (this._source === value) {
  59. return;
  60. }
  61. this._loaded = false;
  62. this._source = value;
  63. this._domImage = new DOMImage();
  64. this._domImage.onload = () => {
  65. this._onImageLoaded();
  66. }
  67. this._domImage.src = value;
  68. }
  69. constructor(public name?: string, url?: string) {
  70. super(name);
  71. this.source = url;
  72. }
  73. protected _getTypeName(): string {
  74. return "Image";
  75. }
  76. public synchronizeSizeWithContent() {
  77. if (!this._loaded) {
  78. return;
  79. }
  80. this.width = this._domImage.width + "px";
  81. this.height = this._domImage.height + "px";
  82. }
  83. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  84. context.save();
  85. this._applyStates(context);
  86. if (this._processMeasures(parentMeasure, context)) {
  87. if (this._loaded) {
  88. switch (this._stretch) {
  89. case Image.STRETCH_NONE:
  90. context.drawImage(this._domImage, this._currentMeasure.left, this._currentMeasure.top);
  91. break;
  92. case Image.STRETCH_FILL:
  93. context.drawImage(this._domImage, 0, 0, this._imageWidth, this._imageHeight,
  94. this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  95. break;
  96. case Image.STRETCH_UNIFORM:
  97. var hRatio = this._currentMeasure.width / this._imageWidth;
  98. var vRatio = this._currentMeasure.height / this._imageHeight;
  99. var ratio = Math.min(hRatio, vRatio);
  100. var centerX = (this._currentMeasure.width - this._imageWidth * ratio) / 2;
  101. var centerY = (this._currentMeasure.height - this._imageHeight * ratio) / 2;
  102. context.drawImage(this._domImage, 0, 0, this._imageWidth, this._imageHeight,
  103. this._currentMeasure.left + centerX, this._currentMeasure.top + centerY, this._imageWidth * ratio, this._imageHeight * ratio);
  104. break;
  105. case Image.STRETCH_EXTEND:
  106. context.drawImage(this._domImage, this._currentMeasure.left, this._currentMeasure.top);
  107. if (this._autoScale) {
  108. this.synchronizeSizeWithContent();
  109. }
  110. this._root.width = this.width;
  111. this._root.height = this.height;
  112. break;
  113. }
  114. }
  115. }
  116. context.restore();
  117. }
  118. // Static
  119. private static _STRETCH_NONE = 0;
  120. private static _STRETCH_FILL = 1;
  121. private static _STRETCH_UNIFORM = 2;
  122. private static _STRETCH_EXTEND = 3;
  123. public static get STRETCH_NONE(): number {
  124. return Image._STRETCH_NONE;
  125. }
  126. public static get STRETCH_FILL(): number {
  127. return Image._STRETCH_FILL;
  128. }
  129. public static get STRETCH_UNIFORM(): number {
  130. return Image._STRETCH_UNIFORM;
  131. }
  132. public static get STRETCH_EXTEND(): number {
  133. return Image._STRETCH_EXTEND;
  134. }
  135. }
  136. }