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.
<!DOCTYPE html>
<html>
<head>
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec4 vColor;
attribute vec2 vTexCoord;
varying vec4 fColor;
varying vec2 fTexCoord;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
void main()
{
gl_Position = projectionMatrix*modelViewMatrix*vPosition;
fColor = vColor;
fTexCoord = vTexCoord;
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 fColor;
varying vec2 fTexCoord;
uniform sampler2D texture;
void
main()
{
gl_FragColor = fColor * texture2D( texture, fTexCoord );
}
</script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/webgl-utils.js"></script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/initShaders.js"></script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/MV.js"></script>
</head>
<body>
<canvas id="gl-canvas" width="512" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
<br />
Move:
<button id = "Button3">Farther</button><span id="distance"></span>
<button id = "Button4">Closer</button>
<button id = "Button6">Up</button><span id="theta"></span>
<button id = "Button5">Down</button>
<button id = "Button8">Left</button><span id="phi"></span>
<button id = "Button7">Right</button>
<div>
<input type="checkbox" id="perspective" name="perspective"
checked>
<label for="perspective">Use perspective</label>
</div>
</body>
<img crossorigin="anonymous" id="texImage" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2999896/Brooklyn_College_Seal.png" hidden></img>
</html>
var gl;
var program;
var points = []; // All vertices to be drawn (including duplicates)
var colors = []; // Colors of all vertices to be drawn (including duplicates)
var texCoords = []; // Texture coordinates for each vertex
var near = 0.001;
var far = 1000.0;
var radius = 3.0;
var theta = 60 * Math.PI/180;
var phi = 30 * Math.PI/180;
var dr = 5.0 * Math.PI/180.0;
var fovy = 45.0; // Field-of-view in Y direction angle (in degrees)
var aspect; // Viewport aspect ratio
var modelViewMatrix, projectionMatrix;
var modelViewMatrixLoc, projectionMatrixLoc;
var eye;
const at = vec3(0.0, 0.0, 0.0);
const up = vec3(0.0, 1.0, 0.0);
var usePerspective = true;
var texture;
var texCoordsBase = [
vec2(0, 0),
vec2(1, 0),
vec2(1, 1),
vec2(0, 1)
];
var vertices = [
vec3( -0.5, -0.5, 0.5 ),
vec3( -0.5, 0.5, 0.5),
vec3( 0.5, 0.5, 0.5),
vec3( 0.5, -0.5, 0.5),
vec3( -0.5, -0.5, -0.5),
vec3( -0.5, 0.5, -0.5),
vec3( 0.5, 0.5, -0.5),
vec3( 0.5, -0.5, -0.5)
];
var vertexColors = [
vec4( 1.0, 1.0, 1.0, 1.0 ), // white
vec4( 1.0, 0.0, 0.0, 1.0 ), // red
vec4( 1.0, 1.0, 0.0, 1.0 ), // yellow
vec4( 0.0, 1.0, 0.0, 1.0 ), // green
vec4( 0.0, 0.0, 1.0, 1.0 ), // blue
vec4( 1.0, 0.0, 1.0, 1.0 ), // magenta
vec4( 0.0, 0.0, 0.0, 1.0 ), // black
vec4( 0.0, 1.0, 1.0, 1.0 ) // cyan
];
function makeCube( )
{
makeCubeFace(0,3,2,1);
makeCubeFace(2,3,7,6);
makeCubeFace(3,0,4,7);
makeCubeFace(1,2,6,5);
makeCubeFace(4,5,6,7);
makeCubeFace(5,4,0,1);
}
function makeCubeFace(a, b, c, d)
{
var inds = [ a, b, c, a, c, d ];
var texInds = [0, 1, 2, 0, 2, 3];
for ( var i = 0; i < inds.length; ++i ) {
points.push( vertices[inds[i]]);
// Color by vertex
//colors.push( vertexColors[inds[i]] );
// Color by face
colors.push(vertexColors[a]);
// Texture
texCoords.push(texCoordsBase[texInds[i]]);
}
}
function checkerboardImage(texSize) {
var image1 = new Array()
for (var i =0; i<texSize; i++) { image1[i] = new Array(); }
for (var i =0; i<texSize; i++) {
for ( var j = 0; j < texSize; j++) {
image1[i][j] = new Float32Array(4);
}
}
for (var i =0; i<texSize; i++) for (var j=0; j<texSize; j++) {
var c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0));
image1[i][j] = [c, c, c, 1];
}
// Convert floats to ubytes for texture
var image2 = new Uint8Array(4*texSize*texSize);
for ( var i = 0; i < texSize; i++ ) {
for ( var j = 0; j < texSize; j++ ) {
for(var k =0; k<4; k++) {
image2[4*texSize*i+4*j+k] = 255*image1[i][j][k];
}
}
}
return image2;
}
function configureTexture( image, isImage, texSize ) {
texture = gl.createTexture();
gl.bindTexture( gl.TEXTURE_2D, texture );
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
if (isImage) {
gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB,
gl.RGB, gl.UNSIGNED_BYTE, image );
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize, texSize, 0,
gl.RGBA, gl.UNSIGNED_BYTE, image);
}
gl.generateMipmap( gl.TEXTURE_2D );
gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER,
gl.LINEAR_MIPMAP_LINEAR );
gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR );
gl.uniform1i(gl.getUniformLocation(program, "texture"), 0);
}
function updateDisplays() {
document.getElementById("distance").innerText = radius.toFixed(2);
document.getElementById("theta").innerText = (theta * 180 / Math.PI).toFixed(2);
document.getElementById("phi").innerText = (phi * 180 / Math.PI).toFixed(2);
}
function init() {
var canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("WebGL isn't available");
}
updateDisplays();
document.getElementById("Button3").onclick = function(){ radius *= 5/4; updateDisplays(); };
document.getElementById("Button4").onclick = function(){ radius *= 4/5; updateDisplays();};
document.getElementById("Button5").onclick = function(){ theta += dr; updateDisplays(); };
document.getElementById("Button6").onclick = function(){ theta -= dr; updateDisplays(); };
document.getElementById("Button7").onclick = function(){ phi += dr; updateDisplays(); };
document.getElementById("Button8").onclick = function(){ phi -= dr; updateDisplays(); };
document.getElementById("perspective").onclick = function(){ usePerspective = document.getElementById("perspective").checked; };
// Configure WebGL
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.enable(gl.DEPTH_TEST); // <--- New
// Set aspect ratio of canvas
aspect = canvas.width/canvas.height;
// Load shaders and initialize attribute buffers
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
// Populate vertices and colors
makeCube();
// Load vertex data into the GPU
var bufferId = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
// Associate vertex shader variables with data buffer
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
// Load color data into the GPU
bufferId = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
// Associate color shader variables with our data buffer
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
// Configure texture buffers
var tBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, tBuffer );
gl.bufferData( gl.ARRAY_BUFFER, flatten(texCoords), gl.STATIC_DRAW );
var vTexCoord = gl.getAttribLocation( program, "vTexCoord" );
gl.vertexAttribPointer( vTexCoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vTexCoord );
// Option 1: Texture from hard-coded URL
// var image = new Image();
// image.onload = function() {
// configureTexture( image, 1, image.width );
// }
// image.crossOrigin = "anonymous";
// image.src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2999896/brickwork-texture.jpg"
// From: http://www.bricksntiles.com/textures/
// Option 2: Texture from hidden <img> element
var image = document.getElementById("texImage");
window.onload = function() { configureTexture( image, 1, image.width ); };
// Option 3: Generate texture image procedurally
// var image2 = checkerboardImage(256);
// configureTexture( image2, 0, 256 )
// Handles to send matrices
modelViewMatrixLoc = gl.getUniformLocation( program, "modelViewMatrix" );
projectionMatrixLoc = gl.getUniformLocation( program, "projectionMatrix" );
// Go
render();
}
function render(){
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
eye = vec3(
radius*(0.05 + Math.sin(theta)*Math.sin(phi)),
radius*Math.cos(theta),
radius*Math.sin(theta)*Math.cos(phi)
);
modelViewMatrix = lookAt(eye, at, up);
if (usePerspective) {
projectionMatrix = perspective(fovy, aspect, near, far);
} else {
projectionMatrix = ortho(-radius/3, radius/3, -radius/3, radius/3, near, far);
}
gl.uniformMatrix4fv( modelViewMatrixLoc, false, flatten(modelViewMatrix) );
gl.uniformMatrix4fv( projectionMatrixLoc, false, flatten(projectionMatrix) );
gl.drawArrays( gl.TRIANGLES, 0, points.length );
requestAnimFrame(render);
}
init();
Also see: Tab Triggers