[Canvas绘图] 第08节 "8"解九妹
2015-12-01 16:39
260 查看
本节目标:
(1) 绘制数字
实现步骤:
电子数字中笔画最多的是"8"字,把它绘制出来后,其它九个数字可以通过减笔画快速生成,包装一个Digit类.
这样就可以调用:
得到结果:
但是这样不适合用作循环,所以我又加了一个封装函数:
可以这样调用:
结果图:
这节就到这里。
(1) 绘制数字
实现步骤:
电子数字中笔画最多的是"8"字,把它绘制出来后,其它九个数字可以通过减笔画快速生成,包装一个Digit类.
/** * @usage 数字形状 * @author mw * @date 2015年12月01日 星期二 15:57:45 * @param * @return * */ function Digit() { var shape = new Shape(); //8 this.eight = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.one = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 //shape.fillRect(x, y, h, w0); //中横 //shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 //shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 //shape.fillRect(x, y-h, h, w0); //上横 //shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.two = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 //shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 shape.fillRect(x-w, y+w, w0, h); //下左竖 //shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.three = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 //shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 //shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.four = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 //shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 //shape.fillRect(x, y-h, h, w0); //上横 //shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.five = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 shape.fillRect(x-w, y-w, w0, h); //上左竖 //shape.fillRect(x+w, y-w, w0, h); //上右竖 //shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.six = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 shape.fillRect(x-w, y-w, w0, h); //上左竖 //shape.fillRect(x+w, y-w, w0, h); //上右竖 shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.seven = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 //shape.fillRect(x, y, h, w0); //中横 //shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 //shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 //shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.nine = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 shape.fillRect(x, y, h, w0); //中横 shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 //shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } this.zero = function(x, y, r) { plot.save(); var h = r * 0.45; //字半高 var w = h / 2.2; //字半宽 var w0 = r *0.1; //填充宽 /* shape.strokeRect(x, y, h, w0); //中横 shape.strokeRect(x-w, y-w, w0, h); //上左竖 shape.strokeRect(x+w, y-w, w0, h); //上右竖 shape.strokeRect(x-w, y+w, w0, h); //下左竖 shape.strokeRect(x+w, y+w, w0, h); //下右竖 shape.strokeRect(x, y-h, h, w0); //上横 shape.strokeRect(x, y+h, h, w0); //下横 */ //填充 //shape.fillRect(x, y, h, w0); //中横 shape.fillRect(x-w, y-w, w0, h); //上左竖 shape.fillRect(x+w, y-w, w0, h); //上右竖 shape.fillRect(x-w, y+w, w0, h); //下左竖 shape.fillRect(x+w, y+w, w0, h); //下右竖 shape.fillRect(x, y-h, h, w0); //上横 shape.fillRect(x, y+h, h, w0); //下横 plot.restore(); } }
这样就可以调用:
var digit = new Digit(); digit.eight(50,20,90); digit.one(100, 20, 90); digit.two(150, 20, 90); digit.three(220, 20, 90); digit.four(270, 20, 90); digit.five(320, 20, 90); digit.six(370, 20, 90); digit.seven(420, 20, 90); digit.nine(470, 20, 90); digit.zero(520, 20, 90);
得到结果:
但是这样不适合用作循环,所以我又加了一个封装函数:
/** * @usage 绘制数字 * @author mw * @date 2015年12月01日 星期二 16:50:23 * @param n [0-9] 要绘制的数字 x, y, 中心点 r 外接圆尺寸 * @return * */ this.number = function(n, x, y, r) { switch (n) { case 0: this.zero(x, y, r); break; case 1: this.one(x, y, r); break; case 2: this.two(x,y,r); break; case 3: this.three(x, y, r); break; case 4: this.four(x,y,r);break; case 5: this.five(x,y,r);break; case 6: this.six(x,y,r); break; case 7: this.seven(x,y,r); break; case 8: this.eight(x,y,r); break; case 9: this.nine(x,y,r); break; default:break; } }
可以这样调用:
var r = 50; for (var i = 0; i < 10; i++) { digit.number(i, i * 50, 50, 50); }
结果图:
这节就到这里。
相关文章推荐
- jQuery多个倒计时的实现
- C#中获取当前时间:System.DateTime.Now.ToString()用法
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)
- 从头认识java-11.3 格式化输出(2)
- JQuery 2次点击实现2次不同的响应
- language model
- jdk jvisualvm 或 jconsole 远程监控 tomcat
- Checkstyle配置文件及常见输出结果
- 2000端口号的坑
- 通过两个GPS计算两个GPS点的距离
- Qt学习(1)
- 【转】Vim 常用命令总结
- shell脚本步骤调试
- Starting MySQL. ERROR! Manager of pid-file quit without updating file.
- [KDD,ML]奇异值分解(SVD)的物理意义
- java 流操作
- Util Yeoman
- 数组根据数组中字典的value值排序
- 通过RTMP协议将AVC(H264)数据发送到流媒体服务器
- Core Data 批量更新(iOS 8 特性)