Advertisement
here2share

# js_objrotate.py

Nov 24th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. # js_objrotate.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''<!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  11. <style>
  12. canvas {
  13.    border:1px solid #d3d3d3;
  14.    background-color: #f1f1f1;
  15. }
  16. </style>
  17. </head>
  18. <body onload="startGame()">
  19. <script>
  20.  
  21. var myGamePiece;
  22.  
  23. function startGame() {
  24.    myGamePiece = new component(30, 30, "red", 80, 75);
  25.    myGameArea.start();
  26. }
  27.  
  28. var myGameArea = {
  29.    canvas : document.createElement("canvas"),
  30.    start : function() {
  31.        this.canvas.width = 480;
  32.        this.canvas.height = 270;
  33.        this.context = this.canvas.getContext("2d");
  34.        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  35.        this.frameNo = 0;
  36.        this.interval = setInterval(updateGameArea, 20);
  37.    },
  38.    stop : function() {
  39.        clearInterval(this.interval);
  40.    },    
  41.    clear : function() {
  42.        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  43.    }
  44. }
  45.  
  46. function component(width, height, color, x, y) {
  47.    this.width = width;
  48.    this.height = height;
  49.    this.angle = 0;
  50.    this.x = x;
  51.    this.y = y;    
  52.    this.update = function() {
  53.        ctx = myGameArea.context;
  54.        ctx.save();
  55.        ctx.translate(this.x, this.y);        
  56.        ctx.rotate(this.angle);
  57.        ctx.fillStyle = color;
  58.        ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);        
  59.        ctx.restore();    
  60.    }
  61. }
  62.  
  63. function updateGameArea() {
  64.    myGameArea.clear();
  65.    myGamePiece.angle += 1 * Math.PI / 180;    
  66.    myGamePiece.update();
  67. }
  68.  
  69. </script>
  70.  
  71. <p>The red square will rotate one degree every time the gamearea updates (50 times per second).</p>
  72.  
  73. <p>Note: The angle property must be a radian. You can convert degrees into radians by using the formula x * Math.PI / 180</p>
  74. </body>
  75. </html>
  76. '''
  77.  
  78. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  79.  
  80. tf = tempfile.mktemp(".html", "JSdemo_")
  81. print tf
  82. with open(tf, 'w') as temp:
  83.     temp.write(js_data)
  84. webbrowser.get(chrome_path).open(tf)
  85. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement