here2share

# js_putImageData.py

Nov 26th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. # js_putImageData.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''<!DOCTYPE html>
  8. <html>
  9.     <head>
  10.     <title>HTML5 putImageData</title>
  11.     </head>
  12.     <body>
  13.         <canvas id="myCanvas" width="1200" height="800"
  14.                         style="border:0px">
  15.         </canvas>
  16.         <script>
  17.             setInterval(function() {
  18.                 var canvas = document.getElementById('myCanvas'),
  19.                     ctx = canvas.getContext('2d');
  20.                 var rectData = ctx.getImageData(10, 10, 1200, 800);
  21.                 for (var y=0; y<800; y++) {
  22.                     for (var x=0; x<1200; x++) {
  23.                         const offset = 4*(y*800+x);// 4* because each pixel is 4 bytes
  24.                         rectData.data[offset+0] = Math.floor(Math.random() * 256);// red
  25.                         rectData.data[offset+1] = Math.floor(Math.random() * 256);// green
  26.                         rectData.data[offset+2] = Math.floor(Math.random() * 256);// blue
  27.                         rectData.data[offset+3] = 255;// alpha, fully opaque
  28.                     }
  29.                 }
  30.                 ctx.putImageData(rectData, 10, 10);
  31.             }, 0)
  32.  
  33.         </script>
  34.     </body>
  35. </html>
  36. '''
  37.  
  38. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  39.  
  40. tf = tempfile.mktemp(".html", "JSdemo_")
  41. print tf
  42. with open(tf, 'w') as temp:
  43.     temp.write(js_data)
  44. webbrowser.get(chrome_path).open(tf)
  45. os.remove(tf)
Add Comment
Please, Sign In to add comment