Home Web Front-end H5 Tutorial HTML5 canvas implements sample code sharing of draggable clock

HTML5 canvas implements sample code sharing of draggable clock

Mar 25, 2017 pm 03:52 PM

The following introduces a small clock that I made myself, which is convenient for dragging.

So how to insert it into your own interface?

Just add the following code:

<script type="text/javascript" src="clock.js">
</script>
Copy after login

The complete code of clcok.js is as follows:

var extra=document.createElement(&#39;p&#39;);
    extra.style.position=&#39;absolute&#39;;
    var extra_canvas=document.createElement(&#39;canvas&#39;);
    extra_canvas.id="extra_canvas";
    extra.appendChild(extra_canvas);
    document.body.appendChild(extra);

    var flag;
    var currentRectX;
    var currentRectY;
  
  function init(){
     flag=true;
     currentRectX=0;
     currentRectY=0;
    }

    

function clock(size,x,y){
  /*    if(!flag){
          document.body.removeChild(extra_canvas);
          console.log(&#39;remove&#39;);
        }
  
  */
  if (!size){size=200;}
  if (!x)x=0;
  if (!y)y=0;
  extra_canvas.width=size;
  extra_canvas.height=size;
  var context=extra_canvas.getContext(&#39;2d&#39;);
  
  extra.style.left=currentRectX+&#39;px&#39;;
  extra.style.top=currentRectY+&#39;px&#39;;
  //console.log(currentRectX,currentRectY);
  //context.fillStyle=&#39;#FF0000&#39;;
  //context.fillRect(0,0,100,100);
  //绘制表盘
      var centerX=x+size/2;
      var centerY=y+size/2;
    //console.log(centerX,centerY);
      var radius=(size-40)/2;
  context.clearRect(x,y,x+size,y+size);
    context.beginPath();
    context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    context.lineWidth = 5;
  context.strokeStyle = "grey";
  context.stroke();
  var grd = context.createLinearGradient(centerX-radius, centerY-radius,centerX+radius, centerY+radius);
  grd.addColorStop(0, "#FFFFFF"); // light blue
  grd.addColorStop(1, "#DDDDFF"); // dark blue
  context.fillStyle = grd;
  context.fill();
  context.closePath();

  //绘制数字时刻
  context.beginPath();
  //context.font="9pt";
 // context.fontsize=9px;
  //context.fontFamily="Square721 BT";
  context.fillStyle = "#0000f0"; // text color
  context.fillText("12",centerX-7,centerY-radius+18);
  context.fillText("3",centerX+radius-18,centerY+4);
  context.fillText("6",centerX-3,centerY+radius-12);
  context.fillText("9",centerX-radius+12,centerY+4);
  
  context.closePath();
/*  //显示日期
context.beginPath();
context.TextOut(100,100,"星期");
context.closePath();
*/
  //绘制刻度
  for (var i=0;i<12;i++){
      context.beginPath();
    if(i%3){
           context.lineWidth = 3;
            context.strokeStyle = "grey";
            len=5;
        }else{
            context.lineWidth = 6;
            context.strokeStyle = "#ff0";
            len=10;
        }
       arc=i/6*Math.PI;
       kx=centerX+radius*Math.cos(arc);
       ky=centerY-radius*Math.sin(arc);
       context.moveTo(kx,ky);
       kx=centerX+(radius-len)*Math.cos(arc);
       ky=centerY-(radius-len)*Math.sin(arc);
       context.lineTo(kx,ky);
       
       context.stroke();
       context.closePath();
      }
  //绘制表中心
     context.beginPath();
     context.arc(centerX, centerY, 4, 0, 2 * Math.PI, false);
     context.lineWidth = 2;
     context.strokeStyle = "grey";
     context.stroke();
     context.closePath();
     
  //绘制时针分针
  
  var now=new Date();
  var hour=now.getHours()%12;
  var minute=now.getMinutes();
  var second=now.getSeconds();
  hour=hour+minute/60;//update the time!!
  minute=minute+second/60;

  var arc_hour=hour/6*Math.PI;
  context.beginPath();
  kx=centerX+(radius-40)*Math.sin(arc_hour);
  ky=centerY-(radius-40)*Math.cos(arc_hour);
  context.moveTo(kx,ky);
  kx=centerX+10*Math.sin(arc_hour+Math.PI);
  ky=centerY-10*Math.cos(arc_hour+Math.PI);
  context.lineTo(kx,ky);
  context.lineWidth = 6;
  context.strokeStyle = "black";
  context.stroke();
  context.closePath();
  
  context.beginPath();
  var arc_minute=minute/30*Math.PI;
  context.beginPath();
  kx=centerX+(radius-20)*Math.sin(arc_minute);
  ky=centerY-(radius-20)*Math.cos(arc_minute);
  context.moveTo(kx,ky);
  kx=centerX+20*Math.sin(arc_minute+Math.PI);
  ky=centerY-20*Math.cos(arc_minute+Math.PI);
  context.lineTo(kx,ky);
  context.lineWidth = 4;
    context.strokeStyle = "red";
  context.stroke();
  context.closePath();
//  flag=false;
  context.beginPath();
  var arc_second=second/30*Math.PI;
  context.beginPath();
  kx=centerX+(radius-20)*Math.sin(arc_second);
  ky=centerY-(radius-20)*Math.cos(arc_second);
  context.moveTo(kx,ky);
  kx=centerX+20*Math.sin(arc_second+Math.PI);
  ky=centerY-20*Math.cos(arc_second+Math.PI);
  context.lineTo(kx,ky);
  context.lineWidth = 2;
    context.strokeStyle = "gray";
  context.stroke();
  context.closePath();

}
/*
extra.onmousedown=null;
extra.onmouseup=null; 
extra.onmousemove=null;
*/
extra_canvas.onmousedown=canvasMouseDownHandler;
extra_canvas.onmouseup=canvasMouseUpHandler; 
function canvasMouseDownHandler(event){
    var canvasMouseX=event.screenX;
    var canvasMouseY=event.screenY;
    extra_canvas.onmousemove=canvasMouseMoveHandler;
  //  console.log(&#39;down&#39;);
    startDragMouseX=canvasMouseX;
    startDragMouseY=canvasMouseY;
    startDragRectX=currentRectX;
    startDragRectY=currentRectY;
  }
  function canvasMouseMoveHandler(event){
      event.preventDefault(); 
    var canvasMouseX=event.screenX;
    var canvasMouseY=event.screenY;
   // console.log(&#39;move&#39;);
    currentRectX=startDragRectX+canvasMouseX-startDragMouseX;
    currentRectY=startDragRectY+canvasMouseY-startDragMouseY;
    //console.log(currentRectX,currentRectY);
    clock();
  }

  function canvasMouseUpHandler(event){
    extra_canvas.onmousemove=null;
    //console.log(&#39;up&#39;);
  }
/*  function cc(){
       clock(null,0,0);
}
*/
//window.setInterval(cc, 200);
init();
window.setInterval(clock, 200);
Copy after login

The above is the detailed content of HTML5 canvas implements sample code sharing of draggable clock. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles