81 lines
3.4 KiB
JavaScript
81 lines
3.4 KiB
JavaScript
(function(){
|
|
// 全局图像缓存对象 存放已加载的图像资源
|
|
window.YooneSnowImageCache = window.YooneSnowImageCache || {};
|
|
|
|
// 获取或加载图像 根据 URL 返回图像对象和加载状态
|
|
window.YooneSnowGetOrLoadImage = function(imageUrl){
|
|
// 条件判断 如果未提供 URL 则返回空
|
|
if (!imageUrl || typeof imageUrl !== 'string'){
|
|
return { img: null, ready: false };
|
|
}
|
|
const existing = window.YooneSnowImageCache[imageUrl];
|
|
// 条件判断 如果已存在缓存则直接返回
|
|
if (existing && existing.ready){
|
|
return existing;
|
|
}
|
|
if (existing && !existing.ready){
|
|
// 条件判断 如果正在加载则返回当前状态
|
|
return existing;
|
|
}
|
|
// 创建新的图像对象 并开始加载
|
|
const img = new Image();
|
|
const record = { img: img, ready: false };
|
|
window.YooneSnowImageCache[imageUrl] = record;
|
|
try { img.decoding = 'async'; } catch(e) {}
|
|
try { img.fetchPriority = 'low'; } catch(e) {}
|
|
img.onload = function(){ record.ready = true; };
|
|
img.onerror = function(){
|
|
// 加载失败 从缓存移除避免重复错误
|
|
delete window.YooneSnowImageCache[imageUrl];
|
|
};
|
|
img.src = imageUrl;
|
|
return record;
|
|
};
|
|
|
|
window.YooneSnowLoadAssetViaFetch = function(imageUrl, onReady){
|
|
if (!imageUrl || typeof imageUrl !== 'string'){
|
|
if (typeof onReady === 'function'){ onReady(false); }
|
|
return;
|
|
}
|
|
var existing = window.YooneSnowImageCache[imageUrl];
|
|
if (existing && existing.ready){
|
|
if (typeof onReady === 'function'){ onReady(true); }
|
|
return;
|
|
}
|
|
if (!existing){ window.YooneSnowImageCache[imageUrl] = { img: null, ready: false }; }
|
|
if (typeof fetch === 'function' && typeof createImageBitmap === 'function'){
|
|
fetch(imageUrl, { cache: 'force-cache' }).then(function(resp){ return resp.blob(); }).then(function(blob){
|
|
return createImageBitmap(blob);
|
|
}).then(function(bmp){
|
|
window.YooneSnowImageCache[imageUrl] = { img: bmp, ready: true };
|
|
if (typeof onReady === 'function'){ onReady(true); }
|
|
}).catch(function(){
|
|
var rec = window.YooneSnowGetOrLoadImage(imageUrl);
|
|
var fired = false;
|
|
if (rec && rec.img){
|
|
var markReady = function(){ rec.ready = true; };
|
|
rec.img.onload = function(){ if (!fired){ fired = true; markReady(); if (typeof onReady === 'function'){ onReady(true); } } };
|
|
rec.img.onerror = function(){ if (!fired){ fired = true; if (typeof onReady === 'function'){ onReady(false); } } };
|
|
}
|
|
});
|
|
} else {
|
|
var rec2 = window.YooneSnowGetOrLoadImage(imageUrl);
|
|
var fired2 = false;
|
|
if (rec2 && rec2.img){
|
|
var markReady2 = function(){ rec2.ready = true; };
|
|
rec2.img.onload = function(){ if (!fired2){ fired2 = true; markReady2(); if (typeof onReady === 'function'){ onReady(true); } } };
|
|
rec2.img.onerror = function(){ if (!fired2){ fired2 = true; if (typeof onReady === 'function'){ onReady(false); } } };
|
|
}
|
|
}
|
|
};
|
|
|
|
// 居中绘制图像 根据目标中心点和宽高进行缩放绘制
|
|
window.YooneSnowDrawCenteredImage = function(context, img, centerX, centerY, width, height){
|
|
// 条件判断 如果图像不存在则不绘制
|
|
if (!img) { return; }
|
|
const drawX = centerX - width / 2;
|
|
const drawY = centerY - height / 2;
|
|
context.drawImage(img, drawX, drawY, width, height);
|
|
};
|
|
})();
|