babylon.dynamicTexture.js 2.7 KB

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