Advertisement
here2share

# js_square_ani.py

Nov 26th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. # js_square_ani.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''<!DOCTYPE html>
  8. <html>
  9.     <head>
  10.     <title>HTML5 Square Animation</title>
  11.     </head>
  12.     <body>
  13.         <canvas id="myCanvas" width="1000" height="300"
  14.                         style="border:0px">
  15.         </canvas>
  16.         <script>
  17.             const canvas = document.querySelector('canvas');
  18.             const ctx = canvas.getContext('2d');
  19.  
  20.             canvas.width = 1000;
  21.             canvas.height = 300;
  22.  
  23.             let cw = canvas.width;
  24.             let ch = canvas.height;
  25.  
  26.             let canvasX = 0;
  27.             let canvasY = 0;
  28.  
  29.             let rectX = 0;
  30.             let rectY = ch/2;
  31.  
  32.             let rectSize = 50;
  33.             let rectSizeLeft = 50;
  34.             let rectSizeRight = 100;
  35.             let rectSpeed = 1;
  36.             let rectSpeedLeft = 1;
  37.  
  38.             // my change
  39.             let currentColors = randomColors();
  40.  
  41.             function field(){
  42.  
  43.                 ctx.fillStyle = '#ddd';
  44.                 ctx.fillRect(canvasX, canvasY, cw, ch);
  45.  
  46.                 window.requestAnimationFrame(field);
  47.  
  48.             }
  49.  
  50.             function randomColors() {
  51.  
  52.                 let r = Math.floor(Math.random()*255);
  53.                 let g = Math.floor(Math.random()*255);
  54.                 let b = Math.floor(Math.random()*255);
  55.  
  56.                 return 'rgb('+r+','+g+','+b+')';
  57.  
  58.             }
  59.  
  60.  
  61.             function rect(){
  62.  
  63.                 // my change
  64.                 if (rectX % 20 === 0) {
  65.                     currentColors = randomColors();
  66.                 }
  67.  
  68.                 // my change
  69.                 ctx.fillStyle = currentColors;
  70.  
  71.                 ctx.fillRect(rectX,rectY,rectSize,rectSize);
  72.  
  73.                 rectX+=rectSpeed;
  74.  
  75.  
  76.                 if(rectX+rectSize == 0){
  77.                     rectSize = rectSizeLeft;
  78.                     rectSpeed = rectSpeedLeft;
  79.                 } else if(rectX+rectSizeRight == cw+rectSizeRight){
  80.                     rectSize=rectSizeRight;
  81.                     rectSpeed = -rectSpeed;
  82.                 } ;
  83.  
  84.                 window.requestAnimationFrame(rect);
  85.  
  86.  
  87.                 // if(rectX+rectSizeRight == cw){
  88.                 //     rectSize=rectSizeRight;
  89.                 //     rectSpeed = -rectSpeed
  90.                 //   }
  91.  
  92.  
  93.  
  94.  
  95.                 //   if (rectX+rectSize <= 0){
  96.                 //       rectSize = rectSize;
  97.                 //       rectSpeed = -rectSpeed;
  98.                 //   }
  99.                 //   if(rectX+rectSizeRight>=cw){
  100.                 //       rectSize=rectSizeRight;
  101.                 //       rectSpeed = -rectSpeed;
  102.                 //   }
  103.                  }
  104.  
  105.  
  106.              function animate(){
  107.                 // my change
  108.                 field();
  109.                 rect();
  110.              }
  111.  
  112.              window.requestAnimationFrame(animate);
  113.  
  114.         </script>
  115.     </body>
  116. </html>
  117. '''
  118.  
  119. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  120.  
  121. tf = tempfile.mktemp(".html", "JSdemo_")
  122. print tf
  123. with open(tf, 'w') as temp:
  124.     temp.write(js_data)
  125. webbrowser.get(chrome_path).open(tf)
  126. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement