0

我有这段代码在 Chromium 和 Firefox 中运行良好,但 Opera 不会触发加载事件,因为它无法处理图像数据。如果我将数据设置为常规 jpeg,它可以正常工作,但不适用于 svg。有什么解决办法吗?

var img = new Image();

var that = this;

img.onload = function(){ onCacheCreated.call( that, img ); }

img.src = "data:image/svg+xml;base64," 
    + btoa( ( new XMLSerializer() ).serializeToString( this._dom ) );
4

1 回答 1

0

嗯,这里展示了相当有趣的行为范围。

使它特别有趣的部分是它实际上可以加载 SVG。我尝试将其作为附加到的 base64 编码字符串和附加到"data:image/svg+xml;base64," 的纯文本"data:image/svg+xml,"

“整洁”的部分是 - onload 事件永远不会触发。但是,如果我们创建一个 img 元素并将其添加到页面中,我们可以看到它在加载时显示。但同样,我们从来没有从它那里得到一个 onload 事件。

更奇怪的是:当我使用带有透明阴影、渐变和模糊的徽标时,渐变、模糊和阴影不起作用,但仅在以纯文本形式发送时。

更奇怪的是,Chrome 显示它们的方式相同,IE9 仅显示 base64 版本,Opera 显示两者,但外观截然不同。

也许您可以设置一个间隔计时器,然后检查正在加载的 img 元素的尺寸。有人会假设它们在加载之前和之后会有所不同。我必须检查这是否是处理(从未触发的)onload 事件的可行替代方案。

这是我使用的代码:

<!DOCTYPE>
<html>
<head>
<style>
</style>
<script>
function myInit()
{
    var img = new Image();
    document.body.appendChild(img);
    var imgStr = "data:image/svg+xml;base64,";
    imgStr += "PHN2ZwogICB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIgogICB4bWxuczppbmtzY2FwZT0iaHR0cDovL3d3dy5pbmtzY2FwZS5vcmcvbmFtZXNwYWNlcy9pbmtzY2FwZSIKICAgd2lkdGg9Ijg1IgogICBoZWlnaHQ9Ijg1Igo+CiAgPGRlZnMKICAgICBpZD0iZGVmczQxNzIiPgogICAgPHJhZGlhbEdyYWRpZW50CiAgICAgICBpbmtzY2FwZTpjb2xsZW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MjUsLTE2LjUzMTMgMCwtMjEuMjk5NDEgNy4xMDY1LC0zOC41NjI1MDA0IDE1Ljg3NSwtMzguNTYyNTAwNCA4Ljc2MzM0LDAgMTUuODk4OTUsMTcuMjQ0MzkwNCAxNS45MDYyNSwzOC41MzEzMDA0IGwgLTE1LjkwNjI1LDAuMDMxIDE1LjkwNjI1LDAgYyAwLDUuOTI3NiAtMC41NDQzMSwxMS41MTM5IC0xLjUzMTI1LDE2LjUzMTIgNS43MTYzNiwtMS45MDQ2IDEwLjk0MTI3LC00Ljk0MTggMTUuNDM3NSwtOC44MTI1IDAuMzU2NTMsLTIuNDk4MSAwLjUzMTI1LC01LjA4MDggMC41MzEyNSwtNy43MTg3IDAsLTIyLjUxMDIzIC0xMy41OTE5OCwtNDAuNzgxMzAwNCAtMzAuMzQzNzUsLTQwLjc4MTMwMDQgeiIKICAgICAgIGlkPSJwYXRoNDkwOSIKICAgICAgIGlua3NjYXBlOmNvbm5lY3Rvci1jdXJ2YXR1cmU9IjAiIC8+CiAgPC9nPgo8L3N2Zz4K";
    img.src = imgStr;

    img = new Image();
    document.body.appendChild(img);
    var imgStr = "data:image/svg+xml,";
    imgStr += '<svg   xmlns="http://www.w3.org/2000/svg"   xmlns:xlink="http://www.w3.org/1999/xlink"   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"   width="85"   height="85">  <defs     id="defs4172">    <radialGradient       inkscape:collect="always"       xlink:href="#linearGradient4899"       id="radialGradient4907"       cx="219.63432"       cy="53.499279"       fx="219.63432"       fy="53.499279"       r="15.890625"       gradientTransform="matrix(-3.2783033,-0.72853385,0.69995344,-3.1496954,739.17321,345.31995)"       gradientUnits="userSpaceOnUse" />    <linearGradient       id="linearGradient4899">      <stop         style="stop-color:#000000;stop-opacity:1;"         offset="0"         id="stop4901" />      <stop         style="stop-color:#f7b4b4;stop-opacity:1;"         offset="1"         id="stop4903" />    </linearGradient>    <linearGradient       inkscape:collect="always"       xlink:href="#linearGradient4914"       id="linearGradient4920"       x1="216.71745"       y1="-52.048553"       x2="215.80347"       y2="85.141525"       gradientUnits="userSpaceOnUse"       gradientTransform="translate(-165.25951,-15.946439)" />    <linearGradient       inkscape:collect="always"       id="linearGradient4914">      <stop         style="stop-color:#ffffff;stop-opacity:1;"         offset="0"         id="stop4916" />      <stop         style="stop-color:#ffffff;stop-opacity:0;"         offset="1"         id="stop4918" />    </linearGradient>    <filter       color-interpolation-filters="sRGB"       inkscape:collect="always"       id="filter5096">      <feGaussianBlur         inkscape:collect="always"         stdDeviation="0.55645637"         id="feGaussianBlur5098" />    </filter>    <filter       color-interpolation-filters="sRGB"       inkscape:collect="always"       id="filter5076">      <feGaussianBlur         inkscape:collect="always"         stdDeviation="1.2507933"         id="feGaussianBlur5078" />    </filter>  </defs>  <g     inkscape:label="Layer 1"     inkscape:groupmode="layer"     id="layer1">    <path       id="path4922"       d="m 23.770467,50.684151 c -16.7517702,2.0478 -24.38592023,11.1113 -17.0842902,20.2355 7.3016202,9.1241 26.7788002,14.8597 43.5305702,12.812 16.75177,-2.0477 24.4273,-11.1024 17.12568,-20.2266 -7.30163,-9.1242 -26.82019,-14.8686 -43.57196,-12.8209 z m 0.7197,0.8994 c 8.76334,-1.0712 21.4925,5.0463 28.40462,13.6737 l -15.89611,1.957 15.90625,-1.9444 c 6.90889,8.6334 5.37076,16.5033 -3.39774,17.5751 -8.7685,1.0719 -21.47462,-5.0567 -28.38351,-13.6901 -6.90889,-8.6335 -5.40201,-16.4995 3.36649,-17.5713 z"       style="opacity:0.31858407;fill:#000000;fill-opacity:1;stroke:none;filter:url(#filter5076)"       inkscape:connector-curvature="0" />    <path       style="fill:#ff0000;fill-opacity:1;stroke:none"       d="m 50.365497,2.5223006 c -16.75177,0 -30.3125,18.2710704 -30.3125,40.7812504 0,22.5102 13.56073,40.75 30.3125,40.75 16.75177,0 30.34375,-18.2398 30.34375,-40.75 0,-22.51018 -13.59198,-40.7812504 -30.34375,-40.7812504 z m 0,2.21875 c 8.76334,0 15.89895,17.2443904 15.90625,38.5313004 l -15.90625,0.031 15.90625,0 c 0,21.2994 -7.13775,38.5625 -15.90625,38.5625 -8.7685,0 -15.875,-17.2631 -15.875,-38.5625 0,-21.29946 7.1065,-38.5625504 15.875,-38.5625504 z"       id="path4887"       inkscape:connector-curvature="0" />    <path       style="fill:url(#radialGradient4907);fill-opacity:1;stroke:none"       d="m 50.365497,4.7410506 c -8.7685,0 -15.875,17.2630904 -15.875,38.5625004 0,21.2994 7.1065,38.5625 15.875,38.5625 8.7685,0 15.90625,-17.2631 15.90625,-38.5625 l -2.375,0 c 0,20.2853 -6.07148,36.7188 -13.53125,36.7188 -7.45977,0 -13.5,-16.4335 -13.5,-36.7188 0,-20.28534 6.04023,-36.7500004 13.5,-36.7500004 7.45538,0 13.52504,16.4453704 13.53125,36.7188004 l 2.375,0 c -0.007,-21.28691 -7.14291,-38.5313004 -15.90625,-38.5313004 z"       id="path4891"       inkscape:connector-curvature="0" />    <path       style="fill:url(#linearGradient4920);fill-opacity:1;stroke:none;filter:url(#filter5096)"       d="m 50.365497,2.5223006 c -16.75177,0 -30.3125,18.2710704 -30.3125,40.7812504 0,2.6379 0.20673,5.2206 0.5625,7.7188 4.50164,3.8753 9.71309,6.9082 15.4375,8.8125 -0.98384,-5.0181 -1.5625,-10.6024 -1.5625,-16.5313 0,-21.29941 7.1065,-38.5625004 15.875,-38.5625004 8.76334,0 15.89895,17.2443904 15.90625,38.5313004 l -15.90625,0.031 15.90625,0 c 0,5.9276 -0.54431,11.5139 -1.53125,16.5312 5.71636,-1.9046 10.94127,-4.9418 15.4375,-8.8125 0.35653,-2.4981 0.53125,-5.0808 0.53125,-7.7187 0,-22.51023 -13.59198,-40.7813004 -30.34375,-40.7813004 z"       id="path4909"       inkscape:connector-curvature="0" />  </g></svg>';
    img.src = imgStr;
}
</script>
</head>
<body onload='myInit();'>

</body>
</html>
于 2012-10-06T14:15:33.200 回答