css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <canvas id="canvas"></canvas>

<div id="controls">
  <p id="intro">Try different values below and click the Generate button to create new art. Hover over inputs for tips.</p>
	<p><label for="numberOfLines">No. of lines</label><input type="text" id="numberOfLines" value="6" title="Number of lines to draw - between 3 and 8 gives the best results"/></p>
	<p><label for="size">Line width</label><input type="text" id="size" value="4" title="Width of the lines - try chunky lines for a different effect"/></p>
	<p><label for="distance">Step size</label><input type="text" id="distance" value="1" title="The length of each line/step before rotating - smaller numbers produce smoother patterns"/></p>
	<p><label for="speed">Draw speed</label><input type="text" id="speed" value="30" title="Delay in milliseconds between draw updates - doesn't affect the end result"/></p>
	<p><label for="increment">Step increment</label><input type="text" id="increment" value="0.8" title="Amount to increase the step size by each time - very small numbers are best"/></p>
	<p><label for="turn">Rotation amount</label><input type="text" id="turn" value="45" title="Amount to rotate by between steps"/></p>
	<p><label for="wobble">Split point</label><input type="text" id="wobble" value="7" title="An interval afterwhich the rotation amount resets to zero - results in sharp turns. Set to zero to turn off."/></p>
	<p><label for="opacity">Line opacity</label><input type="text" id="opacity" value="0.5" title="Opacity/alpha values of the lines"/></p>
	<!--<p><label for="randomVariance">Randomness</label><input type="text" id="randomVariance" value="0" title="Make lines wobbly - lessons better"/></p>-->
	<p id="shareLine"><label for="sharecode">Share code</label><input type="text" id="sharecode" value="" title="Copy and paste this code for easily sharing between friends"/></p>
	<p id="buttonsLine">
		<input type="submit" id="submit" value="Generate" title="Click to redraw with your new settings"/>
		<input type="button" id="random" value="Random" title="Get some random settings"/>
		<input type="button" id="hide" value="Hide" title="Get rid of this settings panel - you won't get it back though!"/>
	</p>
  <p><a href="#" id="download" title="Careful, this can affect the image on screen!">Download image (beta)</a></p>
  <p id="reddit">View other people's creations on <a href="http://www.reddit.com/r/InternetIsBeautiful/comments/2u0248/spiral_art_generator_xpost_from_rwebdev/" target="_blank">Reddit</a>!</p>
</div>
            
          
!
            
              body {
  color: #000;
  background-color:#1D1F20;
  margin: 0;
  padding: 0;
  font-family: 'Lato', sans-serif;
}

canvas {
  background-color:#000;
  display: block;
  margin: 0;
  width: 100%;
  height: 100%;
  box-shadow: 0px 0px 20px 5px rgba(0,0,0,0.65);
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  right: 0;
}

#controls {
  position: fixed;
  top: 20px;
  left: 20px;
  border:1px solid #808080;
  padding: 5px 15px;
  background: #CCC;
  box-shadow: 0px 0px 20px 5px rgba(0,0,0,0.65);
  opacity: 0.95;
}

#intro, #reddit {
  width: 200px;
  font-size: 0.9em;
}

#intro {
  padding-bottom: 10px;
  font-weight: bold;
}

#reddit {
  padding-top: 20px;
}

#controls p {
  margin: 5px 0;
}

#controls label {
  display: inline-block;
  width: 130px;
  font-size: 0.9em;
}

#controls input {
  width: 70px;
  text-align: center;
}

#controls #submit, #controls #random, #controls #hide {
  margin-right: 1px;
  font-family: 'Lato', sans-serif;
  cursor: pointer;
}

#controls #submit {
  width: 80px;
}

#controls #random {
  width: 70px;
}

#controls #hide {
  width: 50px;
  margin-right: 0;
}

#controls #shareLine {
  margin-top: 15px;
}

