babylon.dynamicTexture.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.DynamicTexture = function (name, options, scene, generateMipMaps) {
  5. this._scene = scene;
  6. this._scene.textures.push(this);
  7. this.name = name;
  8. this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  9. this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  10. this._generateMipMaps = generateMipMaps;
  11. if (options.getContext) {
  12. this._canvas = options;
  13. this._texture = scene.getEngine().createDynamicTexture(options.width, options.height, generateMipMaps);
  14. } else {
  15. this._canvas = document.createElement("canvas");
  16. if (options.width) {
  17. this._texture = scene.getEngine().createDynamicTexture(options.width, options.height, generateMipMaps);
  18. } else {
  19. this._texture = scene.getEngine().createDynamicTexture(options, options, generateMipMaps);
  20. }
  21. }
  22. var textureSize = this.getSize();
  23. this._canvas.width = textureSize.width;
  24. this._canvas.height = textureSize.height;
  25. this._context = this._canvas.getContext("2d");
  26. };
  27. BABYLON.DynamicTexture.prototype = Object.create(BABYLON.Texture.prototype);
  28. // Methods
  29. BABYLON.DynamicTexture.prototype.getContext = function () {
  30. return this._context;
  31. };
  32. BABYLON.DynamicTexture.prototype.update = function (invertY) {
  33. this._scene.getEngine().updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY);
  34. };
  35. BABYLON.DynamicTexture.prototype.drawText = function (text, x, y, font, color, clearColor, invertY) {
  36. var size = this.getSize();
  37. if (clearColor) {
  38. this._context.fillStyle = clearColor;
  39. this._context.fillRect(0, 0, size.width, size.height);
  40. }
  41. this._context.font = font;
  42. if (x === null) {
  43. var textSize = this._context.measureText(text);
  44. x = (size.width - textSize.width) / 2;
  45. }
  46. this._context.fillStyle = color;
  47. this._context.fillText(text, x, y);
  48. this.update(invertY);
  49. };
  50. BABYLON.DynamicTexture.prototype.clone = function () {
  51. var textureSize = this.getSize();
  52. var newTexture = new BABYLON.DynamicTexture(this.name, textureSize.width, this._scene, this._generateMipMaps);
  53. // Base texture
  54. newTexture.hasAlpha = this.hasAlpha;
  55. newTexture.level = this.level;
  56. // Dynamic Texture
  57. newTexture.wrapU = this.wrapU;
  58. newTexture.wrapV = this.wrapV;
  59. return newTexture;
  60. };
  61. })();