Cropping with drawImage not working in Safari

I’m working on some simple image manipulation functions with canvas.
The user uploads an image, is able to rotate and crop it and then clicks ok.
The image is then split in half with each half drawn mirrored to two canvas elements, like this:

Original

Mirrored

It all works great in Chrome, Firefox, IE and Android devices.
Safari won’t play nice though. All the image manipulation works fine except the split function. It does draw to one of the canvas elements, but the other is just black. I’ve tried changing the drawImage code around, but I just can’t get it to work.

Here’s the function:

function splitImage(canvas, context, image, isLeftSide) {
  canvas.width = img.width;
  canvas.height = img.height;
  context.save();
  if(isLeftSide) {
    context.drawImage(
      image, 
      image.width / 2,
      0, 
      image.width, 
      image.height, 
      canvas.width / 2, 
      0, 
      canvas.width, 
      canvas.height
    );
    context.scale(-1, 1);
    context.drawImage(
      image, 
      image.width / 2, 
      0, 
      image.width, 
      image.height, 
      -canvas.width / 2, 
      0, 
      canvas.width, 
      canvas.height
    );
  } else {
    context.drawImage(
      image, 
      0, 
      0, 
      image.width / 2, 
      image.height, 
      0, 
      0, 
      canvas.width / 2, 
      canvas.height
    );
    context.scale(-1, 1);
    context.drawImage(
      image, 
      0, 
      0, 
      image.width / 2, 
      image.height, 
      -canvas.width, 
      0, 
      canvas.width / 2, 
      canvas.height
    );
  }
  context.restore();
  download(canvas);
}

To be exact, it’s the drawImage operations inside the if(isLeftSide) that doesn’t work in Safari.

Any ideas?

Edit:
It doesn’t seem to work on iOS devices either.
I’ve read that Safari and iOS devices might run out of memory when working with large images.
To counteract this (and reduce some lag) I’ve added a resize function. The image is resized to a maximum of 800 px width and 800 px height if necessary, keeping the aspect ratio intact. This is done before any other image manipulation, but hasn’t made any difference.

The resize function:

function resizeImage() {
  var size = 800;
  if(imgTemp.width > size && imgTemp.width >= imgTemp.height) {
    imgTemp.height = (imgTemp.height / imgTemp.width) * size;
    imgTemp.width = size;
  } else if (imgTemp.height > size && imgTemp.height > imgTemp.width) {
    imgTemp.width = (imgTemp.width / imgTemp.height) * size;
    imgTemp.height = size;
  }
}

136 thoughts on “Cropping with drawImage not working in Safari”

  1. Pingback: free viagra
  2. Pingback: otc viagra
  3. Pingback: sildenafil generic
  4. Pingback: sildenafil 100mg
  5. Pingback: cheapest viagra
  6. Pingback: herbal cialis
  7. Pingback: cialis for men
  8. Pingback: female viagra
  9. Pingback: www.cialisgap.com
  10. Pingback: online cialis
  11. Pingback: natural viagra
  12. Pingback: viagra prices
  13. Pingback: tadalafil cost cvs
  14. Pingback: cvs viagra
  15. Pingback: viagra walmart
  16. Pingback: viagra cock
  17. Pingback: viagra kaufen
  18. Pingback: buy viagra
  19. Pingback: levitra vs viagra
  20. Pingback: buy viagra cheap
  21. Pingback: viagras
  22. Pingback: viagra tablets uk
  23. Pingback: viagra connect
  24. Pingback: women viagra
  25. Pingback: walgreens viagra
  26. Pingback: cialis wholesale

Leave a Comment