HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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');
Also see: Tab Triggers