babylon.dynamicTexture.ts 4.1 KB

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