Skip to content

Instantly share code, notes, and snippets.

@bernardofd
Last active August 29, 2015 14:20
Show Gist options
  • Save bernardofd/5104fa62434b3009868f to your computer and use it in GitHub Desktop.
Save bernardofd/5104fa62434b3009868f to your computer and use it in GitHub Desktop.
Isolated test case for canvas negative scaling of images, aiming to pin the issue to excanvas in IE8.
<!DOCTYPE html>
<html>
<head>
<title>Canvas - Image Negative Scaling</title>
<!--[if IE]>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/excanvas.min.js"></script>
<![endif]-->
</head>
<body>
<button id="action">Negative Scaling</button>
<canvas id="canvas" width="400" height="300"></canvas>
<script type="text/javascript">
(function($window) {
$window.onload = function() {
var ctx = canvas.getContext('2d'),
imageUrl = 'http://fabricjs.com/assets/pug.jpg',
image = new Image(),
button = document.getElementById('action'),
flipped = false;
image.onload = function() {
ctx.drawImage(this, 200, 0, 180, 300);
}
image.src = imageUrl;
button.onclick = function() {
flipped = !flipped;
ctx.clearRect(0,0,400,300);
ctx.save();
ctx.scale(flipped? -1 : 1,1);
ctx.drawImage(image, flipped? -200 : 200, 0, 180, 300);
ctx.restore();
};
}
})(window);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Canvas - Image Negative Scaling</title>
<!--[if IE]>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/excanvas.min.js"></script>
<![endif]-->
</head>
<body>
<button id="action">Rotate</button>
<canvas id="canvas" width="400" height="400"></canvas>
<script type="text/javascript">
(function($window) {
$window.onload = function() {
var ctx = canvas.getContext('2d'),
imageUrl = 'http://fabricjs.com/assets/pug.jpg',
image = new Image(),
button = document.getElementById('action'),
flipped = false;
image.onload = function() {
ctx.drawImage(this, 50, 100, 300, 200);
}
image.src = imageUrl;
button.onclick = function() {
flipped = !flipped;
ctx.clearRect(0,0,400,400);
ctx.save();
ctx.translate(200,200);
ctx.rotate(flipped? Math.PI/4 : 0);
ctx.translate(-150,-100);
ctx.drawImage(image, 0, 0, 300, 200);
ctx.restore();
};
}
})(window);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment