feature: add native context types

This commit is contained in:
gloryyin 2022-10-08 02:50:32 +08:00
parent c7857d7e2b
commit 4e89618a78
2 changed files with 10 additions and 5 deletions

View File

@ -86,7 +86,7 @@ var CONTEXT_PROPERTIES = [
'globalAlpha', 'globalAlpha',
'globalCompositeOperation', 'globalCompositeOperation',
'imageSmoothingEnabled', 'imageSmoothingEnabled',
]; ] as const;
const traceArrMax = 100; const traceArrMax = 100;
/** /**
@ -701,6 +701,11 @@ export class Context {
} }
} }
// supported context properties
type CanvasContextProps = Pick<CanvasRenderingContext2D, typeof CONTEXT_PROPERTIES[number]>;
export interface Context extends CanvasContextProps {};
CONTEXT_PROPERTIES.forEach(function (prop) { CONTEXT_PROPERTIES.forEach(function (prop) {
Object.defineProperty(Context.prototype, prop, { Object.defineProperty(Context.prototype, prop, {
get() { get() {

View File

@ -57,7 +57,7 @@ describe('Context', function () {
'textBaseline', 'textBaseline',
'globalAlpha', 'globalAlpha',
'globalCompositeOperation', 'globalCompositeOperation',
]; ] as const;
it('context wrapper should work like native context', function () { it('context wrapper should work like native context', function () {
var stage = addStage(); var stage = addStage();
@ -108,10 +108,10 @@ describe('Context', function () {
// test get // test get
nativeContext.fillStyle = '#ff0000'; nativeContext.fillStyle = '#ff0000';
assert.equal(context['fillStyle'], '#ff0000'); assert.equal(context.fillStyle, '#ff0000');
// test set // test set
context['globalAlpha'] = 0.5; context.globalAlpha = 0.5;
assert.equal(context['globalAlpha'], 0.5); assert.equal(context.globalAlpha, 0.5);
}); });
}); });