Skip to content

Instantly share code, notes, and snippets.

@greggman
Created November 8, 2022 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/0cc10a63046769ea1b35b2e3e0d57267 to your computer and use it in GitHub Desktop.
Save greggman/0cc10a63046769ea1b35b2e3e0d57267 to your computer and use it in GitHub Desktop.
Proxy an HTMLCanvasElement
/*bug-in-github-api-content-can-not-be-empty*/
/*bug-in-github-api-content-can-not-be-empty*/
const canvas = document.createElement('canvas');
const handler = {
get(target, prop, receiver) {
return target[prop];
},
set(target, prop, value) {
console.log('set:', prop, '=', value);
target[prop] = value;
}
};
// Make the proxy
const canvasProxy = new Proxy(canvas, handler);
// test the proxy
canvasProxy.width = 512;
canvasProxy.height = 128;
// use the proxy
const ctx = document.createElement('canvas').getContext('2d');
ctx.drawImage(canvasProxy, 0, 0);
{"name":"Proxy an HTMLCanvasElement","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment