Determine when an image has loaded in svg with RaphaelJS

纵然是瞬间 提交于 2019-12-22 06:47:23

问题


I'm trying to work out how to determine when an svg image has loaded in the browser. I'm using Raphael JS and I've tried:

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

and:

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

all to no avail. I've also used "onload" and "onsvgload" none of which work.

Is there away to determine if an svg image has actually loaded?

I even tried loading the image using an Image() object and then calling paper.image() - but I get two calls to the image (instead of using the preloaded image); ie:

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
});

Any ideas?


回答1:


Using the onLoad event handler works, with one additional line of code:

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!");
})

You need to set the externalResourcesRequired attribute to true. You may read more about it here: http://www.w3.org/TR/SVG/struct.html#ExternalResourcesRequired



来源:https://stackoverflow.com/questions/14875675/determine-when-an-image-has-loaded-in-svg-with-raphaeljs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!