欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于面向对象思想封装一个水球插件

程序员文章站 2024-01-26 22:41:10
首先上图, 需要一个环形的水球,没错,我和大家想的一样,去把插件,可是没有一个满意的插件,败兴而归啊;自己动手实践;下面对于开发思路总结: 1.绘制图型需要canvas,canvas中我们需要绘制圆,裁切,绘制文字,进度绘制其实也是圆 2.html5元素canvas 3.是否需要其他的插件做为依赖, ......

首先上图,

基于面向对象思想封装一个水球插件

需要一个环形的水球,没错,我和大家想的一样,去把插件,可是没有一个满意的插件,败兴而归啊;自己动手实践;下面对于开发思路总结:

1.绘制图型需要canvas,canvas中我们需要绘制圆,裁切,绘制文字,进度绘制其实也是圆

2.html5元素canvas

3.是否需要其他的插件做为依赖,为了高的移植性和复制性,那么我打算去掉了依赖的jquery等

好了,先开始第一版

  1 <!doctype html>
  2 <html lang="en">
  3   <head>
  4     <meta charset="utf-8" />
  5     <title>test</title>
  6     <style>
  7       body {
  8         display: flex;
  9         flex-flow: column wrap;
 10         justify-content: center;
 11         align-items: center;
 12       }
 13       #c {
 14         margin-top: 20px;
 15         background: #435ae4;
 16       }
 17     </style>
 18   </head>
 19   <body>
 20     <canvas id="canvas">当前浏览器不支持canvas 请升级!</canvas>
 21   </body>
 22   <script>
 23     canvas = document.getelementbyid("canvas");
 24     ctx = canvas.getcontext("2d");
 25     datas = 100;
 26     m = math;
 27     sin = m.sin;
 28     cos = m.cos;
 29     sqrt = m.sqrt;
 30     pow = m.pow;
 31     pi = m.pi;
 32     round = m.round;
 33     ow = canvas.width = 250;
 34     oh = canvas.height = 250;
 35     canvas.style.borderradius = "50%";
 36     // 线宽
 37     linewidth = 1;
 38     // 大半径
 39     r = ow / 2;
 40     cr = r - 1 * linewidth;
 41     ctx.beginpath();
 42     ctx.linewidth = linewidth;
 43     // 水波动画初始参数
 44     axislength = 2 * r - 16 * linewidth; // sin 图形长度
 45     unit = axislength / 9; // 波浪宽
 46     range = 0.4; // 浪幅
 47     nowrange = range;
 48     xoffset = 8 * linewidth; // x 轴偏移量
 49     data = datas / 100; // 数据量
 50     sp = 0; // 周期偏移量
 51     nowdata = 0;
 52     waveupsp = 0.006; // 水波上涨速度
 53     // 圆动画初始参数
 54     arcstack = []; // 圆栈
 55     br = r - 20 * linewidth;
 56     soffset = -(pi / 2); // 圆动画起始位置
 57     circlelock = true; // 起始动画锁
 58     // 获取圆动画轨迹点集
 59     for (var i = soffset; i < soffset + 2 * pi; i += 1 / (8 * pi)) {
 60       arcstack.push([r + br * cos(i), r + br * sin(i)]);
 61     }
 62     // 圆起始点
 63     cstartpoint = arcstack.shift();
 64     ctx.fillstyle = "transparent";
 65     ctx.moveto(cstartpoint[0], cstartpoint[1]);
 66     ctx.fill();
 67     // 开始渲染
 68     render();
 69     function drawsine() {
 70       ctx.beginpath();
 71       ctx.save();
 72       var stack = []; // 记录起始点和终点坐标
 73       for (var i = xoffset; i <= xoffset + axislength; i += 20 / axislength) {
 74         var x = sp + (xoffset + i) / unit;
 75         var y = sin(x) * nowrange;
 76         var dx = i;
 77         var dy = 2 * cr * (1 - nowdata) + (r - cr) - unit * y;
 78         ctx.lineto(dx, dy);
 79         stack.push([dx, dy]);
 80       }
 81       // 获取初始点和结束点
 82       var startp = stack[0];
 83       var endp = stack[stack.length - 1];
 84       ctx.lineto(xoffset + axislength, ow);
 85       ctx.lineto(xoffset, ow);
 86       ctx.lineto(startp[0], startp[1]);
 87       ctx.fillstyle = "#3b7bf8";
 88       ctx.fill();
 89       ctx.restore();
 90     }
 91 
 92     function drawtext() {
 93       ctx.globalcompositeoperation = "source-over";
 94       var size = 0.4 * cr;
 95       ctx.font = "bold " + size + "px microsoft yahei";
 96       txt = (nowdata.tofixed(2) * 100).tofixed(0) + "%";
 97       var fonty = r + size / 2;
 98       var fontx = r - size * 0.8;
 99       ctx.fillstyle = "#9e90ef";
100       ctx.textalign = "center";
101       ctx.filltext(txt, r + 5, r + 20);
102     }
103     //最外面淡黄色圈
104     function drawcircle() {
105       ctx.beginpath();
106       ctx.linewidth = 15;
107       ctx.strokestyle = "#3f1eb9";
108       ctx.arc(r, r, cr - 6.1, 0, 2 * math.pi);
109       ctx.stroke();
110       ctx.restore();
111     }
112     //灰色圆圈
113     function graycircle() {
114       ctx.beginpath();
115       ctx.linewidth = 1;
116       ctx.strokestyle = "transparent";
117       ctx.arc(r, r, cr + 10, 0, 2 * math.pi);
118       ctx.stroke();
119       ctx.restore();
120       ctx.beginpath();
121     }
122     //橘黄色进度圈
123     function orangecircle() {
124       ctx.beginpath();
125       ctx.strokestyle = "#b0baee";
126       //使用这个使圆环两端是圆弧形状
127       ctx.linecap = "round";
128       ctx.arc(
129         r,
130         r,
131         cr - 4,
132         0 * (math.pi / 180.0) - math.pi / 2,
133         nowdata * 360 * (math.pi / 180.0) - math.pi / 2
134       );
135       ctx.stroke();
136       ctx.save();
137     }
138     //裁剪中间水圈
139     function clipcircle() {
140       ctx.beginpath();
141       ctx.arc(r, r, cr - 10, 0, 2 * math.pi, false);
142       ctx.clip();
143     }
144     //渲染canvas
145     function render() {
146       ctx.clearrect(0, 0, ow, oh);
147       //最外面淡黄色圈
148       drawcircle();
149       //灰色圆圈
150       graycircle();
151       //橘黄色进度圈
152       orangecircle();
153       //裁剪中间水圈
154       clipcircle();
155       // 控制波幅
156 
157       if (data >= 0.85) {
158         if (nowrange > range / 4) {
159           var t = range * 0.01;
160           nowrange -= t;
161         }
162       } else if (data <= 0.1) {
163         if (nowrange < range * 1.5) {
164           var t = range * 0.01;
165           nowrange += t;
166         }
167       } else {
168         if (nowrange <= range) {
169           var t = range * 0.01;
170           nowrange += t;
171         }
172         if (nowrange >= range) {
173           var t = range * 0.01;
174           nowrange -= t;
175         }
176       }
177       if (data - nowdata > 0) {
178         nowdata += waveupsp;
179       }
180       if (data - nowdata < 0) {
181         nowdata -= waveupsp;
182       }
183       sp += 0.07;
184       // 开始水波动画
185       drawsine();
186       // 写字
187       drawtext();
188       requestanimationframe(render);
189     }
190   </script>
191 </html>

但是这样并不友好,每一次都需要写这么长的脚本,实在不符合前端的工程化编程;进入优化时代:

1.我们可以像swiper那样通过一个对象来实例化每一个轮播对象,那么我们也创建一个对象实例化我们的水球

2.确定一些可变的参数:比如背景色,进度条的颜色,

3.对象内的方法私有化,减少全局使用

4.独立我们需要的水球效果脚本,只需要传入id和实例化参数可以实现不同的样式

5.对于不输入参数的,我们实例化时自动配置默认的一些参数;

  1 /**
  2  *  author:starry
  3  *  version:0.0.1;
  4  *  params:{
  5  *    id:canvans的id,默认canvas string
  6  *    text:为空则不显示,水纹的字体颜色 string
  7  *    textc:字体颜色,默认是#000, string
  8  *    r:圆球半径大小 number
  9  *    linew:线条的粗细 number
 10  *    data:进度数据,number
 11  *    pc: 进度条颜色  string
 12  *    wc:水球颜色    string
 13  *    bg: canvas背景色 string
 14  * }
 15  *
 16  */
 17 function getwater(options) {
 18   let id = options.id ? options.id : "canvas";
 19   let text = options.text ? options.text : "";
 20   let textc = options.textc ? options.textc : "#000";
 21   let r = options.r ? options.r : 200;
 22   let linew = options.w ? options.w : 1;
 23   let data = options.data ? options.data : 30;
 24   let progress = options.pc ? options.pc : "";
 25   let wc = options.wc ? options.wc : "#3b7bf8";
 26   let bg = options.bg ? options.bg : "#fff";
 27   canvas = document.getelementbyid(id);
 28 
 29   this.ctx = canvas.getcontext("2d");
 30 
 31   m = math;
 32   sin = m.sin;
 33   cos = m.cos;
 34   sqrt = m.sqrt;
 35   pow = m.pow;
 36   pi = m.pi;
 37   round = m.round;
 38   ow = canvas.width = r;
 39   oh = canvas.height = r;
 40   if (options.bg != "") {
 41     canvas.style.borderradius = "50%";
 42   }
 43   canvas.style.background = bg;
 44   // 线宽
 45   linewidth = linew;
 46   // 大半径
 47   r = ow / 2;
 48   cr = r - 1 * linewidth;
 49   this.ctx.beginpath();
 50   this.ctx.linewidth = linewidth;
 51   // 水波动画初始参数
 52   axislength = 2 * r - 16 * linewidth; // sin 图形长度
 53   unit = axislength / 9; // 波浪宽
 54   range = 0.4; // 浪幅
 55   nowrange = range;
 56   xoffset = 8 * linewidth; // x 轴偏移量
 57   this.data = data / 100; // 数据量
 58   sp = 0; // 周期偏移量
 59   this.nowdata = 0;
 60   waveupsp = 0.006; // 水波上涨速度
 61   // 圆动画初始参数
 62   arcstack = []; // 圆栈
 63   br = r - 20 * linewidth;
 64   soffset = -(pi / 2); // 圆动画起始位置
 65   circlelock = true; // 起始动画锁
 66   // 获取圆动画轨迹点集
 67   for (var i = soffset; i < soffset + 2 * pi; i += 1 / (8 * pi)) {
 68     arcstack.push([r + br * cos(i), r + br * sin(i)]);
 69   }
 70   // 圆起始点
 71   cstartpoint = arcstack.shift();
 72   this.ctx.fillstyle = "transparent";
 73   this.ctx.moveto(cstartpoint[0], cstartpoint[1]);
 74   this.ctx.fill();
 75   // 开始渲染
 76   this.drawsine = function() {
 77     this.ctx.beginpath();
 78     this.ctx.save();
 79     var stack = []; // 记录起始点和终点坐标
 80     for (var i = xoffset; i <= xoffset + axislength; i += 20 / axislength) {
 81       var x = sp + (xoffset + i) / unit;
 82       var y = sin(x) * nowrange;
 83       var dx = i;
 84       var dy = 2 * cr * (1 - that.nowdata) + (r - cr) - unit * y;
 85       this.ctx.lineto(dx, dy);
 86       stack.push([dx, dy]);
 87     }
 88     // 获取初始点和结束点
 89     var startp = stack[0];
 90     var endp = stack[stack.length - 1];
 91     this.ctx.lineto(xoffset + axislength, ow);
 92     this.ctx.lineto(xoffset, ow);
 93     this.ctx.lineto(startp[0], startp[1]);
 94     this.ctx.fillstyle = wc;
 95     this.ctx.fill();
 96     this.ctx.restore();
 97   };
 98   this.drawtext = function() {
 99     this.ctx.globalcompositeoperation = "source-over";
100     var size = 0.4 * cr;
101     this.ctx.font = "bold " + size + "px microsoft yahei";
102 
103     if (typeof text == "number") {
104       txt = (that.nowdata.tofixed(2) * 100).tofixed(0) + "%";
105     } else {
106       txt = text;
107     }
108 
109     var fonty = r + size / 2;
110     var fontx = r - size * 0.8;
111     this.ctx.fillstyle = textc;
112     this.ctx.textalign = "center";
113     this.ctx.filltext(txt, r + 5, r + 20);
114   };
115   //最外面淡黄色圈
116   this.drawcircle = function() {
117     this.ctx.beginpath();
118     this.ctx.linewidth = 15;
119     this.ctx.strokestyle = progress;
120     this.ctx.arc(r, r, cr - 6.1, 0, 2 * math.pi);
121     this.ctx.stroke();
122     this.ctx.restore();
123   };
124   //灰色圆圈
125   this.graycircle = function() {
126     this.ctx.beginpath();
127     this.ctx.linewidth = 1;
128     this.ctx.strokestyle = "transparent";
129     this.ctx.arc(r, r, cr + 10, 0, 2 * math.pi);
130     this.ctx.stroke();
131     this.ctx.restore();
132     this.ctx.beginpath();
133   };
134   //橘黄色进度圈
135   this.orangecircle = function() {
136     this.ctx.beginpath();
137     this.ctx.strokestyle = "#b0baee";
138     //使用这个使圆环两端是圆弧形状
139     this.ctx.linecap = "round";
140     this.ctx.arc(
141       r,
142       r,
143       cr - 4,
144       0 * (math.pi / 180.0) - math.pi / 2,
145       that.nowdata * 360 * (math.pi / 180.0) - math.pi / 2
146     );
147     this.ctx.stroke();
148     this.ctx.save();
149   };
150   //裁剪中间水圈
151   this.clipcircle = function() {
152     this.ctx.beginpath();
153     this.ctx.arc(r, r, cr - 10, 0, 2 * math.pi, false);
154     this.ctx.clip();
155   };
156   var that = this;
157   this.render = function() {
158     that.ctx.clearrect(0, 0, ow, oh);
159     //最外面淡黄色圈
160     that.drawcircle();
161     //灰色圆圈
162     that.graycircle();
163     //橘黄色进度圈
164     that.orangecircle();
165     //裁剪中间水圈
166     that.clipcircle();
167     // 控制波幅
168 
169     if (that.data >= 0.85) {
170       if (nowrange > range / 4) {
171         var t = range * 0.01;
172         nowrange -= t;
173       }
174     } else if (that.data <= 0.1) {
175       if (nowrange < range * 1.5) {
176         var t = range * 0.01;
177         nowrange += t;
178       }
179     } else {
180       if (nowrange <= range) {
181         var t = range * 0.01;
182         nowrange += t;
183       }
184       if (nowrange >= range) {
185         var t = range * 0.01;
186         nowrange -= t;
187       }
188     }
189     if (that.data - that.nowdata > 0) {
190       that.nowdata += waveupsp;
191     }
192     if (that.data - that.nowdata < 0) {
193       that.nowdata -= waveupsp;
194     }
195     sp += 0.07;
196     // 开始水波动画
197     that.drawsine();
198     // 写字
199     if (text != "") {
200       that.drawtext();
201     }
202     requestanimationframe(that.render);
203   };
204   this.render();
205 
206   //渲染canvas
207 }

