通过获取异步加载JS文件进度实现一个canvas环形loading图

1.整理下思路,要获取异步加载JS文件的进度要怎么做?

答:将需要异步载入的文件放进一个数组中。如下。

const scriptArr = ['./test_1.js', './test_3.js', './test_4.js', './test_5.js'];

然后动态创建script标签插入到body标签中。通过script.onload获取JS是否加载完毕

2.怎么绘制一个动态的canvas环形loading加载图?

答:需要用到的canvas 核心Api有:ctx.arc()。这是绘制园环的必须api.

3.既然能获取到加载完毕的回调函数,也能够创建一个canvas loading实例,如何把它们关联到一起整合到一块?

  1. 编写一个circleProgress类,用来创建环形loading实例

        class CircleProgress {
            constructor(ctxs, width, height, arc) {
                this.ctx = ctxs
                this.width = width
                this.height = height
                this.arc = arc
    
                this.setArea(width, height)
            }
            //设置canvas的宽高
            setArea(width, height) {
                this.ctx.canvas.width = width
                this.ctx.canvas.height = height
            }
            //清除画布
            clearFill() {
                this.ctx.clearRect(0, 0, this.width, this.width);
            }
             //绘制环形进度图的背景 颜色是可配置的 
            fillBg() {
                this.ctx.beginPath();
                this.ctx.lineWidth = this.arc;
                this.ctx.strokeStyle = '#ccc';
                this.ctx.arc(this.width / 2, this.width / 2, 45, 0, 2 * Math.PI);
                this.ctx.stroke();
            }
            //绘制进度条
            fillArc(x) {
                this.ctx.beginPath();
                this.ctx.lineWidth = this.arc;
                this.ctx.strokeStyle = 'yellow';
                this.ctx.arc(this.width / 2, this.width / 2, 45, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);
                this.ctx.stroke();
            }
            //绘制中心数字展示
            fillText(x) {
                this.ctx.font = '14px' + ' Arial';
                this.ctx.fillStyle = 'red';
                this.ctx.textBaseline = "middle";
                this.ctx.textAlign = 'center';
                this.ctx.fillText(x.toFixed(1) + '%', this.width / 2, this.width / 2);
            }
            //总绘制方法
            fill(x) {
                this.fillBg();
                this.fillArc(x);
                this.fillText(x);
            }
    
        }

    大概就是这个样子
    clipboard.png

  2. 获取当前JS,加载进度
    function jsProgress(circle, eachs, max, scriptArr) {
        let currentIndex = 0;
        //遍历所有文件名
        for (let i = 0; i < scriptArr.length; i++) {
            let scriptNode = document.createElement('script');
            scriptNode.src = scriptArr[i];
            
            //插入创建好的script引用节点
            document.getElementById('bodys').appendChild(scriptNode);
            
            //创建分布值 每个文件占据的进度值 比如4个文件 每个文件占据100/4=25
            let steps = 0;
            
            //插入的文件加载完毕后的回调
            scriptNode.onload = function() {
                //按照每20毫秒一帧渲染canvas画布 以展示出动态的加载效果
                let ani = setInterval(function() {

                    //此处可以优化,有好的建议可以告诉我
                    if (steps <= max || steps == 100) {
                        circle.clearFill();
                        if (steps > 100) {
                            steps = 100
                        }

                        circle.fill(steps)
                        steps++
                    } else {
                        clearInterval(ani)
                        if (max <= 100) {
                            max = max + eachs
                            currentIndex++;
                        }
                        if (currentIndex == scriptArr.length) {
                            console.log(`全部JS加载完成`)
                        }
                        console.log(`sciprtNode${i}已加载完成`)
                    }
                }, 20)

                
                
            }
        }
    }

最终效果

clipboard.png

