Created
May 12, 2025 06:29
-
-
Save shreyanshp-cactus/bb93218444fb1d0c913e80626b965d5a to your computer and use it in GitHub Desktop.
wrap canvas context methods
This file contains hidden or 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.getElementById('myCanvas'); | |
const ctx = canvas.getContext('2d'); | |
// Save the original method | |
const originalFillRect = ctx.fillRect; | |
// Override it | |
ctx.fillRect = function(x, y, width, height) { | |
console.log("Called fillRect with:", x, y, width, height); | |
// Call the original method so the canvas still works | |
return originalFillRect.call(this, x, y, width, height); | |
}; | |
// Now when you draw: | |
ctx.fillRect(10, 10, 100, 50); // This logs AND draws |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment