ios 6或7大于2mb图片渲染到canvas压扁了

https://www.bountysource.com/issues/1335840-large-images-add-to-canvas-squashed-in-ios

jaydenseric opened this issue on Nov 26, 2013

Large images are added to the canvas dramatically squashed on Safari iOS (6 or 7). This is persistent when manipulating the image with Fabric.

See this image of a landscape scene:
ipad-fabric-large-image-issue

This is a known iOS Safari canvas issue with a programatic fix: See Stack Overflow post.


There is a $50 open bounty on this issue. Add to the bounty at Bountysource.

View original issue »

Comments 6

ArendPijls on Jan 20, 2014

Can this be fixed for fabric js?

Johannes Gottschalk on Jan 31, 2014

this issue is related to the limitations of mobile safari and can't really be fixed in fabric. What you need are smaller images or a dirty workaround.

You can prevent this behavior on all devices by using images smaller than 3 megapixel. Devices with more than 256mb ram you can use images with 5MP

FYI:https://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariWebContent/CreatingContentforSafarioniPhone/CreatingContentforSafarioniPhone.html

Juriy Zaytsev on Feb 2, 2014

@jaydenseric can you attach an image that's squished (for testing)

jaydenseric on Feb 2, 2014

I don't have access to the original project anymore. I think I was able to find the same image I used in the screenshot. It is a wallpaper from the internet, so I don't know the licensing implications of putting it here but here goes...

landscape-wallpaper

alisspers on Apr 30, 2014

Has anyone successfully managed to use any of the linked fixes with fabric?

lavrton on Apr 30, 2014

Hi guys. I am KineticJS contributor (but also like Fabric a lot). You may use thishttp://stackoverflow.com/questions/11929099/html5-canvas-drawimage-ratio-bug-ios workaround to solve the issue. But inserting this workaround inside library might be bad idea because of possible side effects. So while discovering the problem I found that the best way might be replacing image element with fixed canvas element. Here is the code:

// Workaround for vertically squashed images in iOS6 & iOS7
// general idea is using canvas element instead of image

// detect scale ratio for image
function detectVerticalSquash(img) {
  var ih = img.height;
  var canvas = document.createElement('canvas');
  canvas.width = 1;
  canvas.height = ih;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);
  var data = ctx.getImageData(0, 0, 1, ih).data;
  // search image edge pixel position in case it is squashed vertically.
  var sy = 0;
  var ey = ih;
  var py = ih;
  while (py > sy) {
      var alpha = data[(py - 1) * 4 + 3];
      if (alpha === 0) {
          ey = py;
      } else {
          sy = py;
      }
      py = (ey + sy) >> 1;
  }
  var ratio = (py / ih);
  return (ratio===0)?1:ratio;
}


// create canvas with correct representation to replace with image
function generateCanvas(image){
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    canvas.width = image.width;
    canvas.height = image.height;
    var vertSquashRatio = detectVerticalSquash(image);
    context.drawImage(image, 0, 0, 
                       image.width, image.height / vertSquashRatio);
    return(canvas);
}



var img = new Image();

img.onload = function() {
  var canvas = new fabric.Canvas('con');
  var imgInstance = new fabric.Image(generateCanvas(img), {
    left: 10,
    top: 10,
    angle: 30,
    opacity: 0.85
  });
 canvas.add(imgInstance);
  canvas.renderAll();

};
img.src = sourseOfHUGEimage;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值