Created
November 8, 2022 06:50
-
-
Save greggman/0cc10a63046769ea1b35b2e3e0d57267 to your computer and use it in GitHub Desktop.
Proxy an HTMLCanvasElement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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