我希望代理绘画API以便测试抽象方法是否确实绘制到画布上,但是在代理后,我遇到了错误:
'strokeStyle' setter called on an object that does not implement interface CanvasRenderingContext2D
这段代码被简化了,但会抛出同样的错误:
有什么想法吗?
这段代码被简化了,但会抛出同样的错误:
/** !NB: This snippet will probably only run in Firefox */
var canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 100;
canvas.style.backgroundColor = '#FF0000';
var ctx = canvas.getContext("2d");
var calls = [];
var handler = {
get( target, property, receiver ) {
if ( typeof ctx[property] === 'function' ){
return function( ...args ){
calls.push( { call: property, args: args } )
return ctx[property]( ...args );
};
}
return ctx[property];
}
};
try {
document.body.appendChild(canvas);
var proxy = new Proxy( ctx, handler );
proxy.scale( 1, 1 );
proxy.strokeStyle = '#000000';
canvas.getContext = function(){
return proxy;
};
}
catch( e ) {
document.getElementById('message').innerHTML = 'Error: ' + e.message;
}
<div id="message"></div>
ctx.strokeStyle = colour
,其中colour是未定义的。我看到的错误没有提供帮助,它指向了代理对象:TypeError: proxy set handler returned false for property '"strokeStyle"'
。 - thelastshadow