HTML5时钟-柯乐义 柯乐义 原文 HTML5时钟 柯乐义提示您,请使用支持HTML5的浏览器,例如Chrome,IE9,IE10,Firefox等。 <br>var canvas = document.getElementById('canvas'); <br>var ctx = canvas.getContext('2d'); <br>if (ctx) { <br>var 타이머Id; <br>var 프레임레이트 = 60; <br>function canvObject() { <br>this.x = 0; <br>this.y = 0; <br>this.rotation = 0; <br>this.borderWidth = 2; <br>this.borderColor = '#000000'; <br>this.fill = false; <br>this.fillColor = '#ff0000'; <br>this.update = function () { <br>if (!this.ctx) throw new Error('柯乐义提示:您没指定ctx对象。'); <br>var ctx = this.ctx <br>ctx.save(); <br>ctx.lineWidth = this.borderWidth; <br>ctx.StrokeStyle = this.borderColor; <br>ctx.fillStyle = this.fillColor; <br>ctx.translate(this.x, this.y); <br>if (this.rotation) ctx.rotate(this.rotation * Math.PI / 180); <br>if (this.draw) this.draw(ctx); <br>if (this.fill) ctx.fill(); <br>ctx.Stroke(); <br>ctx.restore(); <br>} <br>}; <br>함수 라인() { }; <br>Line.prototype = new canvObject(); <br>Line.prototype.fill = false; <br>Line.prototype.start = [0, 0]; <br>Line.prototype.end = [5, 5]; <br>Line.prototype.draw = function (ctx) { <br>ctx.beginPath(); <br>ctx.moveTo.apply(ctx, this.start); <br>ctx.lineTo.apply(ctx, this.end); <br>ctx.closePath(); <br>}; <br><br>function Circle() { }; <br>Circle.prototype = new canvObject(); <br>Circle.prototype.draw = function (ctx) { <br>ctx.beginPath(); <br>ctx.arc(0, 0, this.radius, 0, 2 * Math.PI, true); <br>ctx.closePath(); <br>}; <br><br>var Circle = new Circle(); <br>circle.ctx = ctx; <br>circle.x = 100; <br>circle.y = 100; <br>circle.radius = 90; <br>circle.fill = true; <br>circle.borderWidth = 6; <br>circle.fillColor = '#ffffff'; <br><br>var hour = new Line(); <br>시간.ctx = ctx; <br>시간.x = 100; <br>시간.y = 100; <br>hour.borderColor = "#000000"; <br>hour.borderWidth = 10; <br>시간.회전 = 0; <br>시간.시작 = [0, 20]; <br>hour.end = [0, -50]; <br><br>var 분 = new Line(); <br>분.ctx = ctx; <br>분.x = 100; <br>분.y = 100; <br>분.borderColor = "#333333"; <br>분.borderWidth = 7; <br>분.회전 = 0; <br>분.시작 = [0, 20]; <br>분.end = [0, -70]; <br><br>var second = new Line(); <br>초.ctx = ctx; <br>초.x = 100; <br>초.y = 100; <br>seconds.borderColor = "#ff0000"; <br>seconds.borderWidth = 4; <br>초.회전 = 0; <br>초.시작 = [0, 20]; <br>초.end = [0, -80]; <br><br>var center = new Circle(); <br>center.ctx = ctx; <br>center.x = 100; <br>center.y = 100; <br>center.radius = 5; <br>center.fill = true; <br>center.borderColor = '주황색'; <br><br>for (var i = 0, ls = [], 캐시; i < 12; i++) { <br/>cache = ls[i] = new Line(); <br/>cache.ctx = ctx; <br/>cache.x = 100; <br/>cache.y = 100; <br/>cache.borderColor = "주황색"; <br/>cache.borderWidth = 2; <br/>cache.rotation = i * 30; <br/>cache.start = [0, -70]; <br/>cache.end = [0, -80]; <br/>} <br/><br/>timerId = setInterval(function () { <br/>// 清除画布 <br/>ctx.clearRect(0, 0, 200, 200); <br/>// 填充背景color <br/>ctx.fillStyle = 'orange'; <br/>ctx.fillRect(0, 0, 200, 200); <br/>// 表盘 <br/>circle.update(); for (var i = 0; 캐시 = ls[i++]; ) 캐시.update(); <br/>// 时针 <br/>hour.rotation = (new Date()).getHours() * 30; <br/> hour.update(); <br/>// 分针 <br/> Minute.rotation = (new Date()).getMinutes() * 6; <br/> Minute.update(); <br/>// 秒针 <br/> second.rotation = (new Date()).getSeconds() * 6; <br/>seconds.update(); <br/>// 中心圆 <br/>center.update(); <br/>} 프레임 속도) 0); <br/>} else { <br/>alert('柯乐义提示:您的浏览器不支持HTML5无法预览.'); <br/>} <br/>