Skip to content

Instantly share code, notes, and snippets.

@greggman
Created February 11, 2015 08:15
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/b86b2e6e2cd45d8d2859 to your computer and use it in GitHub Desktop.
Save greggman/b86b2e6e2cd45d8d2859 to your computer and use it in GitHub Desktop.
Quick Greasemonkey Hack to see if this was the only issue for Firefox in a "My God, it's Full of Sinus" (http://awsm.de/)
// ==UserScript==
// @name CanvasWrap
// @namespace Greggman
// @description Wraps the Canvas
// @version 1
// @grant none
// ==/UserScript==
CanvasRenderingContext2D.prototype.drawImage = (function(oldFn) {
return function() {
var args = Array.prototype.slice.call(arguments);
if (args.length == 9) {
var img = args[0];
var sx = args[1];
var sy = args[2];
var sw = args[3];
var sh = args[4];
var dx = args[5];
var dy = args[6];
var dw = args[7];
var dh = args[8];
if (sw <= 0 || dw <= 0 || sh <= 0 || dh <= 0) {
return;
}
dx = dx || 0;
dy = dy || 0;
var srcToDstWidthFactor = dw / sw;
var srcToDstHeightFactor = dh / sh;
// clip left
if (sx < 0) {
sw += sx;
dw += sx * srcToDstWidthFactor;
dx -= sx * srcToDstWidthFactor;
sx = 0;
}
// clip right
if (sx + sw > img.width) {
var delta = sx + sw - img.width;
sw -= delta;
dw -= delta * srcToDstWidthFactor;
}
// clip top
if (sy < 0) {
sh += sy;
dw += sy * srcToDstHeightFactor;
dy -= sy * srcToDstHeightFactor;
sy = 0;
}
// clip bottom
if (sy + sh > img.height) {
var delta = sy + sh - img.height;
sh -= delta;
dh -= delta * srcToDstWidthFactor;
}
// NOTE: It's not clear if I should adjust dw,dh or not. I have no idea
// what is intended
if (sw > 0 && sh > 0) {
oldFn.call(this, img, sx, sy, sw, sh, dx, dy, dw, dh);
}
} else {
oldFn.apply(this, args);
}
};
}(CanvasRenderingContext2D.prototype.drawImage))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment