Canvas.toDataURL不能在移动Safari上使用iOS? [英] Canvas.toDataURL not working on mobile Safari iOS?

查看:5372
本文介绍了Canvas.toDataURL不能在移动Safari上使用iOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过以下。我从svg图像创建了一个< img> 。然后我在画布上绘制它,最后我导出为PNG,并将结果PNG设置为新的< img> 。它适用于Android,Chrome,Safari,FireFox。但是, canvas.toDataUrl()在iOS上的移动Safari上不工作。它只有在你不在画布上使用SVG图像时才能工作。

I tried the following. I created an <img> from an svg image. Then I draw it on a canvas and finally I exported it as PNG and set the resulting PNG as a new <img>. It works well on Android, Chrome, Safari, FireFox. But, canvas.toDataUrl() is not working on mobile Safari on iOS. It is only working when you don't use SVG images on the canvas.

这里是我用于测试的代码:

Here is the code I use for testing:

<div id="mydiv"></div>
<img id="image2">



var mydiv  = document.getElementById('mydiv'),
    image2 = document.getElementById('image2');

image2.crossOrigin="anonymous";

var image3 = new Image();
mydiv.appendChild(image3);
image3.onload = function() {
  var canvas = document.createElement('canvas'),
  ctx = canvas.getContext('2d');

  canvas.width = image3.width;
  canvas.height = image3.height;

  ctx.drawImage(image3,0,0, canvas.width, canvas.height);
  var dataUrl = canvas.toDataURL('image/png');
  image2.src = dataUrl;
}
image3.crossOrigin="anonymous";
image3.src = "https://dl.dropboxusercontent.com/u/47067729/sticker4.svg";

我在这里创建了一个JSFiddle: http://jsfiddle.net/confile/ZqJYG/

I created a JSFiddle here: http://jsfiddle.net/confile/ZqJYG/

问题仅发生在您在iOS上运行时。

The problem occurs only when you run it on iOS. It does not run on mobile Safari and not on mobile Chrome.

此问题是否有解决方法?

推荐答案

我认为您的浏览器可能不支持它。请参阅以下内容。

I think your browser may not support it . see the following.

http://caniuse.com/#search=canvas

iPhone 3GS        - Mobile Safari 4.0.5
iPhone 4          - Mobile Safari 4.0.5
iPhone 4s         - Mobile Safari 5.1
iPad 1 / 3.2.2    - Mobile Safari 4.0.4
iPad 2 / 4.3.3    - Mobile Safari 5.02
iPad 2 / 5.0      - Mobile Safari 5.1
iPad 3 / 5.1      - Mobile Safari 5.1
iPhone 5 / 6.0    - Mobile Safari 6.0
iPad 4 / 6.0      - Mobile Safari 6.0

您也可以使用以下代码测试浏览器支持:

You can also test your browser support by using the following code:

function supportsToDataURL()
{
    var c = document.createElement("canvas");
    var data = c.toDataURL("image/png");
    return (data.indexOf("data:image/png") == 0);
}

希望这有帮助。

这篇关于Canvas.toDataURL不能在移动Safari上使用iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