繁体   English   中英

使用Raphael在画布中进行图像后备

[英]Image fallback in canvas with Raphael

我必须绘制一些图像(使用for循环),并且如果找不到正确的图像,则要加载后备图像。

我的JavaScript:

var image = paper_layer.image(image_selected, x, y, width, height);
$('image').one('error', function () {    
    console.log('image not found')
    image_selected='/my_path/not_found.png'
    var image = paper_layer.image(image_selected, x, y, width, height);
});

问题在于,当错过一个图像时,此功能会为每个图像(甚至找到的图像)添加一个备用图像。

我也尝试过此操作,但是它不起作用(没有回退,控制台上未显示'image not found' ”消息):

var image = paper_layer.image(image_selected, x, y, width, height);
image.onerror=function () {
    console.log('image not found')
    image_selected='/my_path/not_found.png'
    var image = paper_layer.image(image_selected, x, y, width, height);
};

PS:我正在使用Raphael

paper_layer.image不返回HTMLImageElement对象,而是某种Raphael对象。

但是,这似乎包含对键0下的实际图像元素的引用-因此,请尝试image[0].onerror=... ,它应将错误处理程序正确绑定到实际HTML图像元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM