Skip to content

Instantly share code, notes, and snippets.

@shreyanshp-cactus
Created May 12, 2025 06:29
Show Gist options
  • Save shreyanshp-cactus/bb93218444fb1d0c913e80626b965d5a to your computer and use it in GitHub Desktop.
Save shreyanshp-cactus/bb93218444fb1d0c913e80626b965d5a to your computer and use it in GitHub Desktop.
wrap canvas context methods
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