离屏Canvas

离屏效果:


离屏非离屏并存效果


[javascript]  view plain  copy
  1. <!DOCTYPE html>  
  2. <!--     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  3. -->  
  4. <html>  
  5.    <head>  
  6.      <title>Image Clock</title>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  8.       <style>   
  9.          body {  
  10.             background: #dddddd;  
  11.          }  
  12.  
  13.          #canvas {  
  14.             display: none  
  15.          }  
  16.  
  17.          #snapshotImageElement {  
  18.             position: absolute;  
  19.             left: 10px;  
  20.             margin: 20px;  
  21.             border: thin solid #aaaaaa;  
  22.          }  
  23.       </style>  
  24.    </head>  
  25.   
  26.   <body>  
  27.     <img id='snapshotImageElement'/>  
  28.     <canvas id='canvas' width='400' height='400'>  
  29.       Canvas not supported  
  30.     </canvas>  
  31.     <script>  
  32.         var canvas = document.getElementById('canvas'),  
  33.     context = canvas.getContext('2d'),  
  34.     snapshotButton = document.getElementById('snapshotButton'),  
  35.     snapshotImageElement = document.getElementById('snapshotImageElement'),  
  36.     FONT_HEIGHT = 15,  
  37.     MARGIN = 35,  
  38.     HAND_TRUNCATION = canvas.width/25,  
  39.     HOUR_HAND_TRUNCATION = canvas.width/10,  
  40.     NUMERAL_SPACING = 20,  
  41.     RADIUS = canvas.width/2 - MARGIN,  
  42.     HAND_RADIUS = RADIUS + NUMERAL_SPACING,  
  43.     loop;  
  44.   
  45.   
  46.   
  47. function drawCircle() {  
  48.    context.beginPath();  
  49.    context.arc(canvas.width/2, canvas.height/2, RADIUS, 0, Math.PI*2, true);  
  50.    context.stroke();  
  51. }  
  52.      
  53. function drawNumerals() {  
  54.    var numerals = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ],  
  55.        angle = 0,  
  56.        numeralWidth = 0;  
  57.   
  58.    numerals.forEach(function(numeral) {  
  59.       angle = Math.PI/6 * (numeral-3);  
  60.       numeralWidth = context.measureText(numeral).width;  
  61.       context.fillText(numeral,   
  62.          canvas.width/2  + Math.cos(angle)*(HAND_RADIUS) - numeralWidth/2,  
  63.          canvas.height/2 + Math.sin(angle)*(HAND_RADIUS) + FONT_HEIGHT/3);  
  64.    });  
  65. }  
  66.   
  67. function drawCenter() {  
  68.    context.beginPath();  
  69.    context.arc(canvas.width/2, canvas.height/2, 5, 0, Math.PI*2, true);  
  70.    context.fill();  
  71. }  
  72.   
  73. function drawHand(loc, isHour) {  
  74.    var angle = (Math.PI*2) * (loc/60) - Math.PI/2,  
  75.        handRadius = isHour ? RADIUS-HAND_TRUNCATION-HOUR_HAND_TRUNCATION   
  76.                            : RADIUS-HAND_TRUNCATION;  
  77.   
  78.    context.moveTo(canvas.width/2, canvas.height/2);  
  79.    context.lineTo(canvas.width/2  + Math.cos(angle)*handRadius,   
  80.                   canvas.height/2 + Math.sin(angle)*handRadius);  
  81.    context.stroke();  
  82. }  
  83.   
  84. function drawHands() {  
  85.    var date = new Date,  
  86.        hour = date.getHours();  
  87.    hour = hour > 12 ? hour - 12 : hour;  
  88.    drawHand(hour*5 + (date.getMinutes()/60)*5, true, 0.5);  
  89.    drawHand(date.getMinutes(), false, 0.5);  
  90.    drawHand(date.getSeconds(), false, 0.2);  
  91. }  
  92.   
  93. //每隔一秒抓取一次canvas快照,并更新img的src属性  
  94. function updateClockImage() {  
  95.    dataUrl = canvas.toDataURL();  
  96.    snapshotImageElement.src = dataUrl;  
  97. }  
  98.   
  99. //每秒执行一次绘制,canvas已隐藏,每秒更新一次img的src属性  
  100. function drawClock() {  
  101.    context.clearRect(0,0,canvas.width,canvas.height);  
  102.   
  103.    context.save();  
  104.   
  105.    context.fillStyle = 'rgba(255,255,255,0.8)';  
  106.    context.fillRect(0, 0, canvas.width, canvas.height);  
  107.   
  108.    drawCircle();  
  109.    drawCenter();  
  110.    drawHands();  
  111.   
  112.    context.restore();  
  113.   
  114.    drawNumerals();  
  115.   
  116.    updateClockImage();  
  117. }  
  118.   
  119.   
  120. context.font = FONT_HEIGHT + 'px Arial';  
  121. loop = setInterval(drawClock, 1000);  
  122.           
  123.     </script>  
  124.   </body>  
  125. </html>  

离屏演示地址:http://sandbox.runjs.cn/show/lgymk7zo

离屏非离屏并存效果:http://sandbox.runjs.cn/show/uzxeuq4o

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值