Advertisement
here2share

# js_droplets.py

Nov 26th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. # js_droplets.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''<!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10.    <meta charset="UTF-8" />
  11.    <title>Droplets</title>
  12.    <script>
  13.        document.createElement("canvas");
  14.    </script>
  15.    <style>
  16.        body {
  17.            font-family: sans-serif;
  18.            margin: 20px;
  19.            color: #333;
  20.            background-color: #fff;
  21.        }
  22.  
  23.        #mycanvas {
  24.            float: left;
  25.            width: 300px;
  26.            height: 300px;
  27.            margin: 0 20px 0 0;
  28.            background-image: url(rain.jpg);
  29.            border: 1px solid #000;
  30.        }
  31.  
  32.            #mycanvas.active {
  33.                background-image: none;
  34.            }
  35.    </style>
  36. </head>
  37. <body>
  38.  
  39.    <h1>HTML Canvas Example</h1>
  40.    <canvas id="mycanvas" width="300" height="300"></canvas>
  41.    <p>This page works in any browser.</p>
  42.  
  43.    <script>
  44.        var canvas = document.getElementById("mycanvas");
  45.        if (canvas.getContext) {
  46.  
  47.            // canvas supported
  48.            canvas.className = "active";
  49.  
  50.            // start animation
  51.            var cxt = canvas.getContext("2d");
  52.            cxt.fillStyle = "rgba(255,255,255,0.5)";
  53.  
  54.            setInterval(function () {
  55.                var x = Math.round(Math.random() * canvas.width),
  56.                    y = Math.round(Math.random() * canvas.height),
  57.                    e = 20 + Math.round(Math.random() * 30),
  58.                    s = 0;
  59.  
  60.                (function () {
  61.                    s++;
  62.                    if (s <= e) {
  63.                        setTimeout(arguments.callee, s);
  64.                        var c = 255 - (e - s) * 3;
  65.                        cxt.strokeStyle = "rgb(" + c + "," + c + "," + c + ")";
  66.                        cxt.beginPath();
  67.                        cxt.arc(x, y, s, 0, Math.PI * 2, true);
  68.                        cxt.fill();
  69.                        cxt.stroke();
  70.                    }
  71.                })();
  72.            }, 100);
  73.  
  74.        }
  75.    </script>
  76. </body>
  77. </html>
  78. '''
  79.  
  80. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  81.  
  82. tf = tempfile.mktemp(".html", "JSdemo_")
  83. print tf
  84. with open(tf, 'w') as temp:
  85.     temp.write(js_data)
  86. webbrowser.get(chrome_path).open(tf)
  87. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement