babylon.dynamicTexture.ts 3.6 KB

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