RectText.js 1.6 KB

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