Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.43 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <title>Canvas Test</title>
  5.         <style>
  6.             html, body {
  7.                 margin: 0 0;
  8.                 padding: 0;
  9.                 width: 100%;
  10.                 height: 100%;
  11.             }
  12.             canvas {
  13.                 display: block;
  14.             }
  15.             #fps {
  16.                 position: absolute; top: 0; right: 0;
  17.                 padding: 10px;
  18.                 color: white;
  19.                 text-shadow: 0 0 4px black;
  20.                 font: 14pt "Arial Black";
  21.             }
  22.         </style>
  23.     </head>
  24.     <body>
  25.         <canvas id="plasma" width="256" height="256"></canvas>
  26.             <div id="fps"></div>
  27.         <script type="text/javascript">
  28.             var
  29.                 TAU = Math.PI*2,
  30.                 element = document.getElementById('plasma'),
  31.                 canvas = element.getContext('2d'),
  32.                 width = parseInt(element.getAttribute("width")),
  33.                 height = parseInt(element.getAttribute("height"));
  34.                 image = canvas.createImageData(width, height);
  35.                 ua = navigator.userAgent,
  36.                 kit = /webkit/i.test(ua)?'-webkit-':'-moz-',
  37.                 theta=0,
  38.                 fps = document.getElementById('fps');
  39.             function resize() {
  40.                 var dwidth = document.body.clientWidth,
  41.                     dheight = document.body.clientHeight;
  42.                 element.style[kit+'transform-origin'] = '0 0';
  43.                 element.style[kit+'transform'] = 'scale('+[
  44.                     dwidth/width,
  45.                     dheight/height
  46.                 ].join(',')+')';
  47.             }
  48.             resize();
  49.             window.addEventListener('resize', resize, false);
  50.             function setPixel(img, x, y, r, g, b, a) {
  51.                 index = 4*(x+y*img.width);
  52.                 img.data[index]=r;
  53.                 img.data[index+1]=g;
  54.                 img.data[index+2]=b;
  55.                 img.data[index+3]=a;
  56.             }
  57.             var
  58.                 start = (new Date()).getTime(),
  59.                 d1 = Math.random()*TAU,
  60.                 d2 = Math.random()*TAU,
  61.                 d3 = Math.random()*TAU,
  62.                 d4 = Math.random()*TAU,
  63.                 d5 = 1+Math.random(),
  64.                 d6 = 1+Math.random();
  65.             var palette = [];
  66.            
  67.             for (i=0; i<256; i++) {
  68.                 if (i<64) {
  69.                     palette.push([ i*4, 0, 0]);
  70.                 } else if (i<128) {
  71.                     palette.push([ 255, (i-64)*4, 0]);
  72.                 } else if (i<192) {
  73.                     palette.push([ 255,255,(i-128)*4]);
  74.                 } else if (i<224) {
  75.                    palette.push([ 255, (224-i)*4+128, (224-i)*8]);
  76.                } else {
  77.                  palette.push([ (255-i)*8, (255-i)*4, 0]);
  78.                }
  79.             }
  80.            for (var y=0; y<height*width*4; y+=4) image.data[y+3]=0xff;
  81.            var f =0;
  82.             function iterate() {
  83.                 palette.push(palette.shift());
  84.                 var rgb, i,x,y,r,g,b,a,t=(((new Date()).getTime()-start)/1000),
  85.                     dx1 = Math.sin(d1+t/TAU) * d5 * TAU * Math.cos(d4+t/TAU)/ width,
  86.                     dx2 = Math.cos(d2+t/TAU) * d6 * TAU * Math.sin(d3+t/TAU) / height,
  87.                    pind = 0;
  88.                 for (y=0; y<height; y++) for (x=0; x<width; x++) {
  89.                     rgb = palette[Math.floor(((Math.sin(dx1*x)+1) + (Math.cos(dx2*y)+1))*64)];
  90.                    for (c=0; c<rgb.length; c++) image.data[pind+c]=rgb[c];
  91.                    pind+=4;
  92.                 }
  93.                 canvas.putImageData(image, 0, 0);
  94.                f++;
  95.                var rate = new String(Math.floor(f*100/t)/100);
  96.                while (rate.replace(/^\d+\./, '').length < 2) rate+='0';
  97.                fps.innerText = rate;
  98.                 setTimeout(arguments.callee, 0);
  99.             }
  100.             iterate();
  101.         </script>
  102.     </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement