在我的画布中显示我的资源的加载进度
Display the loading progress of my resources in my canvas
我正面临一个问题与画布。不返回任何错误。我试图做一个加载器,加载每一个资源,如图片,声音,视频等,在应用程序开始之前。加载程序必须绘制动态加载的资源数量。但是此时此刻,我的加载器的结果是冻结浏览器,直到它绘制加载的资源总数。
告诉我如果我不清楚:)这是代码:
function SimpleLoader(){
var ressources ;
var canvas;
var ctx;
this.getRessources = function(){
return ressources;
};
this.setAllRessources = function(newRessources){
ressources = newRessources;
};
this.getCanvas = function(){
return canvas;
};
this.setCanvas = function(newCanvas){
canvas = newCanvas;
};
this.getCtx = function(){
return ctx;
};
this.setCtx = function(newCtx){
ctx = newCtx;
};
};
SimpleLoader.prototype.init = function (ressources, canvas, ctx){
this.setAllRessources(ressources);
this.setCanvas(canvas);
this.setCtx(ctx);
};
SimpleLoader.prototype.draw = function (){
var that = this;
this.getCtx().clearRect(0, 0, this.getCanvas().width, this.getCanvas().height);
this.getCtx().fillStyle = "black";
this.getCtx().fillRect(0,0,this.getCanvas().width, this.getCanvas().height)
for(var i = 0; i < this.getRessources().length; i++){
var data = this.getRessources()[i];
if(data instanceof Picture){
var drawLoader = function(nbLoad){
that.getCtx().clearRect(0, 0, that.getCanvas().width, that.getCanvas().height);
that.getCtx().fillStyle = "black";
that.getCtx().fillRect(0,0, that.getCanvas().width, that.getCanvas().height);
that.getCtx().fillStyle = "white";
that.getCtx().fillText("Chargement en cours ... " + Number(nbLoad) +"/"+ Number(100), that.getCanvas().width/2, 100 );
}
data.img = new Image();
data.img.src = data.src;
data.img.onload = drawLoader(Number(i)+1); //Update loader to reflect picture loading progress
} else if(data instanceof Animation){
/* Load animation */
} else if(data instanceof Video){
/* Load video */
} else if(data instanceof Sound){
/* Load sound */
}else {
}
}
};
所以在这段代码中,所有的资源都被加载了,但是我想显示加载的进度。知道我错过了什么吗?
你在加载器中"忙循环",所以浏览器没有机会重新绘制/更新画布。
你可以实现一个setTimeout(getNext, 0)
或者把draw函数放在一个requestAnimationFrame
循环之外轮询当前状态。在这种情况下,我建议使用前者。
在伪代码中,这是让它工作的一种方法:
//Global:
currentItem = 0
total = numberOfItems
//The loop:
function getNextItem() {
getItem(currentItem++);
drawProgressToCanvas();
if (currentItem < total)
setTimeout(getNextItem(), 0);
else
isReady();
}
getNextItem(); //start the loader
根据需要采用
值为0的setTimeout
将提示下一次有时间的呼叫(即。重绘后,清空事件堆栈等)。这里的isReady()
只是加载所有内容后进入下一步的一种方法。(如果您发现使用0有任何问题,请尝试使用例如16。)
使用requestAnimationFrame
是一种更低级、更有效的方法。目前并不是所有的浏览器都支持它,但是有一些多填充可以帮你解决这个问题——对于这种用法,它不是那么重要,但只是让你知道这个选项(如果你还没有)。
相关文章:
- 我的加载更多功能适用于按钮单击,但它不适用于滚动
- jQuery 限制我正在加载/追加的字符数
- 优化 CSS 交付.但我正在加载 JS 的样式
- $(窗口).on('scroll',函数 () 当滚动到 50% 时,我想加载很少的数据
- jQuery Mobile添加了我可以加载的文本't拆卸
- 当我重新加载页面时,我放在文本框中的文本保持不变,我能让这种情况不发生吗
- 我如何加载javascript模板与他们的id
- require.js:我如何加载一个模块,它在不同的名称下定义了一个名称
- 为什么我的加载处理程序不起作用
- 我如何在网站上加载特定的字体与较少的加载时间
- 当我重新加载页面时,我之前的评论消失了——这与本地存储有关
- 我可以订阅openwindow的加载事件吗?
- Jstree:当我重新加载树时,我不能将所有的复选框重置为默认取消选中
- 我可以加载一个新的页面时,有人点击关闭按钮在浏览器上
- 为什么以及如何在console.log中引用变量来修复我遇到的加载错误?
- 我的加载更多的按钮是不显示,尽管一切都是正确的,根据我
- 为什么我的加载更多的按钮后,ajax加载需要另一个点击它之前隐藏,即使没有什么要加载
- 如何优化我的加载JQuery函数
- 为什么我的加载没有'不起作用
- 当加载html与ajax在jQuery,我怎么能确保我的加载gif保持可见的整个时间,直到html已添加