123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<body bgcolor="#000000">
  <canvas id="canvas" height="768" width="768"></canvas>
</body>
 
            
          
!
            
              #canvas {
    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;

    margin: auto;
}
            
          
!
            
              var stats = new Stats();
stats.showPanel( 1 ); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild( stats.dom );

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
var smallcanvas = document.createElement('canvas');
var smallctx = smallcanvas.getContext('2d');
smallcanvas.width = 256; smallcanvas.height = 256;
var canvasHeight = smallcanvas.height;
var canvasWidth  = smallcanvas.width;
var imageData = smallctx.getImageData(0, 0, canvasWidth, canvasHeight);

var buf = new ArrayBuffer(imageData.data.length);
var buf8 = new Uint8ClampedArray(buf);
var data = new Uint32Array(buf);

Math.clip = function(number, min, max) {
  return Math.max(min, Math.min(number, max));
}

function pset(x, y, color) { //color object with properties a, r, g, b
  //buffer overflows are quite costly. so we check to make sure the point is on the canvas 
if(x > 0 && x < canvasWidth){
  if(y > 0 && y < canvasHeight){
    data[y * canvasWidth + x] =
      (color.a << 24) |    // alpha
      ((color.b) << 16) |    // blue
      ((color.g) <<  8) |    // green
      (color.r);            // red
  }
}
      
};

function line(x1, y1, x2, y2, color){
  
   var dy = y2 - y1;
    var dx = x2 - x1;
    var stepx, stepy;

    if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
    if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
    dy <<= 1;        // dy is now 2*dy
    dx <<= 1;        // dx is now 2*dx

    pset(x1,y1, color);
    if (dx > dy) 
    {
        var fraction = dy - (dx >> 1);  // same as 2*dy - dx
        while (x1 != x2) 
        {
           if (fraction >= 0) 
           {
               y1 += stepy;
               fraction -= dx;          // same as fraction -= 2*dx
           }
           x1 += stepx;
           fraction += dy;              // same as fraction -= 2*dy
           pset(x1, y1, color);
        };
     } else {
        fraction = dx - (dy >> 1);
        while (y1 != y2) {
           if (fraction >= 0) {
               x1 += stepx;
               fraction -= dy;
           }
           y1 += stepy;
           fraction += dx;
           pset(x1, y1, color);
        }
     }
  
}; //BRESENHAM FTW!!

function circle(xm, ym, r, color){
     var x = -r, y = 0, err = 2-2*r; /* II. Quadrant */ 
     do {
       
        
        pset(xm-x, ym+y, color); /*   I. Quadrant */
        pset(xm-y, ym-x, color); /*  II. Quadrant */
        pset(xm+x, ym-y, color); /* III. Quadrant */
        pset(xm+y, ym+x, color); /*  IV. Quadrant */
        r = err;
        if (r <= y) err += ++y*2+1;           /* e_xy+e_y < 0 */
        if (r > x || err > y) err += ++x*2+1; /* e_xy+e_x > 0 or no 2nd y-step */
     } while (x < 0);
  };

function fillCircle(xm,ym,r,color){
var x = -r, y = 0, err = 2-2*r; /* II. Quadrant */ 
     do {
       
        line(xm-x, ym-y, xm+x, ym-y, color);
        line(xm-x, ym+y, xm+x, ym+y, color);
        r = err;
        if (r <= y) err += ++y*2+1;           /* e_xy+e_y < 0 */
        if (r > x || err > y) err += ++x*2+1; /* e_xy+e_x > 0 or no 2nd y-step */
     } while (x < 0);
}

function rect(x1, y1, x2, y2, color){
  line(x1, y1, x2, y1, color);
  line(x2, y1, x2, y2, color);
  line(x1, y2, x2, y2, color);
  line(x1, y2, x1, y1, color);
};

function fillRect(x1, y1, x2, y2, color){
  var i = Math.abs(y2 - y1);
  line(x1, y1, x2, y1, color);
  while(--i){
    line(x1, y1+i, x2, y1+i, color);
  }
  line(x1,y2, x2, y2, color);
}

var 
    num = 6000,
    m = 0, 
    c = 0,
    s = 0,
    cc = 0, ss = 0;
    color1 = 0,
    color2 = 0,
    i = 0,
    cx = 0, cy = 0,
    dx = 0, dy = 0,
    x1 = 0, x2 = 0,
    y1 = 0, y2 = 0,
    cir = [];

//i = 50; 

for(i = num; i > 0; i--)
{
  cir.push(
    {
      cx: Math.floor( (Math.random() * 1024)),
      cy: Math.floor( (Math.random() * 1024)),
      cz: Math.floor( (Math.random() * 2048)),
      dia: (Math.random() * 512),
      color: {
        r: 200,
        g: (Math.random() * 200) & 0xff,
        b: 0,
        a: 255
      }
    })
}

//console.log(cir);
var loop = function(){
  
  stats.begin();
  
  m += .01;
    
//rendering  
for(var x = 0; x < canvasWidth; x++){
  for(var y = 0; y < canvasHeight; y++ ){
    var value = x * y & 0xff;
    pset(x,y, {r:0, g:0, b:value, a:255})
  }
}  
i = num;
  while(--i){ 
    //yellow lines
    //c = Math.floor(Math.cos(m+i) * 127);
    //s = Math.floor(Math.sin(m+i) * 127);
    //line(128+s,128+c,128-s,128-c, { r:255, g:200, b:0, a:255 }); 
    
    //black circles
    dx = cir[i].cx-256;
    dy = cir[i].cy-256;
    fl = 600;
    
    
    cc = Math.cos(m);
    ss = Math.sin(m);
    dz = cir[i].cz;
    p = dz / (fl + dz);
    cx = Math.floor( ( (cc * dx - ss * dy) ) * p) + 128;
    cy = Math.floor( ( (ss * dx + cc * dy) + 128 ) * p)+ 64;
                   
    //(cx,cy, cir[i].dia, cir[i].color);
    circle(cx, cy, Math.floor(6 * p), cir[i].color);
  }
  
rect(1,1,254,254, { r:0, g:255, b:200, a:255 });

//circle(127,127,32, { r:254, g:254, b:254, a:255 });

imageData.data.set(buf8);
smallctx.putImageData(imageData, 0, 0);
ctx.drawImage(smallcanvas, 0,0, 255, 255, 0,0, 767, 767);
  
stats.end();
requestAnimationFrame(loop);
}
loop();


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console