【问题标题】:Determine when an image has loaded in svg with RaphaelJS确定何时使用 RaphaelJS 在 svg 中加载图像
【发布时间】:2013-05-28 09:16:58
【问题描述】:

我正在尝试确定如何确定 svg 图像何时加载到浏览器中。我正在使用 Raphael JS,并且我已经尝试过:

var image = paper.image(path, 0,0,10,10);
image.node.addEventListener('load', function(){alert("test");});

和:

$('image').on('load')  

一切都无济于事。我还使用了“onload”和“onsvgload”,它们都不起作用。

是否可以确定 svg 图像是否已实际加载?

我什至尝试使用 Image() 对象加载图像,然后调用 paper.image() - 但我得到了两次对图像的调用(而不是使用预加载的图像); 即:

var preload = new Image();
preload.src = imgPath;
preload.addEventListener('load', function () {
    image.path = preload.src;
    //Now load image in raphael - except this still forces the browser to make another call for the image
});

有什么想法吗?

【问题讨论】:

    标签: javascript svg raphael


    【解决方案1】:

    使用onLoad 事件处理程序可以工作,只需多一行代码:

    var image = paper.image(path, 0,0,10,10);
    var image_node = image.node;
    image_node.setAttribute('externalResourcesRequired','true');
    image_node.addEventListener("load", function() {
        console.log("image is loaded!");
    })
    

    您需要将externalResourcesRequired 属性设置为true。您可以在这里阅读更多相关信息:http://www.w3.org/TR/SVG/struct.html#ExternalResourcesRequired

    【讨论】:

    • 这适用于除 IE 之外的所有浏览器。任何想法如何在 IE 中进行这项工作?
    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2013-01-05
    • 2011-05-28
    相关资源
    最近更新 更多