babylon.dynamicTexture.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. module BABYLON {
  2. /**
  3. * A class extending {BABYLON.Texture} allowing drawing on a texture
  4. * @see http://doc.babylonjs.com/how_to/dynamictexture
  5. */
  6. export class DynamicTexture extends Texture {
  7. private _generateMipMaps: boolean;
  8. private _canvas: HTMLCanvasElement;
  9. private _context: CanvasRenderingContext2D;
  10. private _engine: Engine;
  11. /**
  12. * Creates a {BABYLON.DynamicTexture}
  13. * @param name defines the name of the texture
  14. * @param options provides 3 alternatives for width and height of texture, a canvas, object with width and height properties, number for both width and height
  15. * @param scene defines the scene where you want the texture
  16. * @param generateMipMaps defines the use of MinMaps or not (default is false)
  17. * @param samplingMode defines the sampling mode to use (default is BABYLON.Texture.TRILINEAR_SAMPLINGMODE)
  18. * @param format defines the texture format to use (default is BABYLON.Engine.TEXTUREFORMAT_RGBA)
  19. */
  20. constructor(name: string, options: any, scene: Nullable<Scene> = null, generateMipMaps: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, format: number = Engine.TEXTUREFORMAT_RGBA) {
  21. super(null, scene, !generateMipMaps, undefined, samplingMode, undefined, undefined, undefined, undefined, format);
  22. this.name = name;
  23. this._engine = (<Scene>this.getScene()).getEngine();
  24. this.wrapU = Texture.CLAMP_ADDRESSMODE;
  25. this.wrapV = Texture.CLAMP_ADDRESSMODE;
  26. this._generateMipMaps = generateMipMaps;
  27. if (options.getContext) {
  28. this._canvas = options;
  29. this._texture = this._engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
  30. } else {
  31. this._canvas = document.createElement("canvas");
  32. if (options.width) {
  33. this._texture = this._engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
  34. } else {
  35. this._texture = this._engine.createDynamicTexture(options, options, generateMipMaps, samplingMode);
  36. }
  37. }
  38. var textureSize = this.getSize();
  39. this._canvas.width = textureSize.width;
  40. this._canvas.height = textureSize.height;
  41. this._context = <CanvasRenderingContext2D>this._canvas.getContext("2d");
  42. }
  43. /**
  44. * Gets the current state of canRescale
  45. */
  46. public get canRescale(): boolean {
  47. return true;
  48. }
  49. private _recreate(textureSize: ISize): void {
  50. this._canvas.width = textureSize.width;
  51. this._canvas.height = textureSize.height;
  52. this.releaseInternalTexture();
  53. this._texture = this._engine.createDynamicTexture(textureSize.width, textureSize.height, this._generateMipMaps, this._samplingMode);
  54. }
  55. /**
  56. * Scales the texture
  57. * @param ratio the scale factor to apply to both width and height
  58. */
  59. public scale(ratio: number): void {
  60. var textureSize = this.getSize();
  61. textureSize.width *= ratio;
  62. textureSize.height *= ratio;
  63. this._recreate(textureSize);
  64. }
  65. /**
  66. * Resizes the texture
  67. * @param width the new width
  68. * @param height the new height
  69. */
  70. public scaleTo(width: number, height: number): void {
  71. var textureSize = this.getSize();
  72. textureSize.width = width;
  73. textureSize.height = height;
  74. this._recreate(textureSize);
  75. }
  76. /**
  77. * Gets the context of the canvas used by the texture
  78. * @returns the canvas context of the dynamic texture
  79. */
  80. public getContext(): CanvasRenderingContext2D {
  81. return this._context;
  82. }
  83. /**
  84. * Clears the texture
  85. */
  86. public clear(): void {
  87. var size = this.getSize();
  88. this._context.fillRect(0, 0, size.width, size.height);
  89. }
  90. /**
  91. * Updates the texture
  92. * @param invertY defines the direction for the Y axis (default is true - y increases downwards)
  93. */
  94. public update(invertY?: boolean): void {
  95. this._engine.updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY, undefined, this._format || undefined);
  96. }
  97. /**
  98. * Draws text onto the texture
  99. * @param text defines the text to be drawn
  100. * @param x defines the placement of the text from the left
  101. * @param y defines the placement of the text from the top when invertY is true and from the bottom when false
  102. * @param font defines the font to be used with font-style, font-size, font-name
  103. * @param color defines the color used for the text
  104. * @param clearColor defines the color for the canvas, use null to not overwrite canvas
  105. * @param invertY defines the direction for the Y axis (default is true - y increases downwards)
  106. * @param update defines whether texture is immediately update (default is true)
  107. */
  108. public drawText(text: string, x: number, y: number, font: string, color: string, clearColor: string, invertY?: boolean, update = true) {
  109. var size = this.getSize();
  110. if (clearColor) {
  111. this._context.fillStyle = clearColor;
  112. this._context.fillRect(0, 0, size.width, size.height);
  113. }
  114. this._context.font = font;
  115. if (x === null || x === undefined) {
  116. var textSize = this._context.measureText(text);
  117. x = (size.width - textSize.width) / 2;
  118. }
  119. if (y === null || y === undefined) {
  120. var fontSize = parseInt((font.replace(/\D/g, '')));;
  121. y = (size.height / 2) + (fontSize / 3.65);
  122. }
  123. this._context.fillStyle = color;
  124. this._context.fillText(text, x, y);
  125. if (update) {
  126. this.update(invertY);
  127. }
  128. }
  129. /**
  130. * Clones the texture
  131. * @returns the clone of the texture.
  132. */
  133. public clone(): DynamicTexture {
  134. let scene = this.getScene();
  135. if (!scene) {
  136. return this;
  137. }
  138. var textureSize = this.getSize();
  139. var newTexture = new DynamicTexture(this.name, textureSize, scene, this._generateMipMaps);
  140. // Base texture
  141. newTexture.hasAlpha = this.hasAlpha;
  142. newTexture.level = this.level;
  143. // Dynamic Texture
  144. newTexture.wrapU = this.wrapU;
  145. newTexture.wrapV = this.wrapV;
  146. return newTexture;
  147. }
  148. /** @ignore */
  149. public _rebuild(): void {
  150. this.update();
  151. }
  152. }
  153. }