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"> Sorry, no canvas support here :( </canvas>
<canvas id="hover"></canvas>
<div id="start"> START </div>
<div id="info"> Double click anywhere = create/destroy point </div>
body {
margin: 0px;
overflow: hidden;
background: -moz-radial-gradient(center, ellipse cover, rgba(205,235,142,1) 0%, rgba(165,201,86,1) 100%);
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(205,235,142,1)), color-stop(100%,rgba(165,201,86,1)));
background: -webkit-radial-gradient(center, ellipse cover, rgba(205,235,142,1) 0%,rgba(165,201,86,1) 100%);
background: -o-radial-gradient(center, ellipse cover, rgba(205,235,142,1) 0%,rgba(165,201,86,1) 100%);
background: -ms-radial-gradient(center, ellipse cover, rgba(205,235,142,1) 0%,rgba(165,201,86,1) 100%);
background: radial-gradient(ellipse at center, rgba(205,235,142,1) 0%,rgba(165,201,86,1) 100%);
}
#hover
{
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
#start, #info {
width: 60px;
position: absolute;
top: 12px;
left: 10px;
background-color: rgba(0,0,0,0.7);
color: #fff;
height: 30px;
text-align: center;
line-height: 30px;
font-family: helvetica;
font-size: 12px;
border-radius: 3px;
cursor: pointer;
}
#info {
width: 255px;
left: 80px;
cursor: default;
background-color: rgba(0,0,0,0.4);
}
#start:hover {
background-color: rgba(0,0,0,0.8);
}
function BezierSim(){
var _this = this;
var width = window.innerWidth;
var height = window.innerHeight;
var canvas = document.getElementById('canvas');
var hover = document.getElementById('hover');
var mouse = {
x: 0,
y: 0
};
var request;
var mouseDown = false;
var selected = null;
var started = false;
this.context = canvas.getContext('2d');
this.hctx = hover.getContext('2d');
this.previous = {x: 0, y: 0};
//Set canvas to full screen.
canvas.width = width;
canvas.height = height;
hover.width = width;
hover.height = height;
var points = [];
//Controlable variables
this.steps = 150;
this.step = 0;
this.showDetail = true;
this.init = function(){
canvas.addEventListener('dblclick', doubleClick, false);
canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mouseup', mouseUp, false);
canvas.addEventListener('mousemove', mouseMove, false);
renderPoints();
}
//Render the key points of the curve
var renderPoints = function(){
clearCanvas();
var i = 0;
for (i; i < points.length; i++){
//Large points
drawPoint(points[i], 7);
if (i + 1 < points.length){
drawBetweenPoints(points[i], points[i + 1])
}
}
}
//Start and stop.
this.start = function(){
//copyToClipboard();
hover.width = hover.width;
this.hctx.strokeStyle = 'rgba(100,0,0,1)';
this.hctx.lineWidth = 2;
if (points.length > 0) {
if (started == false) {
started = true;
//Start loop
loop();
}
else {
started = false;
}
}
}
this.reset = function(){
_this.step = 0;
started = false;
/* cancelRequestAnimationFrame(request);*/
renderPoints();
}
//Loop through animation
var loop = function(){
//Has reached the end. Revert to start - and stop.
if (_this.step > _this.steps){
_this.reset();
return;
}
//No longer looping
if (started == false){
return;
}
requestAnimationFrame(loop);
animate();
}
var drawPoint = function(point, size){
_this.context.fillStyle = 'rgba(0, 0, 0, 1)';
_this.context.lineWidth = 3;
_this.context.beginPath();
_this.context.arc(point.x, point.y, size ,0 , Math.PI*2, true);
_this.context.closePath();
_this.context.stroke();
}
var drawBetweenPoints = function(point1, point2){
drawPoint(point1, 2);
drawPoint(point2, 2);
_this.context.lineWidth = 0.4;
_this.context.beginPath();
_this.context.moveTo(point1.x, point1.y);
_this.context.lineTo(point2.x, point2.y);
_this.context.stroke();
}
//Recursively determines the epoch point
var getPointBetween = function(epoch, points){
var i = 0;
var foundPoints = [];
if (points.length > 1) {
for (i = 0; i < points.length - 1; i++) {
var point = {x:0, y:0};
//B(t) = P0 + t(P1 - P0)
point.x = points[i].x + epoch * (points[i + 1].x - points[i].x);
point.y = points[i].y + epoch * (points[i + 1].y - points[i].y);
if (_this.showDetail == true) {
drawBetweenPoints(points[i], points[i + 1]);
}
foundPoints.push(point);
}
//Recurse with new points
return getPointBetween(epoch, foundPoints);
} else {
return points[0];
}
}
var animate = function(){
if (_this.showDetail == true){
clearCanvas();
}
renderPoints();
var epoch = _this.step/_this.steps;
var point = getPointBetween(epoch, points);
//Main point - Do not draw on first step (For ui purposes)
if (_this.step != 0) {
_this.context.fillStyle = 'rgba(0, 0, 0, 0.8)';
_this.context.beginPath();
_this.context.arc(point.x, point.y, 8, 0, Math.PI * 2, true);
_this.context.closePath();
_this.context.fill();
_this.hctx.beginPath();
_this.hctx.moveTo(_this.previous.x, _this.previous.y);
_this.hctx.lineTo(point.x, point.y);
_this.hctx.stroke();
}
_this.previous.x = point.x;
_this.previous.y = point.y;
//This allows it to still render while stopped
if (started == true){
_this.step++;
}
}
var clearCanvas = function(){
canvas.width = canvas.width;
}
// ------------------------------------------------------------------------------
// Loading data / Saving data
// ------------------------------------------------------------------------------
//TODO: Investigate clean string addition in js
var copyToClipboard = function(){
var i = 0;
var url = location.href;
var url_parts = url.split('?');
var main_url = url_parts[0];
var string = main_url + '?q=';
for (i; i < points.length; i++) {
//Scale data down, so it can be loaded equaly on another screen size
string = string + 'x' + Math.round((width/points[i].x) * 100)/100;
string = string + 'y' + Math.round((height/points[i].y) * 100)/100;
}
//console.log( string);
}
//TODO: Learn the propper regex method
//Load data string.
this.loadData = function(dataString){
var i = 1; //skip first entry
var data = dataString.split('x');
for (i; i < data.length; i++){
var point = data[i].split('y');
points.push({x:(width/parseFloat(point[0])), y:height/parseFloat(point[1])});
}
}
var mouseUp = function(event){
mouseDown = false;
selected = null;
}
var mouseMove = function(event){
mouse.x = event.offsetX || (event.layerX - canvas.offsetLeft);
mouse.y = event.offsetY || (event.layerY - canvas.offsetTop);
if (mouseDown == true && selected != null){
points[selected].x = mouse.x;
points[selected].y = mouse.y;
clearCanvas();
renderPoints();
//Don't animate step 0
if(_this.step != 0){
animate();
}
}
}
var mouseDown = function(event){
//Prevent draging of points from confusing the screen.
event.preventDefault();
mouseDown = true;
var i = 0;
for (i; i < points.length; i++) {
dx = points[i].x - mouse.x;
dy = points[i].y - mouse.y;
sqrDist = Math.sqrt(dx * dx + dy * dy);
//You may now drag selected point
if (sqrDist < 30) {
if (selected == null) {
selected = i;
}
}
}
}
var doubleClick = function(event){
var i = 0, dx, dy;
mouse.x = event.offsetX || (event.layerX - canvas.offsetLeft);
mouse.y = event.offsetY || (event.layerY - canvas.offsetTop);
//If there are no points
if (points.length == 0){
createPoint({x: mouse.x, y: mouse.y});
return;
}
for (i; i < points.length; i++){
dx = points[i].x - mouse.x;
dy = points[i].y - mouse.y;
sqrDist = Math.sqrt(dx * dx + dy * dy);
//If the double click was on another point
if (sqrDist < 30) {
removePoint(i);
return;
}
}
createPoint({x: mouse.x, y: mouse.y});
}
var removePoint = function(index){
points.splice(index, 1);
renderPoints();
animate();
}
var createPoint = function(point){
points.push(point);
renderPoints();
animate();
}
//Global function to clear
this.clearPoints = function(){
points = new Array();
_this.reset();
}
}
/**
* Provides requestAnimationFrame in a cross browser way.
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
if ( !window.requestAnimationFrame ) {
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
return window.setTimeout(callback, 1000 / 60);
};
})();
}
var bezierSim;
// For the demo
setTimeout( function() {
bezierSim = new BezierSim();
bezierSim.loadData( "x17.9y4.58x7.38y1.19x3.16y1.19x2.8y2.13x2.54y7.27x1.71y7.27x1.57y2.04x1.44y1.2x1.18y1.2x1.09y5.52");
bezierSim.init();
bezierSim.start();
}, 10 );
var startbutton = document.getElementById("start");
startbutton.onclick = function() {
bezierSim.start();
}
Also see: Tab Triggers