Preload基礎(chǔ)使用方法詳解
PreloadJS是一個用來管理和協(xié)調(diào)相關(guān)資源加載的類庫,它可以方便的幫助你預(yù)先加載相關(guān)資源,例如
圖片,文件,音頻,數(shù)據(jù),其他
它使用了XHR2來提供實時的加載進度信息,如果不支持則使用標(biāo)簽式的簡化進度來實現(xiàn)進度展示。
支持多隊列,多連接,暫停隊列等等功能
在PreloadJS中,LoadQueue是主要用來預(yù)加載內(nèi)容的API。LoadQueue是一個加載管理器,可以預(yù)先加載一個文件或者一個文件隊列。
var queue = new createjs.LoadQueue(true);
以上代碼中,傳遞一個false參數(shù)則強制使用標(biāo)簽式的加載。LoadQueue包含了以下幾個可以訂閱的事件:
complete: 當(dāng)隊列完成全部加載后觸發(fā)
error: 當(dāng)隊列遇到錯誤時觸發(fā)
progress: 整個隊列變化時展示的進度
fileload: 一個單獨文件加載完畢
fileprogress: 一個單獨文件變化的進度,請注意只有文件使用XHR加載才會觸發(fā),其它只會顯示0或者100%
可以通過調(diào)用loadfile("文件路徑")加載一個文件或者調(diào)用loadMnifest()來加載多個文件。
LoadQueue支持相關(guān)文件類型如下:
BINARY: XHR調(diào)用的二進制文件
CSS: CSS文件
IMAGE: 一般圖片文件格式
JAVASCRIPT: JavaScript文件
JSON: JSON數(shù)據(jù)
JSONP: 跨域JSON文件
MANIFEST: JSON格式的文件列表
SOUND: 音頻文件
SVG: SVG文件
TEXT: 文本文件 - 僅支持XHR
XML: XML數(shù)據(jù)
代碼示例:
var canvas = document.getElementById("myCanvas");
var stage = new createjs.Stage(canvas);
var manifest;
var preload;
var progressText = new createjs.Text("", "20px Arial", "#dd4814");
progressText.x = 125 - progressText.getMeasuredWidth() / 2;
progressText.y = 20;
stage.addChild(progressText);
stage.update();
//定義相關(guān)JSON格式文件列表
function setupManifest() {
manifest = [{
src: "http://cdn.gbtags.com/EaselJS/0.7.1/easeljs.min.js",
id: "easeljs"
}, {
src: "http://www.gbtags.com/gb/networks/uploadimgthumb/4d8f3f13-89c0-455c-95f3-ba5120c2f123.jpg",
id: "logo"
}, {
src: "http://www.gbtags.com/tutorials/html5-tutorial/html5-demos/assets/song.ogg",
id: "audiofile"
}
];
for(var i=1;i<=10;i++)
manifest.push({src:"http://www.gbtags.com/gb/laitu/200x200"})
}
//開始預(yù)加載
function startPreload() {
preload = new createjs.LoadQueue(true);
//注意加載音頻文件需要調(diào)用如下代碼行
preload.installPlugin(createjs.Sound);
preload.on("fileload", handleFileLoad);
preload.on("progress", handleFileProgress);
preload.on("complete", loadComplete);
preload.on("error", loadError);
preload.loadManifest(manifest);
}
//處理單個文件加載
function handleFileLoad(event) {
console.log("文件類型: " + event.item.type);
if(event.item.id == "logo"){
console.log("logo圖片已成功加載");
}
}
//處理加載錯誤:大家可以修改成錯誤的文件地址,可在控制臺看到此方法調(diào)用
function loadError(evt) {
console.log("加載出錯!",evt.text);
}
//已加載完畢進度
function handleFileProgress(event) {
progressText.text = "已加載 " + (preload.progress*100|0) + " %";
stage.update();
}
//全度資源加載完畢
function loadComplete(event) {
console.log("已加載完畢全部資源");
}
setupManifest();
startPreload();
更多關(guān)于PreloadJS使用方法請點擊下面的相關(guān)鏈接
相關(guān)文章
umi插件開發(fā)仿dumi項目實現(xiàn)markdown文件轉(zhuǎn)為頁面
這篇文章主要介紹了umi插件開發(fā)仿dumi項目實現(xiàn)markdown文件轉(zhuǎn)為頁面方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

