html5 canvas实现圆形时钟实例代码
代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML CLOCK</title> </head> <body> <canvas width="500" height="500" id="clock"> 你的浏览器不支持canvas标签,时针显示不出来哦! </canvas> <script type="text/javascript"> //画布背景颜色 var clockBackGroundColor = "#ABCDEF"; //时针颜色 var hourPointColor = "#000"; //时针粗细 var hourPointWidth = 7; //时针长度 var hourPointLength = 100; //分针颜色 var minPointColor = "#000"; //分针粗细 var minPointWidth = 5; //分针长度 var minPointLength = 150; //秒针颜色 var secPointColor = "#F00"; //秒针粗细 var secPointWidth = 3; //秒针长度 var secPointLength = 170; //表盘颜色 var clockPanelColor = "#ABCDEF"; //表盘刻度颜色 var scaleColor = "#000"; //表盘大刻度宽度 3 6 9 12 var scaleBigWidth = 9; //表盘大刻度的长度 var scaleBigLength = 15; //表盘小刻度的宽度 var scaleSmallWidth = 5; //表盘小刻度的长度 var scaleSmallLength = 10; //圆心颜色 var centerColor = 'red'; //时钟画布 var clock = document.getElementById('clock'); clock.style.background = clockBackGroundColor; //时针画布的作图环境(画板) var panel = clock.getContext('2d'); //画线 /** *画线段 * * */ function drowLine(p,width,color,startX,startY,endX,endY,ran,cX,cY){ //保存传入的画板,相当于每次作画新开一个图层 p.save(); //设置画笔宽度 p.lineWidth = width; //设置画笔颜色 p.strokeStyle = color; //新开启作图路径,避免和之前画板上的内容产生干扰 p.beginPath(); p.translate(cX,cY); //旋转 p.rotate(ran); //移动画笔到开始位置 p.moveTo(startX,startY); //移动画笔到结束位置 p.lineTo(endX,endY); //画线操作 p.stroke(); //关闭作图路径,避免和之后在画板上绘制的内容产生干扰 p.closePath(); //在传入的画板对象上覆盖图层 p.restore(); } /** *画水平线 */ function drowHorizontalLine(p,width,length,color,startX,startY,ran,cX,cY){ drowLine(p,width,color,startX,startY,startX+length,startY,ran,cX,cY); } /** *画圈圈 */ function drowCircle(p,width,color,centreX,centreY,r){ p.save(); //设置画笔宽度 p.lineWidth = width; //设置画笔颜色 p.strokeStyle = color; //新开启作图路径,避免和之前画板上的内容产生干扰 p.beginPath(); //画圈圈 p.arc(centreX,centreY,r,0,360,false); //画线操作 p.stroke(); //关闭作图路径,避免和之后在画板上绘制的内容产生干扰 p.closePath(); //在传入的画板对象上覆盖图层 p.restore(); } function drowPoint(p,width,color,centreX,centreY,r){ p.save(); //设置画笔宽度 p.lineWidth = width; //设置画笔颜色 p.fillStyle = color; //新开启作图路径,避免和之前画板上的内容产生干扰 p.beginPath(); //画圈圈 p.arc(centreX,centreY,r,0,360,false); //画线操作 p.fill(); //关闭作图路径,避免和之后在画板上绘制的内容产生干扰 p.closePath(); //在传入的画板对象上覆盖图层 p.restore(); } function drowScales(p){ //画小刻度 for(var i = 0;i < 60;i++){ drowHorizontalLine(p,scaleSmallWidth,scaleSmallLength,scaleColor,195-scaleSmallLength,0,i*6*Math.PI/180,250,250); } //画大刻度 for(var i = 0;i < 12;i++){ drowHorizontalLine(p,i%3==0?scaleBigWidth*1.2:scaleBigWidth,i%3==0?scaleBigLength*1.2:scaleBigLength,scaleColor,195-scaleBigLength,0,i*30*Math.PI/180,250,250); //可以添加数字刻度 } } function drowHourPoint(p,hour){ drowHorizontalLine(p,hourPointWidth,hourPointLength,hourPointColor,-10,0,(hour-3)*30*Math.PI/180,250,250); } function drowMinPoint(p,min){ drowHorizontalLine(p,minPointWidth,minPointLength,minPointColor,-15,0,(min-15)*6*Math.PI/180,250,250); } function drowSecPoint(p,sec){ drowHorizontalLine(p,secPointWidth,secPointLength,secPointColor,-15,0,(sec-15)*6*Math.PI/180,250,250); } function drowClock(){ panel.clearRect(0,0,500,500); panel.fillText("",10,20); panel.fillText("<a href="http://www.jb51.net",10,40">http://www.jb51.net",10,40</a>); var date = new Date(); var sec = date.getSeconds(); var min = date.getMinutes(); var hour = date.getHours() + min/60; drowCircle(panel,7,'blue',250,250,200); drowScales(panel); drowHourPoint(panel,hour); drowMinPoint(panel,min); drowSecPoint(panel,sec); drowPoint(panel,1,centerColor,250,250,7); //drowHorizontalLine(panel,10,10,'red',-5,0,0,250,250); } //drowHorizontalLine(panel,7,30,'#F00',0,0,Math.PI,250,250); drowClock(); setInterval(drowClock,1000); function save(){ var image = clock.toDataURL("image/png").replace("image/png", "image/octet-stream"); location.href=image; } </script> </body> </html>
【相关推荐】
1. HTML5开发手机应用-详细介绍viewport的作用(图文)
3. HTML5 程序设计

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Schools using canvas include Stanford University, MIT, Columbia University, University of California, Berkeley, etc. Detailed introduction: 1. Stanford University uses Canvas as its main online learning platform. Teachers and students at Stanford University use Canvas to manage and communicate course content, and learn through functions such as online discussions, assignment submissions, and exams; 2. Ma Provincial Polytechnic Institute and MIT also use Canvas as their online learning management system and conduct course management through the Canvas platform; 3. Columbia University, etc.

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

How to use canvas to draw charts and animation effects in uniapp requires specific code examples 1. Introduction With the popularity of mobile devices, more and more applications need to display various charts and animation effects on the mobile terminal. As a cross-platform development framework based on Vue.js, uniapp provides the ability to use canvas to draw charts and animation effects. This article will introduce how uniapp uses canvas to achieve chart and animation effects, and give specific code examples. 2. canvas

The tkinter canvas attributes include bg, bd, relief, width, height, cursor, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertwidth, selectbackground, selectforeground, xscrollcommand attributes, etc. Detailed introduction

How to get mouse coordinates for canvas: 1. Create a JavaScript sample file; 2. Get a reference to the Canvas element and add a listener for mouse movement events; 3. When the mouse moves on the Canvas, the getMousePos function will be triggered; 4. Use The "getBoundingClientRect()" method obtains the position and size information of the Canvas element, and obtains the mouse coordinates through event.clientX and event.clientY.