附录:全部代码

        <script>
        class CircleProgress {
            constructor(ctxs, width, height, arc) {
                this.ctx = ctxs
                this.width = width
                this.height = height
                this.arc = arc

                this.setArea(width, height)
            }

            setArea(width, height) {
                this.ctx.canvas.width = width
                this.ctx.canvas.height = height
            }

            clearFill() {
                this.ctx.clearRect(0, 0, this.width, this.width);
            }

            fillBg() {
                this.ctx.beginPath();
                this.ctx.lineWidth = this.arc;
                this.ctx.strokeStyle = '#ccc';
                this.ctx.arc(this.width / 2, this.width / 2, 45, 0, 2 * Math.PI);
                this.ctx.stroke();
            }

            fillArc(x) {
                this.ctx.beginPath();
                this.ctx.lineWidth = this.arc;
                this.ctx.strokeStyle = 'yellow';
                this.ctx.arc(this.width / 2, this.width / 2, 45, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);
                this.ctx.stroke();
            }

            fillText(x) {
                this.ctx.font = '14px' + ' Arial';
                this.ctx.fillStyle = 'red';
                this.ctx.textBaseline = "middle";
                this.ctx.textAlign = 'center';
                this.ctx.fillText(x.toFixed(1) + '%', this.width / 2, this.width / 2);
            }

            fill(x) {
                this.fillBg();
                this.fillArc(x);
                this.fillText(x);
            }

            testFn() {
                ctxs.beginPath();
                ctxs.lineWidth = 10;
                ctxs.strokeStyle = '#ccc';
                ctxs.arc(50, 50, 45, 0, 2 * Math.PI);
                ctxs.stroke();
            }

        }

        function jsProgress(circle, eachs, max, scriptArr) {
            let currentIndex = 0;

            for (let i = 0; i < scriptArr.length; i++) {
                let scriptNode = document.createElement('script');
                scriptNode.src = scriptArr[i];

                document.getElementById('bodys').appendChild(scriptNode);

                let steps = 0;

                scriptNode.onload = function() {
                    let ani = setInterval(function() {


                        if (steps <= max || steps == 100) {
                            circle.clearFill();
                            if (steps > 100) {
                                steps = 100
                            }

                            circle.fill(steps)
                            steps++
                        } else {
                            clearInterval(ani)
                            if (max <= 100) {
                                max = max + eachs
                                currentIndex++;
                            }
                            console.log(`sciprtNode${i}已加载完成`)
                  
                            if (currentIndex == scriptArr.length) {
                                console.log(`全部JS加载完成`)
                            }
                        }
                    }, 20)

                }
            }
        }

        const scriptArr = ['./test_1.js', './test_3.js', './test_4.js', './test_5.js'];

        let canvasNode = document.getElementById('canvas'),
            ctxs = canvasNode.getContext("2d");

        let circle = new CircleProgress(ctxs, 100, 100, 10),
            eachs = parseInt(100 / scriptArr.length),
            max = eachs


        jsProgress(circle, eachs, max, scriptArr);

        // circle.testFn()
    </script>

钻到底~

400 声望
28 粉丝
0 条评论
推荐阅读
Cornerstone.js 介绍 (持续更新)
JS编写的医疗DICOM影像浏览工具,用来支持医学影像的显示与交互。他的作用和他的名字一样基石,很多医疗的影像阅片系统都是基于cornerstone.js开发的,如OHIF Viewer、Lesion Tracker等,目前国内医疗大部分结合A...

风吹一个大耳东3阅读 8.5k评论 5

ESlint + Stylelint + VSCode自动格式化代码(2023)
安装插件 ESLint,然后 File -&gt; Preference-&gt; Settings(如果装了中文插件包应该是 文件 -&gt; 选项 -&gt; 设置),搜索 eslint,点击 Edit in setting.json

谭光志34阅读 20.8k评论 9

安全地在前后端之间传输数据 - 「3」真的安全吗?
在「2」注册和登录示例中,我们通过非对称加密算法实现了浏览器和 Web 服务器之间的安全传输。看起来一切都很美好,但是危险就在哪里,有些人发现了,有些人嗅到了,更多人却浑然不知。就像是给门上了把好锁,还...

边城32阅读 7.3k评论 5

封面图
涨姿势了,有意思的气泡 Loading 效果
今日,群友提问,如何实现这么一个 Loading 效果:这个确实有点意思,但是这是 CSS 能够完成的?没错,这个效果中的核心气泡效果,其实借助 CSS 中的滤镜,能够比较轻松的实现,就是所需的元素可能多点。参考我们...

chokcoco24阅读 2.2k评论 3

在前端使用 JS 进行分类汇总
最近遇到一些同学在问 JS 中进行数据统计的问题。虽然数据统计一般会在数据库中进行,但是后端遇到需要使用程序来进行统计的情况也非常多。.NET 就为了对内存数据和数据库数据进行统一地数据处理,发明了 LINQ (L...

边城17阅读 2k

封面图
过滤/筛选树节点
又是树,是我跟树杠上了吗?—— 不,是树的问题太多了!🔗 相关文章推荐:使用递归遍历并转换树形数据(以 TypeScript 为例)从列表生成树 (JavaScript/TypeScript) 过滤和筛选是一个意思,都是 filter。对于列表来...

边城18阅读 7.8k评论 3

封面图
Vue2 导出excel
2020-07-15更新 excel导出安装 {代码...} src文件夹下新建一个libs文件夹,新建一个excel.js {代码...} vue页面中使用 {代码...} ===========================以下为早期的文章今天在开发的过程中需要做一个Vue的...

原谅我一生不羁放歌搞文艺14阅读 20k评论 9

钻到底~

400 声望
28 粉丝
宣传栏