Skip to content

Instantly share code, notes, and snippets.

@pizthewiz
Created May 25, 2012 17:53
Show Gist options
  • Save pizthewiz/2789482 to your computer and use it in GitHub Desktop.
Save pizthewiz/2789482 to your computer and use it in GitHub Desktop.
workaround iOS 4.3 bug in slicing variant of canvas's drawImage when scaling
function _equalRects(left, right) {
return left.origin.x === right.origin.x && left.origin.y === right.origin.y && left.size.width === right.size.width && left.size.height === right.size.height;
}
function _drawImage(context, image, sourceFrame, destinationFrame) {
// WORKAROUND - iOS 4.3 bug with the slicing variant of drawImage when scaling
if (!_equalRects(sourceFrame, destinationFrame)) {
context.save();
// clip
context.beginPath();
context.rect(destinationFrame.origin.x, destinationFrame.origin.y, destinationFrame.size.width, destinationFrame.size.height);
context.clip();
var scale = destinationFrame.size.width / sourceFrame.size.width;
// translate
context.translate(destinationFrame.origin.x - sourceFrame.origin.x*scale, destinationFrame.origin.y - sourceFrame.origin.y*scale);
// scale
context.scale(scale, scale);
// draw
context.drawImage(image, 0.0, 0.0);
context.restore();
} else {
context.drawImage(image, sourceFrame.origin.x, sourceFrame.origin.y, sourceFrame.size.width, sourceFrame.size.height, destinationFrame.origin.x, destinationFrame.origin.y, destinationFrame.size.width, destinationFrame.size.height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment