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" width="400" height="400"></canvas>
const VERTEX_SHADER = `#version 300 es
// Below are 2 input attributes
// The values for these attributes will be provided
// by us in the javascript "world"
// vec2 is a vector with 2 values representing the
// vertex coordinates (x/y).
in vec2 a_position;
// vec3 is a vector with 3 values representing the
// vertex color (r/g/b).
in vec3 a_color;
// output color for this vertex,
// OpenGL will interpolate these values automatically
out vec3 color;
void main() {
// convert coord from [0,1] space to [0,2] space
vec2 zeroToTwo = a_position * 2.0;
// convert coord from [0,2] space to [-1,1]
vec2 glCoordSpace = zeroToTwo - 1.0;
// set the output in the global predefined gl_Position variable.
// Note a vertex has 4 values: x,y,z,w - we use 0,1 for z,w.
gl_Position = vec4(glCoordSpace, 0, 1);
// set the output color variable, just provide the user intput
color = a_color;
}
`;
const FRAG_SHADER = `#version 300 es
precision highp float;
// input color for this fragment, provided by OpenGL by
// interpolating the output color variable that is in the
// vertex shader.
in vec3 color;
// We should set this output param with the desired
// fragment color
out vec4 outColor;
void main() {
// just output the color we got, note we got vec3 (rgb)
// and outColor is vec4, so we use 1.0 for the alpha.
outColor = vec4(color, 1.0);
}
`;
var canvas = document.getElementById('canvas');
// get webgl2 context
var gl = canvas.getContext('webgl2');
// Create shader object of type "VERTEX"
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
// Load the shader source
gl.shaderSource(vertexShader, VERTEX_SHADER);
// Compile the shader
gl.compileShader(vertexShader);
// Check the compile status
var compiled = gl.getShaderParameter(vertexShader, gl. COMPILE_STATUS);
if (!compiled) {
// Something went wrong during compilation; get the error
var lastError = gl.getShaderInfoLog(vertexShader);
throw new Error('Error compiling shader:' + lastError);
}
// Do the same for the fragment shader
const fragShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragShader, FRAG_SHADER);
gl.compileShader(fragShader);
// Check the compile status
compiled = gl.getShaderParameter(fragShader, gl. COMPILE_STATUS);
if (!compiled) {
// Something went wrong during compilation; get the error
var lastError = gl.getShaderInfoLog(fragShader);
throw new Error('Error compiling shader:' + lastError);
}
// create program object
var program = gl.createProgram();
// attach it with the 2 shaders
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragShader);
// and link
gl.linkProgram(program);
// Check the link status
const linked = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!linked) {
// something went wrong with the link
const lastError = gl.getProgramInfoLog(program);
throw new Error('Error linking gl program:', lastError);
}
// First we create a "buffer", which is just a chunk of memory **on the GPU**:
var vertexParamsBuffer = gl.createBuffer();
// Then we should "bind" it, binding in OpenGL is like making something "active"
gl.bindBuffer(gl.ARRAY_BUFFER, vertexParamsBuffer);
var verticesData = new Float32Array([
// coord color
0.0, 0.0, 1.0, 0.0, 0.0, // 1st vertex
0.5, 1.0, 0.0, 1.0, 0.0, // 2nd vertex
1.0, 0.0, 0.0, 0.0, 1.0 // 3ed vertex
]);
// First parameter is the "slot" onto which load the data
// Second parameter is the chunk of bytes
// Third parameter is a hint to OpenGL how this data used
gl.bufferData(gl.ARRAY_BUFFER, verticesData, gl.STATIC_DRAW);
// Gets the index of the a_position input attribute
var a_positionIdx = gl.getAttribLocation(program, 'a_position');
// Fact that we declared "in vec2 a_position" attribute
// in our vertex shader doesn't mean it is "active",
// we have to enable it specifically:
gl.enableVertexAttribArray(a_positionIdx);
// And now we tell OpenGL how to read data from the buffer
// CURRENTLY bound to the ARRAY_BUFFER "slot" into the
// vertex attribute a_position
gl.vertexAttribPointer(a_positionIdx, 2, gl.FLOAT, false, 20, 0);
// same for the color attribute
var a_colorIdx = gl.getAttribLocation(program, 'a_color');
gl.enableVertexAttribArray(a_colorIdx);
gl.vertexAttribPointer(a_colorIdx, 3, gl.FLOAT, false, 20, 8);
// First we set our program as the active program on the gpu
gl.useProgram(program);
// Call the draw method
gl.drawArrays(gl.TRIANGLES, 0, 3);
Also see: Tab Triggers