image.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 source(value: string) {
  35. if (this._source === value) {
  36. return;
  37. }
  38. this._loaded = false;
  39. this._source = value;
  40. this._domImage = new DOMImage();
  41. this._domImage.onload = () => {
  42. this._imageWidth = this._domImage.width;
  43. this._imageHeight = this._domImage.height;
  44. this._loaded = true;
  45. if (this._autoScale) {
  46. this.synchronizeSizeWithContent();
  47. }
  48. this._markAsDirty();
  49. }
  50. this._domImage.src = value;
  51. }
  52. constructor(public name: string, url: string) {
  53. super(name);
  54. this.source = url;
  55. }
  56. public synchronizeSizeWithContent() {
  57. if (!this._loaded) {
  58. return;
  59. }
  60. this.width = this._domImage.width + "px";
  61. this.height = this._domImage.height + "px";
  62. }
  63. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  64. context.save();
  65. this._applyStates(context);
  66. if (this._processMeasures(parentMeasure, context)) {
  67. if (this._loaded) {
  68. switch (this._stretch) {
  69. case Image.STRETCH_NONE:
  70. context.drawImage(this._domImage, this._currentMeasure.left, this._currentMeasure.top);
  71. break;
  72. case Image.STRETCH_FILL:
  73. context.drawImage(this._domImage, 0, 0, this._imageWidth, this._imageHeight,
  74. this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  75. break;
  76. case Image.STRETCH_UNIFORM:
  77. var hRatio = this._currentMeasure.width / this._imageWidth;
  78. var vRatio = this._currentMeasure.height / this._imageHeight;
  79. var ratio = Math.min(hRatio, vRatio);
  80. var centerX = (this._currentMeasure.width - this._imageWidth * ratio) / 2;
  81. var centerY = (this._currentMeasure.height - this._imageHeight * ratio) / 2;
  82. context.drawImage(this._domImage, 0, 0, this._imageWidth, this._imageHeight,
  83. this._currentMeasure.left + centerX, this._currentMeasure.top + centerY, this._imageWidth * ratio, this._imageHeight * ratio);
  84. break;
  85. case Image.STRETCH_EXTEND:
  86. context.drawImage(this._domImage, this._currentMeasure.left, this._currentMeasure.top);
  87. if (this._autoScale) {
  88. this.synchronizeSizeWithContent();
  89. }
  90. this._root.width = this.width;
  91. this._root.height = this.height;
  92. break;
  93. }
  94. }
  95. }
  96. context.restore();
  97. }
  98. // Static
  99. private static _STRETCH_NONE = 0;
  100. private static _STRETCH_FILL = 1;
  101. private static _STRETCH_UNIFORM = 2;
  102. private static _STRETCH_EXTEND = 3;
  103. public static get STRETCH_NONE(): number {
  104. return Image._STRETCH_NONE;
  105. }
  106. public static get STRETCH_FILL(): number {
  107. return Image._STRETCH_FILL;
  108. }
  109. public static get STRETCH_UNIFORM(): number {
  110. return Image._STRETCH_UNIFORM;
  111. }
  112. public static get STRETCH_EXTEND(): number {
  113. return Image._STRETCH_EXTEND;
  114. }
  115. }
  116. }