1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const EventEmitter = require('wolfy87-eventemitter');
|
8 | const 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 |
|
22 | class Renderer extends EventEmitter {
|
23 | constructor(myCtx) {
|
24 | super();
|
25 | const self = this;
|
26 | self.ctx = myCtx;
|
27 | self.style = {};
|
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 |
|
50 | module.exports = Renderer;
|