UNPKG

1.79 kBJavaScriptView Raw
1function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
2
3function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
4
5/**
6 * f2 专为适配微信小程序绘图上下文 context 而封装的伪 Canvas
7 * @authors (sima.zhang1990@gmail.com)
8 * @version 1.0.0
9 */
10var EventEmitter = require('wolfy87-eventemitter');
11
12var CAPITALIZED_ATTRS_MAP = {
13 fillStyle: 'FillStyle',
14 fontSize: 'FontSize',
15 globalAlpha: 'GlobalAlpha',
16 opacity: 'GlobalAlpha',
17 lineCap: 'LineCap',
18 lineJoin: 'LineJoin',
19 lineWidth: 'LineWidth',
20 miterLimit: 'MiterLimit',
21 strokeStyle: 'StrokeStyle',
22 textAlign: 'TextAlign',
23 textBaseline: 'TextBaseline'
24};
25
26var Renderer =
27/*#__PURE__*/
28function (_EventEmitter) {
29 _inheritsLoose(Renderer, _EventEmitter);
30
31 function Renderer(myCtx) {
32 var _this;
33
34 _this = _EventEmitter.call(this) || this;
35
36 var self = _assertThisInitialized(_assertThisInitialized(_this));
37
38 self.ctx = myCtx;
39 self.style = {}; // just mock
40
41 self._initContext(myCtx);
42
43 return _this;
44 }
45
46 var _proto = Renderer.prototype;
47
48 _proto.getContext = function getContext(type) {
49 if (type === '2d') {
50 return this.ctx;
51 }
52 };
53
54 _proto._initContext = function _initContext(myCtx) {
55 Object.keys(CAPITALIZED_ATTRS_MAP).map(function (key) {
56 Object.defineProperty(myCtx, key, {
57 set: function set(value) {
58 var name = 'set' + CAPITALIZED_ATTRS_MAP[key];
59 myCtx[name](value);
60 }
61 });
62 return key;
63 });
64 };
65
66 return Renderer;
67}(EventEmitter);
68
69module.exports = Renderer;
\No newline at end of file