canvas 中的图像回退与 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 不是 return HTMLImageElement 对象,而是某种 Raphael 对象。

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