Advertisement
here2share

# js_clock.py

Nov 24th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. # js_clock.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''<!DOCTYPE html>
  8. <html>
  9. <body>
  10.  
  11. <canvas id="canvas" width="400" height="400"
  12. style="background-color:#333">
  13. </canvas>
  14.  
  15. <script>
  16. var canvas = document.getElementById("canvas");
  17. var ctx = canvas.getContext("2d");
  18. var radius = canvas.height / 2;
  19. ctx.translate(radius, radius);
  20. radius = radius * 0.90
  21. setInterval(drawClock, 1000);
  22.  
  23. function drawClock() {
  24.  drawFace(ctx, radius);
  25.  drawNumbers(ctx, radius);
  26.  drawTime(ctx, radius);
  27. }
  28.  
  29. function drawFace(ctx, radius) {
  30.  var grad;
  31.  ctx.beginPath();
  32.  ctx.arc(0, 0, radius, 0, 2*Math.PI);
  33.  ctx.fillStyle = 'white';
  34.  ctx.fill();
  35.  grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
  36.  grad.addColorStop(0, '#333');
  37.  grad.addColorStop(0.5, 'white');
  38.  grad.addColorStop(1, '#333');
  39.  ctx.strokeStyle = grad;
  40.  ctx.lineWidth = radius*0.1;
  41.  ctx.stroke();
  42.  ctx.beginPath();
  43.  ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
  44.  ctx.fillStyle = '#333';
  45.  ctx.fill();
  46. }
  47.  
  48. function drawNumbers(ctx, radius) {
  49.  var ang;
  50.  var num;
  51.  ctx.font = radius*0.15 + "px arial";
  52.  ctx.textBaseline="middle";
  53.  ctx.textAlign="center";
  54.  for(num = 1; num < 13; num++){
  55.    ang = num * Math.PI / 6;
  56.    ctx.rotate(ang);
  57.    ctx.translate(0, -radius*0.85);
  58.    ctx.rotate(-ang);
  59.    ctx.fillText(num.toString(), 0, 0);
  60.    ctx.rotate(ang);
  61.    ctx.translate(0, radius*0.85);
  62.    ctx.rotate(-ang);
  63.  }
  64. }
  65.  
  66. function drawTime(ctx, radius){
  67.    var now = new Date();
  68.    var hour = now.getHours();
  69.    var minute = now.getMinutes();
  70.    var second = now.getSeconds();
  71.    //hour
  72.    hour=hour%12;
  73.    hour=(hour*Math.PI/6)+
  74.    (minute*Math.PI/(6*60))+
  75.    (second*Math.PI/(360*60));
  76.    drawHand(ctx, hour, radius*0.5, radius*0.07);
  77.    //minute
  78.    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
  79.    drawHand(ctx, minute, radius*0.8, radius*0.07);
  80.    // second
  81.    second=(second*Math.PI/30);
  82.    drawHand(ctx, second, radius*0.9, radius*0.02);
  83. }
  84.  
  85. function drawHand(ctx, pos, length, width) {
  86.    ctx.beginPath();
  87.    ctx.lineWidth = width;
  88.    ctx.lineCap = "round";
  89.    ctx.moveTo(0,0);
  90.    ctx.rotate(pos);
  91.    ctx.lineTo(0, -length);
  92.    ctx.stroke();
  93.    ctx.rotate(-pos);
  94. }
  95. </script>
  96.  
  97. </body>
  98. </html>
  99. '''
  100.  
  101. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  102.  
  103. tf = tempfile.mktemp(".html", "JSdemo_")
  104. print tf
  105. with open(tf, 'w') as temp:
  106.     temp.write(js_data)
  107. webbrowser.get(chrome_path).open(tf)
  108. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement