babylon.dynamicTexture.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var DynamicTexture = (function (_super) {
  9. __extends(DynamicTexture, _super);
  10. function DynamicTexture(name, options, scene, generateMipMaps, samplingMode) {
  11. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  12. _super.call(this, null, scene, !generateMipMaps);
  13. this.name = name;
  14. this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  15. this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  16. this._generateMipMaps = generateMipMaps;
  17. if (options.getContext) {
  18. this._canvas = options;
  19. this._texture = scene.getEngine().createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
  20. }
  21. else {
  22. this._canvas = document.createElement("canvas");
  23. if (options.width) {
  24. this._texture = scene.getEngine().createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
  25. }
  26. else {
  27. this._texture = scene.getEngine().createDynamicTexture(options, options, generateMipMaps, samplingMode);
  28. }
  29. }
  30. var textureSize = this.getSize();
  31. this._canvas.width = textureSize.width;
  32. this._canvas.height = textureSize.height;
  33. this._context = this._canvas.getContext("2d");
  34. }
  35. Object.defineProperty(DynamicTexture.prototype, "canRescale", {
  36. get: function () {
  37. return true;
  38. },
  39. enumerable: true,
  40. configurable: true
  41. });
  42. DynamicTexture.prototype.scale = function (ratio) {
  43. var textureSize = this.getSize();
  44. textureSize.width *= ratio;
  45. textureSize.height *= ratio;
  46. this._canvas.width = textureSize.width;
  47. this._canvas.height = textureSize.height;
  48. this.releaseInternalTexture();
  49. this._texture = this.getScene().getEngine().createDynamicTexture(textureSize.width, textureSize.height, this._generateMipMaps, this._samplingMode);
  50. };
  51. DynamicTexture.prototype.getContext = function () {
  52. return this._context;
  53. };
  54. DynamicTexture.prototype.clear = function () {
  55. var size = this.getSize();
  56. this._context.fillRect(0, 0, size.width, size.height);
  57. };
  58. DynamicTexture.prototype.update = function (invertY) {
  59. this.getScene().getEngine().updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY);
  60. };
  61. DynamicTexture.prototype.drawText = function (text, x, y, font, color, clearColor, invertY, update) {
  62. if (update === void 0) { 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) {
  70. var textSize = this._context.measureText(text);
  71. x = (size.width - textSize.width) / 2;
  72. }
  73. this._context.fillStyle = color;
  74. this._context.fillText(text, x, y);
  75. if (update) {
  76. this.update(invertY);
  77. }
  78. };
  79. DynamicTexture.prototype.clone = function () {
  80. var textureSize = this.getSize();
  81. var newTexture = new DynamicTexture(this.name, textureSize, this.getScene(), this._generateMipMaps);
  82. // Base texture
  83. newTexture.hasAlpha = this.hasAlpha;
  84. newTexture.level = this.level;
  85. // Dynamic Texture
  86. newTexture.wrapU = this.wrapU;
  87. newTexture.wrapV = this.wrapV;
  88. return newTexture;
  89. };
  90. return DynamicTexture;
  91. }(BABYLON.Texture));
  92. BABYLON.DynamicTexture = DynamicTexture;
  93. })(BABYLON || (BABYLON = {}));