RectText.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var textHelper = require("../helper/text");
  2. var BoundingRect = require("../../core/BoundingRect");
  3. /**
  4. * Mixin for drawing text in a element bounding rect
  5. * @module zrender/mixin/RectText
  6. */
  7. var tmpRect = new BoundingRect();
  8. var RectText = function () {};
  9. RectText.prototype = {
  10. constructor: RectText,
  11. /**
  12. * Draw text in a rect with specified position.
  13. * @param {CanvasRenderingContext2D} ctx
  14. * @param {Object} rect Displayable rect
  15. */
  16. drawRectText: function (ctx, rect) {
  17. var style = this.style;
  18. rect = style.textRect || rect; // Optimize, avoid normalize every time.
  19. this.__dirty && textHelper.normalizeTextStyle(style, true);
  20. var text = style.text; // Convert to string
  21. text != null && (text += '');
  22. if (!textHelper.needDrawText(text, style)) {
  23. return;
  24. } // FIXME
  25. ctx.save(); // Transform rect to view space
  26. var transform = this.transform;
  27. if (!style.transformText) {
  28. if (transform) {
  29. tmpRect.copy(rect);
  30. tmpRect.applyTransform(transform);
  31. rect = tmpRect;
  32. }
  33. } else {
  34. this.setTransform(ctx);
  35. } // transformText and textRotation can not be used at the same time.
  36. textHelper.renderText(this, ctx, text, style, rect);
  37. ctx.restore();
  38. }
  39. };
  40. var _default = RectText;
  41. module.exports = _default;