#controls #buttonsLine {
  margin-top: 15px;
}
            
          
!
            
              $(function () {
	var myCanvas, context, width, height;
	var lines = [], numberOfLines = 12;
	var colours = ['#FFD800','#FF6A00','#FF0000','#0094FF','#0026FF','#4800FF','#7FFF8E','#B6FF00','#4CFF00', '#FFFFFF'];

	var Line = function() {
		return {
			x: 0,
			y: 0,
			size: 4,
			colour: '#FFFFFF',
			distance: 1,
			speed: 30,
			increment: 0.8,
			turn: 45,
			wobble: 7,
			rotation: random(0, 359),
			randomVariance: 0,
			opacity: 0.5,
			clockwise: true,
			glow: true,
			interval: {},
			tick: function() {
				var oldX = this.x;
				var oldY = this.y;

				if (this.clockwise)
					this.rotation -= this.turn;
				else
					this.rotation += this.turn;

				// Wobble (point at which lines bend sharply)
				if (this.wobble) {
					if (this.rotation < 0) this.rotation += this.wobble;
					if (this.rotation >= this.wobble) this.rotation -= this.wobble;
				}

				this.x = this.x + this.distance * Math.sin(this.rotation);
				this.y = this.y + this.distance * Math.cos(this.rotation);

				// Random wander
				if (this.randomVariance) {
					this.x += random((-1 * this.randomVariance), this.randomVariance);
					this.y += random((-1 * this.randomVariance), this.randomVariance);
				}

				if (this.glow) {
					drawLine(oldX, oldY, this.x, this.y, this.size * 6, this.colour, 0.03);
					drawLine(oldX, oldY, this.x, this.y, this.size * 4, this.colour, 0.05);
				}

				drawLine(oldX, oldY, this.x, this.y, this.size, this.colour, this.opacity);

				this.distance += this.increment;

				return this;
			},
			init: function(details) {
				for (var x in details) {
					this[x] = details[x];
				}

				var self = this;
				this.interval = setInterval(function() { self.tick(); }, this.speed);

				return this;
			},
			stop: function() {
				clearInterval(this.interval);
			}
		};
	};

	if ($("canvas").length > 0) {
		myCanvas = $("#canvas")[0];
		context = myCanvas.getContext("2d");
		resizeCanvas();
		eventListeners();
		startAnimation();
	}

	function resizeCanvas() {
		myCanvas.width = $("canvas").width();
	    myCanvas.height = $("canvas").height();
	    width = myCanvas.width;
		height = myCanvas.height;
	}

	$(window).on('resize', resizeCanvas);

	function eventListeners() {
		$('#submit').on('click', function() {
			var options = {};

			$('#controls input[type="text"]').each(function() {
				options[$(this).attr('id')] = checkNumber($(this).val());
			});

			numberOfLines = options.numberOfLines;

			var shareString = '';

			for (var x in options) {
				shareString += options[x] + '|';
			}

			$('#sharecode').val(shareString);

			startAnimation(options);
			return false;
		});

		$('#random').on('click', function() {
			randomSettings();
		});

		$('#hide').on('click', function() {
			$('#controls').hide();
		});

		$('#download').on('click', function() {
      		var image = myCanvas.toDataURL('image/png').replace('image/png', 'image/octet-stream');
			var filename = 'spiral_' + (new Date()).getTime() + '.png';

			$(this).attr('href', image).attr('download', filename);
		});

		$('#sharecode')
		.on('paste', function() {
			setTimeout(function() {
				var shareString = $('#sharecode').val().split('|');

				$('#controls input[type="text"]').not('#sharecode').each(function(x) {
					$(this).val(shareString[x]);
				});

				$('#submit').trigger('click');
			}, 100);
		})
		.on('focus', function() {
			$(this).select();
		});
	}

	function randomSettings() {
		var settings = {
			numberOfLines:    random(2, 12),
			size:             random(1, 10),
			distance:         random(1, 30),
			speed:            random(2, 6) * 10,
			increment:        random(1, 10) / 10,
			turn:             random(1, 200),
			wobble:           random(1, 400),
			opacity:          random(3, 10) / 10,
			randomVariance:   0
		};

		for (var x in settings) {
			$('#' + x).val(settings[x]);
		}

		$('#submit').trigger('click');
	}

	function startAnimation(options) {
    	options = options ? options : {};

		options.x = width / 2;
		options.y = height / 2;

		context.clearRect(0, 0, width, height);
		context.fillStyle = '#000000';
		context.globalAlpha = 1;
		context.fillRect(0, 0, width, height);

		for (var x = 0; x < lines.length; x++) {
			lines[x].stop();
		}

		lines = [];

		for (var y = 0; y < numberOfLines; y++) {
			options.colour = colours[random(0, colours.length - 1)];
			options.rotation = (360 / numberOfLines) * y;
			lines.push(
				Line().init(options)
			);
		}
	}

	function drawPoint(x, y, size, colour) {
		context.fillStyle = colour;
		context.fillRect(x, y, size, size);
	}

	function drawLine(x1, y1, x2, y2, size, colour, opacity) {
		setOpacity(opacity);
		context.beginPath();
		context.strokeStyle = colour;
		context.lineWidth = size;
		context.moveTo(x1, y1);
		context.lineTo(x2, y2);
		context.stroke();
		context.closePath();
	}

	function setOpacity(alpha) {
		context.globalAlpha = alpha;
	}

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

	function checkNumber(n) {
		n = parseFloat(n);
		if (isNaN(n) || n < 0) n = 0;

		return n;
	}
});



  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-45462422-7', 'auto');
  ga('send', 'pageview');
            
          
!
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.
Loading ..................

Console