babylon.dynamicTexture.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = BABYLON.Texture.CLAMP_ADDRESSMODE;
  10. this.wrapV = BABYLON.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 update(invertY?: boolean): void {
  44. this.getScene().getEngine().updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY);
  45. }
  46. public drawText(text: string, x: number, y: number, font: string, color: string, clearColor: string, invertY?: boolean) {
  47. var size = this.getSize();
  48. if (clearColor) {
  49. this._context.fillStyle = clearColor;
  50. this._context.fillRect(0, 0, size.width, size.height);
  51. }
  52. this._context.font = font;
  53. if (x === null) {
  54. var textSize = this._context.measureText(text);
  55. x = (size.width - textSize.width) / 2;
  56. }
  57. this._context.fillStyle = color;
  58. this._context.fillText(text, x, y);
  59. this.update(invertY);
  60. }
  61. public clone(): DynamicTexture {
  62. var textureSize = this.getSize();
  63. var newTexture = new BABYLON.DynamicTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  64. // Base texture
  65. newTexture.hasAlpha = this.hasAlpha;
  66. newTexture.level = this.level;
  67. // Dynamic Texture
  68. newTexture.wrapU = this.wrapU;
  69. newTexture.wrapV = this.wrapV;
  70. return newTexture;
  71. }
  72. }
  73. }