renderer.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import EventEmitter from './EventEmitter.min.js'
  2. const CAPITALIZED_ATTRS_MAP = {
  3. fontSize: 'FontSize',
  4. opacity: 'GlobalAlpha',
  5. lineDash: 'LineDash',
  6. textAlign: 'TextAlign',
  7. };
  8. /**
  9. * wxapp textAlign align 可选值为 left|center|right
  10. * 标准canvas textAlign align 可选值为 left|center|right|start|end
  11. */
  12. const TEXT_ALIGN_MAP = {
  13. 'start': 'left',
  14. 'end': 'right',
  15. };
  16. export default class Renderer extends EventEmitter {
  17. constructor(wxCtx) {
  18. super();
  19. const self = this;
  20. self.ctx = wxCtx;
  21. self.style = {}; // just mock
  22. self._initContext(wxCtx);
  23. }
  24. getContext(type) {
  25. if (type === '2d') {
  26. return this.ctx;
  27. }
  28. }
  29. _initContext(wxCtx) {
  30. Object.keys(CAPITALIZED_ATTRS_MAP).map(style => {
  31. Object.defineProperty(wxCtx, style, {
  32. set: value => {
  33. if (style == "textAlign") {
  34. value = TEXT_ALIGN_MAP[value] ? TEXT_ALIGN_MAP[value] : value;
  35. }
  36. const name = 'set' + CAPITALIZED_ATTRS_MAP[style];
  37. wxCtx[name](value);
  38. }
  39. });
  40. });
  41. }
  42. }