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.
/**
* Combining PIXI.js and THREE.js in one <canvas> Element
*
* Check the DOM, There's just one!
*
* The rotating image on top is just a PIXI Sprite to show that PIXI is present, ;-)
*
* Thanx and respect to Luigi Mannoni for the awesome THREE WebGL Tunnel
* I've changed almost nothing. Only added some PIXI things in your THREE scene function
* @see the original at: https://codepen.io/luigimannoni/pen/bdPVVz
*/
// Setup a PIXI stage and add it (canvas element) to the DOM
// settings don't really matter for the combi, only the dimensions
PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.LINEAR;
this.app = new PIXI.Application( window.innerWidth, window.innerHeight , {
//backgroundColor : 0x000000,
//transparent : true,
//resolution : 1,
antialias : false,
legacy : true,
clearBeforeRender : true,
autoResize : true,
powerPreference : 'high-performance'
});
document.body.appendChild(this.app.view);
// Set up the THREE scene that will be 'integrated' in PIXI.JS
function deg2rad(_degrees) {
return (_degrees * Math.PI / 180);
}
var mouseX = 0;
var mouseY = 0;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
var innerColor = 0x2222ff;
var cubecam = new THREE.CubeCamera(0.1, 120, 256);
cubecam.renderTarget.minFilter = THREE.LinearMipMapLinearFilter;
scene.add(cubecam);
// setup THREE WebGL renderer
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor(0x000000, 0);
renderer.setSize(window.innerWidth, window.innerHeight);
/**
* For the combi to work with only one <canvas> element,
* Do not add the THREE <canvas> element to the DOM
*
* document.body.appendChild(renderer.domElement); // Leave this out!
*
*/
camera.position.z = -110;
camera.lookAt(scene.position);
// Mesh
var group = new THREE.Group();
scene.add(group);
// Lights
var light = new THREE.AmbientLight(0x404040); // Soft white light
scene.add(light);
var directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(0, 128, 128);
scene.add(directionalLight);
// Load texture first
THREE.ImageUtils.crossOrigin = '';
var tunnelTexture = THREE.ImageUtils.loadTexture('https://luigimannoni.github.io/assets/001_electric.jpg');
tunnelTexture.wrapT = tunnelTexture.wrapS = THREE.RepeatWrapping;
tunnelTexture.repeat.set(1, 2);
// Tunnel Mesh
var tunnelMesh = new THREE.Mesh(
new THREE.CylinderGeometry(50, 50, 1024, 16, 32, true),
new THREE.MeshBasicMaterial({
color : innerColor,
ambient : innerColor,
transparent : true,
alphaMap : tunnelTexture,
shininess : 0,
side : THREE.BackSide
})
);
tunnelMesh.rotation.x = deg2rad(90);
tunnelMesh.position.z = 128;
scene.add(tunnelMesh);
// Cube Mesh
var cubeMesh = new THREE.Mesh(
new THREE.BoxGeometry(32, 32, 32),
new THREE.MeshPhongMaterial({
color : 0xffffff,
ambient : 0xffffff,
transparent : false,
envMap : cubecam.renderTarget,
shininess : 100
})
);
cubecam.position.z = cubeMesh.position.z = 5;
scene.add(cubeMesh);
// Starfield
var geometry = new THREE.Geometry();
for(i = 0; i < 5000; i++) {
var vertex = new THREE.Vector3();
vertex.x = Math.random() * 3000 - 1500;
vertex.y = Math.random() * 3000 - 1500;
vertex.z = Math.random() * 200 - 100;
geometry.vertices.push(vertex)
}
var starField = new THREE.PointCloud(
geometry,
new THREE.PointCloudMaterial({
size : 0.5,
color : 0xffff99
})
);
scene.add(starField);
starField.position.z = 400;
var time = new THREE.Clock();
/**
* Here comes PIXI again:
* Creating a PIXI.BaseTexture from the THREE scene
*
* 'renderer.domElement' is the what would normally be added by THREE as/in a <canvas> to the DOM
*/
var THREE_TEXTURE = PIXI.BaseTexture.fromCanvas(renderer.domElement, PIXI.SCALE_MODES.LINEAR);
var THREE_SPRITE = new PIXI.Sprite.from(new PIXI.Texture(THREE_TEXTURE));
this.app.stage.addChild(THREE_SPRITE);
// Adding just some PIXI sprite based on the same image the Tunnel is based on:
// (animating it later on a little)
var somePIXIsprite = new PIXI.Sprite.fromImage('https://luigimannoni.github.io/assets/001_electric.jpg');
somePIXIsprite.scale.set(0.2);
somePIXIsprite.x = window.innerWidth/3;
somePIXIsprite.y = window.innerHeight/3;
somePIXIsprite.anchor.set(0.5);
this.app.stage.addChild(somePIXIsprite);
// Then a requestAnimationFrame function, that will loop.... forever
// Inside THREE does its thing, animating the WebGL tunnel
// And because of PIXI's `RIDE THE LIGHTNING` SPEED .. it (PIXI) captures THREE's changes every frame, 60 times a second, and....
var render = function() {
// First THREE
camera.position.x = mouseX * 0.05;
camera.position.y = -mouseY * 0.05;
camera.lookAt(scene.position);
cubeMesh.rotation.x += 0.01;
cubeMesh.rotation.y += 0.01;
cubeMesh.rotation.z += 0.01;
starField.rotation.z += 0.005;
var innerShift = Math.abs(Math.cos(((time.getElapsedTime() + 2.5) / 20)));
var outerShift = Math.abs(Math.cos(((time.getElapsedTime() + 5) / 10)));
starField.material.color.setHSL(Math.abs(Math.cos((time.getElapsedTime() / 10))), 1, 0.8);
tunnelMesh.material.color.setHSL(Math.abs(Math.cos((time.getElapsedTime() / 10))), 1, 0.5);
tunnelTexture.offset.y = time.getElapsedTime() / 2;
tunnelTexture.offset.x = time.getElapsedTime() / 6;
cubeMesh.visible = false;
cubecam.updateCubeMap(renderer, scene);
cubeMesh.visible = true;
// THREE Renders (updates its scene) at 60 FPS
renderer.render(scene, camera);
/**
* And here is it,
* one simple short magic piece of code
* PIXI updates the created BaseTexture every Frame, at 60 FPS
*/
THREE_TEXTURE.update();
// animate the PIXI Sprite image a little
somePIXIsprite.rotation += 0.01;
// making the Render function loop
requestAnimationFrame(render)
};
render(); // init the render (requestAnimationFrame) function
// Then some mouse and resize events
// These will remain to work, because PIXI renders and captures every move THREE makes
document.addEventListener('mousemove', onDocumentMouseMove, false);
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
var vw = window.innerWidth;
var vh = window.innerHeight;
camera.aspect = vw / vh;
camera.updateProjectionMatrix();
renderer.setSize(vw, vh);
// Resizing for PIXI
this.app.renderer.resize(vw, vh);
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - window.innerWidth/2;
mouseY = event.clientY - window.innerHeight/2;
}
// That's it!!!
Also see: Tab Triggers