Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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.

HTML

            
              <html>
<head>
  <title>Playing with Rectangles</title>
    <style type="text/css">
      body {background-color: #202020}
    </style>
</head>
<body>
  <canvas id="canvas" width="512" height="512"></canvas> 
</body>
</html>
            
          
!

CSS

            
              
            
          
!

JS

            
              // Create some code art using just the 3 rectangle functions
// of canvas and colors. Left click to generate another pattern
// right click to save the image

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

// An array of rbg colors for the theme
var palette = ["27,231,255", "110,235,131", "228,255,26",
  "232,170,20", "255,87,20"
];

var min_x = 80;
var min_y = 80;
var max_x = canvas.width - min_x;
var max_y = canvas.height - min_y;

var min_w = 20;
var max_w = 80;
var min_h = 20;
var max_h = 80;
var count = 25;
var solidPercent = 20;
var lineWidth = 2;
var opacity = 0.7;

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.lineWidth = lineWidth;
  for (var i = 0; i <= count; i++) {
    var x = randomIntBetween(min_x, max_x);
    var y = randomIntBetween(min_y, max_y);
    var w = randomIntBetween(min_w, max_w);
    var h = randomIntBetween(min_h, max_h);
    var c = palette[randomIntBetween(0, 4)];

    if (randomIntBetween(0, 100) < solidPercent) { // chance of a solid fill
      var rgba = "rgba(" + c + "," + opacity + ")";
      ctx.fillStyle = rgba;
      ctx.fillRect(x, y, w, h);
    } else {
      ctx.strokeStyle = 'rgb(' + c + ')';
      ctx.strokeRect(x, y, w, h);
    }

  }
}

// Helper functions
// return a random integer between min and max inclusive
function randomIntBetween(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

// catch left button clicks and draw a new canvas
// but allow right click to save canvas as an image without changing it
canvas.addEventListener("click", canvasClick, false);

function canvasClick(e) {
  e = e || window.event;
  if ('object' === typeof e) {
    if (e.button !== 0) return; // if it's not the left mouse button, then bail
    else draw();                // otherwise, draw a new pattern
  }
}

// use dat.gui to tweak the control variables
// cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5.1/dat.gui.min.js
// see docs at: http://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage
var gui = new dat.GUI();
gui.add(this, 'count', 5, 200);
gui.add(this, 'solidPercent', 0, 100);
gui.add(this, 'min_w', 5, 200);
gui.add(this, 'max_w', 5, 200);
gui.add(this, 'min_h', 5, 200);
gui.add(this, 'max_h', 5, 200);
gui.add(this, 'lineWidth', 1, 10);
gui.add(this, 'opacity', 0, 1);
gui.close();

draw();
            
          
!
999px

Console