I'm facing a really bizarre issue when trying to scale and crop an image using the HTML5 canvas API across browsers.
Specifically, I'm working with trying to find the center of an image and crop a square based on its width and height using these parameters as specified by the context API
context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);
Here's an example of the code I'm using:
<!DOCTYPE html>
<html>
<body>
<p>Image to use:</p>
<img id="scream" width="220" height="277" src="img_the_scream.jpg" alt="The Scream">
<p>Canvas:</p>
<canvas id="myCanvas" width="150" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 0, 25, 220, 277, 0, 0, 150, 188);
}
</script>
</body>
</html>
You can try the demo here: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_canvas_drawimage2
The code does not display anything on the canvas in Safari, however it correctly scales and crops the images in Chrome.
I would upload images but stackoverflow has an arbitrary rule that doesn't allow me to post images until I have a higher reputation!
Any thoughts here would be appreciated!