babylon.dynamicTexture.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /// <reference path="babylon.texture.ts" />
  2. module BABYLON {
  3. export class DynamicTexture extends Texture {
  4. private _generateMipMaps: boolean;
  5. private _canvas: HTMLCanvasElement;
  6. private _context: CanvasRenderingContext2D;
  7. private _engine: Engine;
  8. constructor(name: string, options: any, scene: Scene, generateMipMaps: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, format: number = Engine.TEXTUREFORMAT_RGBA) {
  9. super(null, scene, !generateMipMaps, undefined, samplingMode, undefined, undefined, undefined, undefined, format);
  10. this.name = name;
  11. this._engine = (<Scene>this.getScene()).getEngine();
  12. this.wrapU = Texture.CLAMP_ADDRESSMODE;
  13. this.wrapV = Texture.CLAMP_ADDRESSMODE;
  14. this._generateMipMaps = generateMipMaps;
  15. if (options.getContext) {
  16. this._canvas = options;
  17. this._texture = this._engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
  18. } else {
  19. this._canvas = document.createElement("canvas");
  20. if (options.width) {
  21. this._texture = this._engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
  22. } else {
  23. this._texture = this._engine.createDynamicTexture(options, options, generateMipMaps, samplingMode);
  24. }
  25. }
  26. var textureSize = this.getSize();
  27. this._canvas.width = textureSize.width;
  28. this._canvas.height = textureSize.height;
  29. this._context = <CanvasRenderingContext2D>this._canvas.getContext("2d");
  30. }
  31. public get canRescale(): boolean {
  32. return true;
  33. }
  34. private _recreate(textureSize: ISize): void {
  35. this._canvas.width = textureSize.width;
  36. this._canvas.height = textureSize.height;
  37. this.releaseInternalTexture();
  38. this._texture = this._engine.createDynamicTexture(textureSize.width, textureSize.height, this._generateMipMaps, this._samplingMode);
  39. }
  40. public scale(ratio: number): void {
  41. var textureSize = this.getSize();
  42. textureSize.width *= ratio;
  43. textureSize.height *= ratio;
  44. this._recreate(textureSize);
  45. }
  46. public scaleTo(width: number, height: number): void {
  47. var textureSize = this.getSize();
  48. textureSize.width = width;
  49. textureSize.height = height;
  50. this._recreate(textureSize);
  51. }
  52. public getContext(): CanvasRenderingContext2D {
  53. return this._context;
  54. }
  55. public clear(): void {
  56. var size = this.getSize();
  57. this._context.fillRect(0, 0, size.width, size.height);
  58. }
  59. public update(invertY?: boolean): void {
  60. this._engine.updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY, undefined, this._format || undefined);
  61. }
  62. public drawText(text: string, x: number, y: number, font: string, color: string, clearColor: string, invertY?: boolean, update = true) {
  63. var size = this.getSize();
  64. if (clearColor) {
  65. this._context.fillStyle = clearColor;
  66. this._context.fillRect(0, 0, size.width, size.height);
  67. }
  68. this._context.font = font;
  69. if (x === null || x === undefined) {
  70. var textSize = this._context.measureText(text);
  71. x = (size.width - textSize.width) / 2;
  72. }
  73. if (y === null || y === undefined) {
  74. var fontSize = parseInt((font.replace(/\D/g,'')));;
  75. y = (size.height /2) + (fontSize/3.65);
  76. }
  77. this._context.fillStyle = color;
  78. this._context.fillText(text, x, y);
  79. if (update) {
  80. this.update(invertY);
  81. }
  82. }
  83. public clone(): DynamicTexture {
  84. let scene = this.getScene();
  85. if (!scene) {
  86. return this;
  87. }
  88. var textureSize = this.getSize();
  89. var newTexture = new DynamicTexture(this.name, textureSize, scene, this._generateMipMaps);
  90. // Base texture
  91. newTexture.hasAlpha = this.hasAlpha;
  92. newTexture.level = this.level;
  93. // Dynamic Texture
  94. newTexture.wrapU = this.wrapU;
  95. newTexture.wrapV = this.wrapV;
  96. return newTexture;
  97. }
  98. public _rebuild(): void {
  99. this.update();
  100. }
  101. }
  102. }