这么一看我们示例化水球就好对多了

基于面向对象思想封装一个水球插件

没错,就怎么new一下就可以了

但是发现一个问题进度条并没有随着数值的改变而改变只是和最后一个data:10保持一致,这个不可以啊,感觉弱爆了,不行再有化一下,

  1 /**
  2  *  author:starry
  3  *  version:0.0.1;
  4  *  params:{
  5  *    id:canvans的id,默认canvas string
  6  *    text:为空则不显示,水纹的字体颜色 string
  7  *    textc:字体颜色,默认是#000, string
  8  *    r:圆球半径大小 number
  9  *    linew:线条的粗细 number
 10  *    data:进度数据,number
 11  *    pc: 进度条颜色  string
 12  *    wc:水球颜色    string
 13  *    bg: canvas背景色 string
 14  * }
 15  *
 16  */
 17 function getwater(options) {
 18   let id = options.id ? options.id : "canvas";
 19   let text = options.text ? options.text : "";
 20   let textc = options.textc ? options.textc : "#000";
 21   let r = options.r ? options.r : 200;
 22   let linew = options.w ? options.w : 1;
 23   let data = options.data ? options.data : 30;
 24   let progress = options.pc ? options.pc : "";
 25   let wc = options.wc ? options.wc : "#3b7bf8";
 26   let bg = options.bg ? options.bg : "#fff";
 27   canvas = document.getelementbyid(id);
 28 
 29   this.ctx = canvas.getcontext("2d");
 30 
 31   m = math;
 32   sin = m.sin;
 33   cos = m.cos;
 34   sqrt = m.sqrt;
 35   pow = m.pow;
 36   pi = m.pi;
 37   round = m.round;
 38   ow = canvas.width = r;
 39   oh = canvas.height = r;
 40   if (options.bg != "") {
 41     canvas.style.borderradius = "50%";
 42   }
 43   canvas.style.background = bg;
 44   // 线宽
 45   linewidth = linew;
 46   // 大半径
 47   r = ow / 2;
 48   cr = r - 1 * linewidth;
 49   this.ctx.beginpath();
 50   this.ctx.linewidth = linewidth;
 51   // 水波动画初始参数
 52   axislength = 2 * r - 16 * linewidth; // sin 图形长度
 53   unit = axislength / 9; // 波浪宽
 54   range = 0.4; // 浪幅
 55   nowrange = range;
 56   xoffset = 8 * linewidth; // x 轴偏移量
 57   this.data = data / 100; // 数据量
 58   sp = 0; // 周期偏移量
 59   this.nowdata = 0;
 60   waveupsp = 0.006; // 水波上涨速度
 61   // 圆动画初始参数
 62   arcstack = []; // 圆栈
 63   br = r - 20 * linewidth;
 64   soffset = -(pi / 2); // 圆动画起始位置
 65   circlelock = true; // 起始动画锁
 66   // 获取圆动画轨迹点集
 67   for (var i = soffset; i < soffset + 2 * pi; i += 1 / (8 * pi)) {
 68     arcstack.push([r + br * cos(i), r + br * sin(i)]);
 69   }
 70   // 圆起始点
 71   cstartpoint = arcstack.shift();
 72   this.ctx.fillstyle = "transparent";
 73   this.ctx.moveto(cstartpoint[0], cstartpoint[1]);
 74   this.ctx.fill();
 75   // 开始渲染
 76   this.drawsine = function() {
 77     this.ctx.beginpath();
 78     this.ctx.save();
 79     var stack = []; // 记录起始点和终点坐标
 80     for (var i = xoffset; i <= xoffset + axislength; i += 20 / axislength) {
 81       var x = sp + (xoffset + i) / unit;
 82       var y = sin(x) * nowrange;
 83       var dx = i;
 84       var dy = 2 * cr * (1 - that.nowdata) + (r - cr) - unit * y;
 85       this.ctx.lineto(dx, dy);
 86       stack.push([dx, dy]);
 87     }
 88     // 获取初始点和结束点
 89     var startp = stack[0];
 90     var endp = stack[stack.length - 1];
 91     this.ctx.lineto(xoffset + axislength, ow);
 92     this.ctx.lineto(xoffset, ow);
 93     this.ctx.lineto(startp[0], startp[1]);
 94     this.ctx.fillstyle = wc;
 95     this.ctx.fill();
 96     this.ctx.restore();
 97   };
 98   this.drawtext = function() {
 99     this.ctx.globalcompositeoperation = "source-over";
100     var size = 0.4 * cr;
101     this.ctx.font = "bold " + size + "px microsoft yahei";
102 
103     if (typeof text == "number") {
104       txt = (that.nowdata.tofixed(2) * 100).tofixed(0) + "%";
105     } else {
106       txt = text;
107     }
108 
109     var fonty = r + size / 2;
110     var fontx = r - size * 0.8;
111     this.ctx.fillstyle = textc;
112     this.ctx.textalign = "center";
113     this.ctx.filltext(txt, r + 5, r + 20);
114   };
115   //最外面淡黄色圈
116   this.drawcircle = function() {
117     this.ctx.beginpath();
118     this.ctx.linewidth = 15;
119     this.ctx.strokestyle = progress;
120     this.ctx.arc(r, r, cr - 6.1, 0, 2 * math.pi);
121     this.ctx.stroke();
122     this.ctx.restore();
123   };
124   //灰色圆圈
125   this.graycircle = function() {
126     this.ctx.beginpath();
127     this.ctx.linewidth = 1;
128     this.ctx.strokestyle = "transparent";
129     this.ctx.arc(r, r, cr + 10, 0, 2 * math.pi);
130     this.ctx.stroke();
131     this.ctx.restore();
132     this.ctx.beginpath();
133   };
134   //橘黄色进度圈
135   this.orangecircle = function() {
136     this.ctx.beginpath();
137     this.ctx.strokestyle = "#b0baee";
138     //使用这个使圆环两端是圆弧形状
139     this.ctx.linecap = "round";
140     this.ctx.arc(
141       r,
142       r,
143       cr - 4,
144       0 * (math.pi / 180.0) - math.pi / 2,
145       that.nowdata * 360 * (math.pi / 180.0) - math.pi / 2
146     );
147     this.ctx.stroke();
148     this.ctx.save();
149   };
150   //裁剪中间水圈
151   this.clipcircle = function() {
152     this.ctx.beginpath();
153     this.ctx.arc(r, r, cr - 10, 0, 2 * math.pi, false);
154     this.ctx.clip();
155   };
156   var that = this;
157   this.render = function() {
158     that.ctx.clearrect(0, 0, ow, oh);
159     //最外面淡黄色圈
160     that.drawcircle();
161     //灰色圆圈
162     that.graycircle();
163     //橘黄色进度圈
164     that.orangecircle();
165     //裁剪中间水圈
166     that.clipcircle();
167     // 控制波幅
168 
169     if (that.data >= 0.85) {
170       if (nowrange > range / 4) {
171         var t = range * 0.01;
172         nowrange -= t;
173       }
174     } else if (that.data <= 0.1) {
175       if (nowrange < range * 1.5) {
176         var t = range * 0.01;
177         nowrange += t;
178       }
179     } else {
180       if (nowrange <= range) {
181         var t = range * 0.01;
182         nowrange += t;
183       }
184       if (nowrange >= range) {
185         var t = range * 0.01;
186         nowrange -= t;
187       }
188     }
189     if (that.data - that.nowdata > 0) {
190       that.nowdata += waveupsp;
191     }
192     if (that.data - that.nowdata < 0) {
193       that.nowdata -= waveupsp;
194     }
195     sp += 0.07;
196     // 开始水波动画
197     that.drawsine();
198     // 写字
199     if (text != "") {
200       that.drawtext();
201     }
202     requestanimationframe(that.render);
203   };
204   this.render();
205 
206   //渲染canvas
207 }

最后附上我的实际最后的效果

基于面向对象思想封装一个水球插件