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 | shadow: 'Shadow'
|
21 | };
|
22 |
|
23 | class Renderer extends EventEmitter {
|
24 | constructor(myCtx) {
|
25 | super();
|
26 | const self = this;
|
27 | self.ctx = myCtx;
|
28 | self.style = {};
|
29 | self._initContext(myCtx);
|
30 | }
|
31 |
|
32 | getContext(type) {
|
33 | if (type === '2d') {
|
34 | return this.ctx;
|
35 | }
|
36 | }
|
37 |
|
38 | _initContext(myCtx) {
|
39 | Object.keys(CAPITALIZED_ATTRS_MAP).map(key => {
|
40 | Object.defineProperty(myCtx, key, {
|
41 | set(value) {
|
42 |
|
43 | if (key === 'shadow' && myCtx.setShadow && Array.isArray(value)) {
|
44 | myCtx.setShadow(value[0], value[1], value[2], value[3]);
|
45 | } else {
|
46 | const name = 'set' + CAPITALIZED_ATTRS_MAP[key];
|
47 | myCtx[name](value);
|
48 | }
|
49 | }
|
50 | });
|
51 | return key;
|
52 | });
|
53 | }
|
54 | }
|
55 |
|
56 | module.exports = Renderer;
|