Advertisement
here2share

# js_touchmove.py

Nov 24th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. # js_touchmove.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", 10, 120);
  25.    myGameArea.start();
  26. }
  27.  
  28. var myGameArea = {
  29.    canvas : document.createElement("canvas"),
  30.    start : function() {
  31.        this.canvas.width = 300;//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.interval = setInterval(updateGameArea, 20);
  36.        window.addEventListener('touchmove', function (e) {
  37.            myGameArea.x = e.touches[0].screenX;
  38.            myGameArea.y = e.touches[0].screenY;
  39.        })
  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.speedX = 0;
  50.    this.speedY = 0;    
  51.    this.x = x;
  52.    this.y = y;    
  53.    this.update = function() {
  54.        ctx = myGameArea.context;
  55.        ctx.fillStyle = color;
  56.        ctx.fillRect(this.x, this.y, this.width, this.height);
  57.    }
  58. }
  59.  
  60. function updateGameArea() {
  61.    myGameArea.clear();
  62.    if (myGameArea.x && myGameArea.y) {
  63.        myGamePiece.x = myGameArea.x;
  64.        myGamePiece.y = myGameArea.y;        
  65.    }
  66.    myGamePiece.update();
  67. }
  68. </script>
  69. <p>For touch screens only: Move the red square with your finger.</p>
  70. </body>
  71. </html>
  72. '''
  73.  
  74. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  75.  
  76. tf = tempfile.mktemp(".html", "JSdemo_")
  77. print tf
  78. with open(tf, 'w') as temp:
  79.     temp.write(js_data)
  80. webbrowser.get(chrome_path).open(tf)
  81. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement