Advertisement
here2share

# js_mousemove.py

Nov 24th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. # js_mousemove.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  12. <style>
  13. canvas {
  14.    border:1px solid #d3d3d3;
  15.    background-color: #f1f1f1;
  16. }
  17. </style>
  18. </head>
  19. <body onload="startGame()">
  20. <script>
  21.  
  22. var myGamePiece;
  23. function startGame() {
  24.    myGamePiece = new component(30, 30, "red", 10, 120);
  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.canvas.style.cursor = "none"; //hide the original cursor
  34.        this.context = this.canvas.getContext("2d");
  35.        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  36.        this.interval = setInterval(updateGameArea, 20);
  37.        window.addEventListener('mousemove', function (e) {
  38.            myGameArea.x = e.pageX;
  39.            myGameArea.y = e.pageY;
  40.        })
  41.    },
  42.    clear : function(){
  43.        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  44.    }
  45. }
  46.  
  47. function component(width, height, color, x, y) {
  48.    this.width = width;
  49.    this.height = height;
  50.    this.speedX = 0;
  51.    this.speedY = 0;    
  52.    this.x = x;
  53.    this.y = y;    
  54.    this.update = function() {
  55.        ctx = myGameArea.context;
  56.        ctx.fillStyle = color;
  57.        ctx.fillRect(this.x, this.y, this.width, this.height);
  58.    }
  59. }
  60.  
  61. function updateGameArea() {
  62.    myGameArea.clear();
  63.    if (myGameArea.x && myGameArea.y) {
  64.        myGamePiece.x = myGameArea.x;
  65.        myGamePiece.y = myGameArea.y;        
  66.    }
  67.    myGamePiece.update();
  68. }
  69.  
  70. </script>
  71. <p>Move the cursor inside the canvas to move the red square.</p>
  72. </body>
  73. </html>
  74. '''
  75.  
  76. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  77.  
  78. tf = tempfile.mktemp(".html", "JSdemo_")
  79. print tf
  80. with open(tf, 'w') as temp:
  81.     temp.write(js_data)
  82. webbrowser.get(chrome_path).open(tf)
  83. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement