Skip to content

Commit 578191d

Browse files
authored
Merge pull request #1422 from yinguangyao/master
feature: add native context types
2 parents c7857d7 + 4e89618 commit 578191d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/Context.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var CONTEXT_PROPERTIES = [
8686
'globalAlpha',
8787
'globalCompositeOperation',
8888
'imageSmoothingEnabled',
89-
];
89+
] as const;
9090

9191
const traceArrMax = 100;
9292
/**
@@ -701,6 +701,11 @@ export class Context {
701701
}
702702
}
703703

704+
// supported context properties
705+
type CanvasContextProps = Pick<CanvasRenderingContext2D, typeof CONTEXT_PROPERTIES[number]>;
706+
707+
export interface Context extends CanvasContextProps {};
708+
704709
CONTEXT_PROPERTIES.forEach(function (prop) {
705710
Object.defineProperty(Context.prototype, prop, {
706711
get() {

test/unit/Context-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('Context', function () {
5757
'textBaseline',
5858
'globalAlpha',
5959
'globalCompositeOperation',
60-
];
60+
] as const;
6161

6262
it('context wrapper should work like native context', function () {
6363
var stage = addStage();
@@ -108,10 +108,10 @@ describe('Context', function () {
108108

109109
// test get
110110
nativeContext.fillStyle = '#ff0000';
111-
assert.equal(context['fillStyle'], '#ff0000');
111+
assert.equal(context.fillStyle, '#ff0000');
112112

113113
// test set
114-
context['globalAlpha'] = 0.5;
115-
assert.equal(context['globalAlpha'], 0.5);
114+
context.globalAlpha = 0.5;
115+
assert.equal(context.globalAlpha, 0.5);
116116
});
117117
});

0 commit comments

Comments
 (0)