babylon.dynamicTexture.js 2.2 KB

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