Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                <div id="container">
	<canvas width="340" height="340" id="mainCanvas"></canvas>
  <div id="ctrl">
    <div id=param>
      <div class="param_row">
        <span>Num: </span>
        <span id="num">20</span>
        <input type="range" min="1" max="100" value="20">
      </div>
      <div class="param_row">
        <span>Opacity[max]: </span>
        <span id="opacity">0.3</span>
        <input type="range" min="1" max="10" value="3">
      </div>
      <div class="param_row">
        <span>Radius[min]: </span>
        <span id="radius">40</span>
        <input type="range" min="10" max="100" value="40" step="10">
      </div>
    </div>
    <div id="btns">
      <div class="btn" id="playbtn"><i class="fa fa-play"></i></div>
      <div class="btn" id="stepbtn"><i class="fa fa-step-forward"></i></div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                #container {
	margin: 0 auto;
	padding: 0;
	width: 360px;
	height: 550px;
	background-color: #000000;
  font-family: initial;
}
#mainCanvas	{
	margin: 10px;
	background-color: #000;
  transition: opacity 1s linear;
}
#btns {
  margin: 20px auto 0 auto;
  padding: 0;
  display: -webkit-flex;
  display: flex;
  -webkit-justify-content: center;
  justify-content: center;
}
.btn {
  margin: 0 4px 0 0;
  padding: 0;
  width: 50px;
	height: 40px;
  color: #000;
  background-color: #ccc;
  text-align: center;
  line-height: 40px;
}
.btn:last-child {
  margin: 0;
}
.btn:hover, input[type="range"] {
	cursor: pointer;
}
.param_row {
  display: -webkit-flex;
  display: flex;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-align-items: center;
  align-items: center;
  font-size: 12px;
  color: #fff;
  margin: 0 0 8px 0;
}
.param_row > span:first-child {
  width: 86px;
}
.param_row > span+span {
    width: 50px;
    text-align: center;
}
input[type="range"] {
  width: 160px;
}
              
            
!

JS

              
                // art object
var art = (function() {
	var art = {};
	var cvsWidth;
	var cvsHeight;
	var params = {
		num: 20,
		opacity: 3,
		radius: 40
	};
	var shadowTbl = ["white", "black", "red", "green", "blue"];

	art.setNum = function(n) {params.num = n;};
	art.setOpacity = function(o) {params.opacity = o;};
	art.setRadius = function(r) {params.radius = r;};
	art.init = function() {
		cvsWidth = elems.canvas.width;
		cvsHeight = elems.canvas.height;
	};
	art.draw = function() {
		var r, g, b, x, y, rad;
		var ctx = elems.canvas.getContext("2d");
		ctx.clearRect(0, 0, cvsWidth, cvsHeight);
		ctx.save();
		for (var i=0; i<params.num; i++) {
			ctx.globalAlpha = getRandomInt(1, params.opacity) * 0.1;
			ctx.shadowBlur = getRandomInt(0, 40);
			ctx.shadowColor = shadowTbl[getRandomInt(0, 4)];
			r = getRandom(255);
			g = getRandom(255);
			b = getRandom(255);
			ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
			x = getRandom(cvsWidth);
			y = getRandom(cvsHeight);
			rad = getRandomInt(params.radius, cvsWidth);
			ctx.beginPath();
			ctx.arc(x, y, rad, 0, 2*Math.PI);
			ctx.fill();
		}
		ctx.restore();
	};
	art.autoDraw = function() {
		elems.canvas.style.opacity = 0.2;
		setTimeout(function() {
			art.draw();
			elems.canvas.style.opacity = 1;
		}, 1000);
	};
	art.getDrawImage = function() {
		var ctx = elems.canvas.getContext("2d");
		var image = ctx.getImageData(0, 0, cvsWidth, cvsHeight);
		ctx.clearRect(0, 0, cvsWidth, cvsHeight);
		return image;
	};
	art.putDrawImage = function(image) {
		var ctx = elems.canvas.getContext("2d");
		ctx.putImageData(image, 0, 0);
	};

  function getRandom(max) {
	  return Math.floor(Math.random() * (max + 1));
	}
	function getRandomInt(min, max) {
	  return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	return art;
})();

// Global object
var playFlg = false;
var timerId = 0;
window.onload = function() {
	window.elems = {
		canvas: document.getElementById("mainCanvas"),
		num: document.getElementById("num"),
		opacity: document.getElementById("opacity"),
		radius: document.getElementById("radius")
	};
	if (!elems.canvas || !elems.canvas.getContext) {
		var msg = "It can not be executed because the browser does not support the Canvas API.";
		alert(msg);
		return;
	}
  // Initialize
	art.init();
	art.draw();

  // Event Listener
	document.querySelector("#num + input[type='range']").addEventListener('input', function(e) {
		elems.num.textContent = this.value;
		art.setNum(parseInt(this.value, 10));
	});
	document.querySelector("#opacity + input[type='range']").addEventListener('input', function(e) {
		elems.opacity.textContent = (parseInt(this.value) * 0.1).toFixed(1) + "";
		art.setOpacity(parseInt(this.value, 10));
	});
	document.querySelector("#radius + input[type='range']").addEventListener('input', function(e) {
		elems.radius.textContent = this.value;
		art.setRadius(parseInt(this.value, 10));
	});
	document.getElementById("playbtn").addEventListener('click', function(e) {
		if (playFlg === false) {
			this.firstChild.className = "fa fa-stop";
			art.autoDraw();
			timerId = setInterval(art.autoDraw, 5000);
		} else {
			this.firstChild.className = "fa fa-play";
			clearInterval(timerId);
		}
		playFlg = !playFlg;
	});
	document.getElementById("stepbtn").addEventListener('click', function(e) {
		if (playFlg === true) return;
		art.draw();
	});
};	// onload
              
            
!
999px

Console