babylon.dynamicTexture.js 4.2 KB

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