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.
<form class="boton" id="form">
<input id='audiofile' type='file'>
<label id="label" for="audiofile">Elegir archivo</label>
</form>
<canvas></canvas>
html, body {
margin: 0;
background-color: #000000;
font-family: 'Helvatica', sans-serif;
color: #FFFFFF;
overflow: hidden;
}
canvas {
display: block;
border: 1px solid #333;
margin: calc(50vh - 150px) auto;
}
input {
width: 0.1px;
height: 0.1px;
opacity: 0;
}
label {
color: white;
padding: .5em 1em;
background-color: tomato;
display: block;
width: 6.25em;
text-align: center;
}
form {
position:absolute;
width: 8.25em;
top:50vh;
left:50vw;
margin-left:-4.125em;
margin-top:-2em;
}
label:hover {
background-color: red;
}
// variables para el audio
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var audio, analizador, fuenteDeReproduccion, dataArray;
// variables para el canvas
var canvas = document.querySelector("canvas");
var ctx = canvas.getContext("2d");
var cw = canvas.width = 700;
var ch = canvas.height = 300;
ctx.fillStyle = "white";
// A U D I O
// el evento onchange se dispara cuando cambia el valor de un elemento <input> <select> o <textarea>
audiofile.addEventListener("change", function(event) {
// si se trata de un archivo de sonido
if(event.target.files[0].type.search("audio") == 0){
// crea un nuevo elemento <audio> que no pertenece al DOM
audio = new Audio();
// y cuya fuente ( src ) es el URL blob del audio escogido por el usuario
audio.src = URL.createObjectURL(event.target.files[0]); //console.log(audio)
// utilizamos el evento media "ended" para detectar cuando se acaba la reproducción del sonido
audio.addEventListener(
"ended",
function() {
if (requestId) {
// para parrar la animación
window.cancelAnimationFrame(requestId);
// limpiar el canvas
ctx.clearRect(0, 0, cw, ch);
// y cambiar el display de label a "block", ya que durante la animación el valor de la propiedad display de la etiqueta es "none"
label.style.display = "block";
// Si selectamos el mismo archivo dos veces seguidos, la segunda vez el evento "onchange" de input[type=file] no se dispara. No pasa lo mismo si la segunda vez cargamos otro archivo. Para que esto no pase tenemos que restablecer ( reset ) el formulario:
form.reset();
}
},
false
);
// utilizamos el evento media "canplay" para detectar si archivo de sonido puede ser reproducido.
audio.addEventListener("canplay", function() {
// si puede reproducirse, cambiamos el display de label a "none". La etiqueta ( <label> ) ya no está visible
label.style.display = "none";
// crea un nuevo analizador
analizador = audioCtx.createAnalyser();
analizador.fftSize = 256; // [32, 64, 128, 256, 512, 1024, 2048]
dataArray = new Uint8Array(analizador.frequencyBinCount);
// el método createMediaElementSource se utiliza para crear una nueva fuente de reproducción si hay un elemento <audio> o <video> que puede reproducirse.
fuenteDeReproduccion = audioCtx.createMediaElementSource(audio);
// conecta la fuente de reproducción con el analizador
fuenteDeReproduccion.connect(analizador);
// y el analizador con el dispositivo de destino.
analizador.connect(audioCtx.destination);
// reproduce el audio
audio.play();
// y llama la función que genera la animación
Animacion();
});
}// if type audio cond.
});
function Animacion() {
requestId = window.requestAnimationFrame(Animacion);
ctx.clearRect(0, 0, cw, ch);
analizador.getByteTimeDomainData(dataArray);
// alternativamente puede utilizar el método getByteFrequencyData
// utiliza un bucle for ( for loop ) para leer los datos de dataArray y dibujar un pequeño circulo para cada elemento del array
for (var i = 0; i < dataArray.length; i += 1) {
// calcula la posición y tamaño de cada circulo
var w = cw/dataArray.length;
var x = i * w;
var y = ch - dataArray[i];
// dibuja el circulo
ctx.beginPath();
ctx.arc(x,y,(w-1)/2,0,2*Math.PI);
ctx.fill();
}
}
Also see: Tab Triggers