javascript - canvas context.drawImage differences across browsers? -


i'm facing bizarre issue when trying scale , crop image using html5 canvas api across browsers.

specifically, i'm working trying find center of image , crop square based on width , height using these parameters specified context api

context.drawimage(img,sx,sy,swidth,sheight,x,y,width,height); 

here's example of code i'm using:

<!doctype html> <html> <body>  <p>image 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;"> browser not support 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 demo here: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_canvas_drawimage2

the code not display on canvas in safari, correctly scales , crops images in chrome.

i upload images stackoverflow has arbitrary rule doesn't allow me post images until have higher reputation!

any thoughts here appreciated!

the problem in safari sensitivity source size, region specify source versus available bitmap.

for example, source in example says:

//        source:  x, y , w  , h ctx.drawimage(img, 0, 25, 220, 277, ... 

this mean bitmap has 25+277 pixels in height of course not case input image 277 pixels in height.

try adjusting source region inside image, in case reducing height y offset:

ctx.drawimage(img, 0, 25, 220, 277-25,   0, 0, 150, 188); 

to crop make sure source region inside image, f.ex. if want crop 10 pixels in sides:

var iw = img.naturalwidth; var ih = img.naturalheight; var crop = 10;  ctx.drawimage(img, crop, crop, iw - crop*2, ih - crop*2, 0, 0, 150, 188); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -