Coffeescript实现canvas时钟
前言
参照Mozilla 官方教程,要在Canvas上画动画时钟,思路非常有意思。
把动画看作是多个帧组成,定时每个时间点在Canvas上画一帧来实现动画。而Mozilla 官方教程画图实现的思路有意思的地方在于,它喜欢在画布上面做文章,正常的思路,如果要画一条倾斜45度的直线,一般会先用数据计算拿到起始点与末点的坐标,先定位到起点画线到末点;如何在画布上面做文章呢,它先把画布旋转45度,然后直接在中间画一条竖线,再把画布复原,这样直线也跟着倾斜45度了。就按这思路定时在Canvas上画点、线、面实现时钟动画。
页面效果如下
原代码如下
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>canvas时钟 - Coffeescript实现</title>
<script id="others_zepto_10rc1" type="text/javascript" class="library" src="/js/sandbox/other/zepto.min.js"></script>
<script id="others_coffeescript" type="text/javascript" class="library" src="/js/sandbox/other/coffee-script.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<h2>
Coffeescript 练习
</h2>
<p>
Canvas时钟 - Coffeescript实现
</p>
<p>
参照 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations">An animated clock</a>
</p>
</body>
<script type="text/coffeescript">
clock = ->
now = new Date()
ctx = document.getElementById('canvas').getContext '2d'
ctx.save()
ctx.clearRect 0,0,150,150
ctx.translate 75,75
ctx.scale 0.4,0.4
ctx.rotate -Math.PI/2
ctx.strokeStyle = "black"
ctx.fillStyle = "white"
ctx.lineWidth = 8
ctx.lineCap = "round" #画12个小时的标刻
ctx.save()
for i in [0..11]
ctx.beginPath()
ctx.rotate Math.PI/6
ctx.moveTo 100,0
ctx.lineTo 120,0
ctx.stroke()
ctx.restore() #画60个分钟的标刻
ctx.save()
ctx.lineWidth = 5
for i in [0..59]
if i%5 isnt 0
ctx.beginPath()
ctx.moveTo 117,0
ctx.lineTo 120,0
ctx.stroke()
ctx.rotate Math.PI/30
ctx.restore() sec = now.getSeconds()
min = now.getMinutes()
hr = now.getHours()
hr = if hr >= 12 then hr-12 else hr ctx.fillStyle = "black" #画时针
ctx.save()
ctx.rotate hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec
ctx.lineWidth = 14
ctx.beginPath()
ctx.moveTo -20,0
ctx.lineTo 80,0
ctx.stroke()
ctx.restore() #画分针
ctx.save()
ctx.rotate (Math.PI/30)*min + (Math.PI/1800)*sec
ctx.lineWidth = 10
ctx.beginPath()
ctx.moveTo -28,0
ctx.lineTo 112,0
ctx.stroke()
ctx.restore() #画秒针
ctx.save()
ctx.rotate sec * Math.PI/30
ctx.strokeStyle = "#D40000"
ctx.fillStyle = "#D40000"
ctx.lineWidth = 6
ctx.beginPath()
ctx.moveTo -30,0
ctx.lineTo 83,0
ctx.stroke()
ctx.beginPath()
ctx.arc 0,0,10,0,Math.PI*2,true
ctx.fill()
ctx.beginPath()
ctx.arc 95,0,10,0,Math.PI*2,true
ctx.stroke()
ctx.fillStyle = "rgba(0,0,0,0)"
ctx.arc 0,0,3,0,Math.PI*2,true
ctx.fill()
ctx.restore() #画钟的外圈
ctx.beginPath()
ctx.lineWidth = 14
ctx.strokeStyle = '#325FA2'
ctx.arc 0,0,142,0,Math.PI*2,true
ctx.stroke()
ctx.restore() window.requestAnimationFrame clock
return #启动程序
clock()
</script>
</html>
Coffeescript实现canvas时钟的更多相关文章
- 》》canvas时钟
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 原生js之canvas时钟组件
canvas一直是前端开发中不可或缺的一种用来绘制图形的标签元素,比如压缩上传的图片.比如刮刮卡.比如制作海报.图表插件等,很多人在面试的过程中也会被问到有没有接触过canvas图形绘制. 定义 ca ...
- HTML5之Canvas时钟(网页效果--每日一更)
今天,带来的是使用HTML5中Canvas标签实现的动态时钟效果. 话不多说,先看效果:亲,请点击这里 众所周知,Canvas标签是HTML5中的灵魂,HTML5 Canvas是屏幕上的一个由Java ...
- Canvas - 时钟绘制
导语:距离上一次写canvas,已经过去两年半,如今业务需要,再次拾起,随手记录. [思考] 时钟的绘制主要在于圆的绘制:1. 使用context.arc()方法直接绘制圆或圆弧: 2. 使用圆的方程 ...
- html5 canvas时钟
基础知识点: canvas标签只是图形容器,您必须使用脚本来绘制图形. getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性.——获取上 ...
- canvas时钟效果
话不多说,直接上代码 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/x ...
- HTML5 Canvas 时钟
1. [图片] QQ截图20120712130049.png 2. [代码][HTML]代码 <!DOCTYPE html><html lang="en" &g ...
- 简单的canvas时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- canvas 时钟+自由落体
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
随机推荐
- 关于li元素嵌套的事儿
今天阅读<锋利的jQuery>第二版2.6节案例研究部分的时候,遇到一个问题. <ul> <li class="a1"><a href=& ...
- setprecision **fixed
#include <iostream> #include <iomanip> using namespace std; int main( void ) { const dou ...
- Typescript基础类型
1.布尔值__boolean 2.数字__number----除了支持十进制和十六进制字面量,Typescript还支持ECMAScript 2015中引入的二进制和八进制字面量. 3.字符串__st ...
- java并行计算Fork和Join的使用
Java在JDK7之后加入了并行计算的框架Fork/Join,可以解决我们系统中大数据计算的性能问题.Fork/Join采用的是分治法,Fork是将一个大任务拆分成若干个子任务,子任务分别去计算,而J ...
- VC++ : error LNK2005: ... already defined in *.obj
今天写代码遇到了这么一个链接错误:"已经在*.obj中定义". error LNK2005: "void __cdecl ReplaceWstringVar(class ...
- redhat 配置本地yum源163yum源epel 源,无需卸载yum!无须拷贝ISO,愿网上少一点垃圾教程误人子弟
都知道redhat不收费,但是其yum服务是要收费的,不想出钱那就自己配置yum源就好了. 首先,博主之前也没用过redhat,第一次用yum装包的时候提示什么没注册之类的,balaba一大堆,然后就 ...
- Java数据类型和MySql数据类型对应表
- 网页端打开手机上的app
iOS/Android 浏览器(h5)及微信中唤起本地APP 在移动互联网,链接是比较重要的传播媒质,但很多时候我们又希望用户能够回到APP中,这就要求APP可以通过浏览器或在微信中被方便地唤起. 这 ...
- 【转载】maven插件mybatis-generator自动生成 (1)
http://blog.csdn.net/itlqi/article/details/49534447 1.新建一个maven项目在pom.xml添加如下: <plugins> <p ...
- vmware 下centos7配置网络
步骤一: 虚拟机中的网络设置配置为桥接模式: 步骤二: 注:本人配置的为非静态IP,ip为自动获取 vi /etc/sysconfig/network-scripts/ifcfg-eth0 配置内容如 ...