UNPKG

1.1 kBJavaScriptView Raw
1/**
2 * f2 专为适配微信小程序绘图上下文 context 而封装的伪 Canvas
3 * @authors (sima.zhang1990@gmail.com)
4 * @version 1.0.0
5 */
6
7const EventEmitter = require('wolfy87-eventemitter');
8const CAPITALIZED_ATTRS_MAP = {
9 fillStyle: 'FillStyle',
10 fontSize: 'FontSize',
11 globalAlpha: 'GlobalAlpha',
12 opacity: 'GlobalAlpha',
13 lineCap: 'LineCap',
14 lineJoin: 'LineJoin',
15 lineWidth: 'LineWidth',
16 miterLimit: 'MiterLimit',
17 strokeStyle: 'StrokeStyle',
18 textAlign: 'TextAlign',
19 textBaseline: 'TextBaseline'
20};
21
22class Renderer extends EventEmitter {
23 constructor(myCtx) {
24 super();
25 const self = this;
26 self.ctx = myCtx;
27 self.style = {}; // just mock
28 self._initContext(myCtx);
29 }
30
31 getContext(type) {
32 if (type === '2d') {
33 return this.ctx;
34 }
35 }
36
37 _initContext(myCtx) {
38 Object.keys(CAPITALIZED_ATTRS_MAP).map(key => {
39 Object.defineProperty(myCtx, key, {
40 set(value) {
41 const name = 'set' + CAPITALIZED_ATTRS_MAP[key];
42 myCtx[name](value);
43 }
44 });
45 return key;
46 });
47 }
48}
49
50module.exports = Renderer;