canvas一直是前端开发中不可或缺的一种用来绘制图形的标签元素,比如压缩上传的图片、比如刮刮卡、比如制作海报、图表插件等,很多人在面试的过程中也会被问到有没有接触过canvas图形绘制。
定义
canvas元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成。
canvas标签只是图形容器,您必须使用脚本来绘制图形。
浏览器支持
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持
那么本篇文章就通过一个时钟组件来熟悉使用一下关于canvas的api。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>canvas时钟</title> <style> *{margin:0;padding:0;} body{text-align:center;padding-top:100px;} </style> </head> <body> <canvas id="clock" width="200px" height="200px"></canvas> <script> (function(win){ function DrawClock(options){ this.canvas = options.el; this.ctx = this.canvas.getContext('2d');//方法返回一个用于在画布上绘图的环境 this.width = this.ctx.canvas.width; this.height = this.ctx.canvas.height; this.r = this.width / 2; this.rem = this.width / 200; this.digits = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]; var self = this; self.init(); setInterval(function(){ self.init(); }, 1000); } DrawClock.prototype = { init: function(){ var ctx = this.ctx; ctx.clearRect(0, 0, this.width, this.height); //在给定的矩形内清除指定的像素 var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var hour = hours >= 12 "px Arial";//设置或返回文本内容的当前字体属性 ctx.textAlign = "center"; //设置或返回文本内容的当前对齐方式 ctx.textBaseline = "middle"; //设置或返回在绘制文本时使用的当前文本基线 this.digits.forEach(function(number, i){ var rad = 2 * Math.PI / 12 * i; var x = Math.cos(rad) * (self.r - 33 * self.rem); var y = Math.sin(rad) * (self.r - 33 * self.rem); ctx.fillText(number, x, y); //在画布上绘制"被填充的"文本 }); //分钟的刻度,每分钟转6deg for (var i = 0; i < 60; i++){ ctx.save(); //保存当前环境的状态 ctx.rotate(6 * i * Math.PI / 180); //旋转当前绘图 ctx.beginPath(); //起始一条路径,或重置当前路径 ctx.moveTo(0, -82 * this.rem); //把路径移动到画布中的指定点,不创建线条 ctx.lineTo(0, -87 * this.rem); //添加一个新点,然后在画布中创建从该点到最后指定点的线条 ctx.closePath(); //创建从当前点回到起始点的路径 ctx.strokeStyle = '#000'; //设置或返回用于笔触的颜色、渐变或模式 ctx.lineWidth = 1 * this.rem; //设置或返回当前的线条宽度 ctx.stroke(); //绘制已定义的路径 ctx.restore(); //返回之前保存过的路径状态和属性 } //小时的刻度,每小时转30deg for (var i = 0; i < 12; i++){ ctx.save(); ctx.rotate(30 * i * Math.PI / 180); ctx.beginPath(); ctx.moveTo(0, -79 * this.rem); ctx.lineTo(0, -87 * this.rem); ctx.closePath(); ctx.strokeStyle = '#000'; ctx.lineWidth = 2 * this.rem; ctx.stroke(); ctx.restore(); } }, drawHour: function(hour, minute){ var ctx = this.ctx; ctx.save(); ctx.beginPath(); var hRad = 2 * Math.PI / 12 * hour; var mRad = 2 * Math.PI / 12 / 60 * minute; ctx.rotate(hRad + mRad); ctx.lineWidth = 6 * this.rem; ctx.lineCap = "round"; //设置或返回线条的结束端点样式 ctx.moveTo(0, 10 * this.rem); ctx.lineTo(0, -this.r / 2); ctx.stroke(); ctx.restore(); }, drawMinute: function(minute){ var ctx = this.ctx; ctx.save(); ctx.beginPath(); var rad = 2 * Math.PI / 60 * minute; ctx.rotate(rad); ctx.lineWidth = 3 * this.rem; ctx.lineCap = "round"; ctx.moveTo(0, 10 * this.rem); ctx.lineTo(0, -this.r + 26 * this.rem); ctx.stroke(); ctx.restore(); }, drawSecond: function(second){ var ctx = this.ctx; ctx.save(); ctx.beginPath(); ctx.fillStyle = "#c14543"; var rad = 2 * Math.PI / 60 * second; ctx.rotate(rad); ctx.moveTo(-3 * this.rem, 20 * this.rem); ctx.lineTo(3 * this.rem, 20 * this.rem); ctx.lineTo(1, -this.r + 26 * this.rem); ctx.lineTo(-1, -this.r + 26 * this.rem); ctx.fill(); //填充当前绘图(路径) ctx.restore(); }, drawDot: function(minute){ var ctx = this.ctx; ctx.beginPath(); ctx.fillStyle = "#fff"; ctx.arc(0, 0, 3 * this.rem, 0, 2 * Math.PI, false); ctx.fill(); } }; win.DrawClock = DrawClock; })(window); new DrawClock({el: document.getElementById("clock")}); </script> </body> </html>
只要心中有丘壑,就能耕出二亩田!canvas时钟用到了canvas中大部分的api,通过学习canvas时钟的代码实现,很能了解canvas的属性和方法,同时,实现时钟效果时,用到了数学中的几何模型正弦sin和余弦cos以及弧度的计算方法,又重温了一把当年学数学时的许多乐趣,可谓是一举两得。
时钟效果图如下:
以上就是js基于canvas实现时钟组件的详细内容,更多关于canvas实现时钟组件的资料请关注其它相关文章!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 《年度十大发烧唱片精选 唱片榜获奖作品 2CD》[WAV/分轨][1GB]
- 归龙潮三枚天灯碎片位置在哪 笼律鸟任务天灯图文位置
- 归龙潮新手开荒阵容怎么搭配 新手前期阵容养成攻略
- 归龙潮中浦城区福鼠位置在哪 中浦城区福鼠收集位置大全
- MatteoPasqualini-JohannSebastianBachItalienischesKonzertFranzsischeOuvertureVierDue
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]
- 王菲.1994-《迷路》美卡引进版[WAV+CUE]
- 老外遇到八戒裸体Bug 网友:我需要用它对付蜘蛛精
- 《黑神话》超强越化补丁:游戏里有文字的地都改了
- 玩家吐槽《黑神话:悟空》唐僧相貌:一脸衰样 没半点气质
- 群星.1994-烧得厉害VOL.2我不是一个人拙滚石】【WAV+CUE】
- 群星.1994-烧得厉害VOL.3风笑痴【滚石】【WAV+CUE】
- 群星.1995-烧得厉害VOL.4两个世界【滚石】【WAV+CUE】
- 归龙潮中浦城区宝箱位置收集大全 中浦区宝箱解密攻略
- 黑神话悟空上品幽灯鬼精魄获取方法一览|上品幽灯鬼精魄收集攻略