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.
<div id="space"></div>
<!--
this is a fucking WORK IN PROGRESS
please check back in the next few days
Credits for the Millennium Falcon design goes to: Anthony Dugois
-->
<p id="crawl-title">Episode IV</p>
<p id="crawl-subtitle">A NEW HOPE</p>
<p id="crawl-text">It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire. During the battle, Rebel spies managed to steal secret plans to the Empire's ultimate weapon, the DEATH STAR, an armored space station with enough power to destroy an entire planet. Pursued by the Empire's sinister agents, Princess Leia races home aboard her starship, custodian of the stolen plans that can save her people and restore freedom to the galaxy....</p>
#space {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden;
background: #111;
}
p { display: none; }
var PI = Math.PI
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var RATIO = WIDTH / HEIGHT;
// camera, scene and renderer
var FIELDVIEW = 60;
var NEAR = 1;
var FAR = 5000;
var CAMERA = new THREE.PerspectiveCamera(FIELDVIEW, RATIO, NEAR, FAR);
var SCENE = new THREE.Scene();
var RENDERER = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
});
// update the scene ration on window resize
window.onresize = function() {
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;
RATIO = WIDTH / HEIGHT;
RENDERER.setSize(WIDTH, HEIGHT);
CAMERA.aspect = RATIO;
CAMERA.updateProjectionMatrix();
}
function loop() {
render();
setAcceleration();
requestAnimationFrame(loop);
}
function render() {
RENDERER.render(SCENE, CAMERA);
}
// lights
function createLights() {
var hemisphereLight = new THREE.HemisphereLight(0xffffff);
SCENE.add(hemisphereLight);
}
function init() {
RENDERER.setSize(WIDTH, HEIGHT);
document.getElementById('space').appendChild(RENDERER.domElement);
// camera
CAMERA.position.set(-500, 500, 0); //scene1
CAMERA.lookAt(new THREE.Vector3(500, 0, 0));
// CAMERA.position.set(-300, 200, 0); //scene2
// CAMERA.lookAt(new THREE.Vector3(100, 0, 0));
//CAMERA.position.set(600, 1500, 0); //scene3
//CAMERA.lookAt(new THREE.Vector3(601, 0, 0));
//CAMERA.position.set(-700, 700, 500);
//CAMERA.lookAt(new THREE.Vector3(-300, 0, 500));
createLights();
window.millenniumFalcon = new THREE.Object3D();
millenniumFalcon.add(createBody());
millenniumFalcon.add(createRescueCapsules());
millenniumFalcon.add(createCenterCylinder());
millenniumFalcon.add(createBackCylinders());
millenniumFalcon.add(createFronts());
millenniumFalcon.add(createCockpit());
millenniumFalcon.add(createUndercarriages());
millenniumFalcon.add(createParable());
SCENE.add(millenniumFalcon);
//SCENE.add(createText());
// move the Falcon to mouse coords
window.onmousemove = handleMouseMove;
// looping
window.onclick = handleClick;
// shoot
window.onkeyup = handleKeyup;
loop();
}
init();
//__________________________________
// text
function createText() {
var crawlText = new THREE.Object3D();
var textMaterial = new THREE.MeshLambertMaterial({
color: 0xfbd62a,
//shading: THREE.FlatShading,
});
var maxLineWidth = 1100;
var lineSpacing = 80; //spacing between the line height center and the next
var currentLineCenter = 20;
var minWordSpacing = finalWordSpacing = 40;
var line = {
content: [],
currentWordWidth: [],
wordsWidth: 0,
wordsNumber: 0
};
// POSITION TITLE
var title = document.getElementById('crawl-title').innerText.split(' ');
title.forEach(function(word) {
var textGeo = new THREE.TextGeometry(word, {
size: 90,
height: 20,
//curveSegments: 4,
font: 'franklin gothic book',
//bevelThickness: 2,
//bevelSize: 1.5,
//bevelEnabled: true,
//material: 0,
//extrudeMaterial: 1
});
var textMesh = new THREE.Mesh( textGeo, textMaterial );
textGeo.computeBoundingBox();
var wordWidth = textGeo.boundingBox.max.x - textGeo.boundingBox.min.x;
line.wordsWidth += wordWidth;
line.currentWordWidth.push(wordWidth);
line.wordsNumber += 1;
line.content.push(textMesh);
crawlText.add(textMesh);
});
positionWords(line.content, line.currentWordWidth, minWordSpacing, line.wordsWidth);
newLine(2);
//POSITION SUBTITLE
var subtitle = document.getElementById('crawl-subtitle').innerText.split(' ');
subtitle.forEach(function(word) {
var textGeo = new THREE.TextGeometry(word, {
size: 90,
height: 20,
//curveSegments: 4,
font: 'franklin gothic book',
//bevelThickness: 2,
//bevelSize: 1.5,
//bevelEnabled: true,
//material: 0,
//extrudeMaterial: 1
});
var textMesh = new THREE.Mesh( textGeo, textMaterial );
textGeo.computeBoundingBox();
var wordWidth = textGeo.boundingBox.max.x - textGeo.boundingBox.min.x;
line.wordsWidth += wordWidth;
line.currentWordWidth.push(wordWidth);
line.wordsNumber += 1;
line.content.push(textMesh);
crawlText.add(textMesh);
});
positionWords(line.content, line.currentWordWidth, minWordSpacing, line.wordsWidth);
newLine(2);
// POSITION TEXT
var text = document.getElementById('crawl-text').innerText.split(' ');
text.forEach(function(word) {
var textGeo = new THREE.TextGeometry(word, {
size: 70,
height: 20,
//curveSegments: 4,
font: 'franklin gothic book',
//bevelThickness: 2,
//bevelSize: 1.5,
//bevelEnabled: true,
//material: 0,
//extrudeMaterial: 1
});
var textMesh = new THREE.Mesh( textGeo, textMaterial );
textGeo.computeBoundingBox();
var wordWidth = textGeo.boundingBox.max.x - textGeo.boundingBox.min.x;
var isEndOfParagraph = word.slice(-1) === '.' && word.slice(-2, -1) !== 'r';
var isEndOfLine = line.wordsWidth + wordWidth + (line.wordsNumber - 1) * minWordSpacing >= maxLineWidth;
if(isEndOfLine && isEndOfParagraph) {
line.wordsWidth += wordWidth;
line.currentWordWidth.push(wordWidth);
line.wordsNumber += 1;
line.content.push(textMesh);
finalWordSpacing = (maxLineWidth - line.wordsWidth) / (line.wordsNumber - 1);
positionWords(line.content, line.currentWordWidth, finalWordSpacing);
newLine(2);
} else if(isEndOfLine) {
finalWordSpacing = (maxLineWidth - line.wordsWidth) / (line.wordsNumber - 1);
positionWords(line.content, line.currentWordWidth, finalWordSpacing);
newLine(1);
} else if(isEndOfParagraph) {
line.wordsWidth += wordWidth;
line.currentWordWidth.push(wordWidth);
line.wordsNumber += 1;
line.content.push(textMesh);
positionWords(line.content, line.currentWordWidth, minWordSpacing);
newLine(2);
}
if(!isEndOfParagraph) {
line.wordsWidth += wordWidth;
line.currentWordWidth.push(wordWidth);
line.wordsNumber += 1;
line.content.push(textMesh);
}
crawlText.add(textMesh); //TODO try adding this out of the loop
});
function positionWords(textMeshes, currentWordWidth, spacing, wordsWidth) {
if(wordsWidth) {
var positionCounter = maxLineWidth / 2 - (wordsWidth + spacing * (textMeshes.length - 1)) / 2; //simulate text-align: center
} else {
var positionCounter = 0;
}
textMeshes.forEach(function(el, i) {
el.position.x = positionCounter;
el.position.y = - currentLineCenter;
positionCounter += currentWordWidth[i] + spacing;
});
}
function newLine(howMany) {
line.wordsWidth = 0;
line.currentWordWidth = [];
line.wordsNumber = 0;
line.content = [];
currentLineCenter += lineSpacing * howMany;
}
// POSITION ADJUSTMENTS
//smoother text
//crawlText.computeVertexNormals();
//center text
var boundingBox = new THREE.Box3().setFromObject(crawlText);
var centerOffset = - ( boundingBox.max.x - boundingBox.min.x ) / 2;
crawlText.position.z = centerOffset;
crawlText.position.y = 400;
crawlText.position.x = 200;
crawlText.rotation.z = - PI / 2;
crawlText.rotation.x = - PI / 2;
//handle text shadows
// three js gradient as texture
// https://stemkoski.github.io/Three.js/Text3D-Textures.html
// https://gist.github.com/ekeneijeoma/1186920
return crawlText;
}
function refreshText() {
SCENE.remove( textMesh );
createText();
}
//__________________________________
// behaviour
var isLooping = false;
var horizontalCenter = WIDTH / 2;
var verticalCenter = HEIGHT / 2;
var mouseX, mouseY;
function handleKeyup(e) {
if (e.keyCode == 32) {
shoot();
}
}
function handleClick(e) {
shoot();
//loopityLoop(e);
}
function handleMouseMove(e) {
mouseX = e.pageX;
mouseY = e.pageY;
/*
switch(scene) {
case 1:
mouseFar();
break;
case 2:
mouseClose();
break;
case 3:
mouseMiddle();
break;
}
*/
mouseFar();
//mouseClose();
}
function mouseFar() {
var speed = 1;
var posZ = (mouseX - horizontalCenter) / 1.5;
TweenLite.to(millenniumFalcon.position, speed, {
z: posZ
});
var posX = - (mouseY - verticalCenter) / verticalCenter * 50;
TweenLite.to(millenniumFalcon.position, speed, {
x: posX
});
}
function mouseClose() {
var speed = 1;
/*
* x = cos(phi) * h
* <=> x / h = cos(phi)
* <=> phi = arccos(x / h)
* y = h - sin(phi) * h
* <=> y = h * (1 - sin(arccos(x / h)))
*/
var posZ = (mouseX - horizontalCenter) / 3.5;
var posY = horizontalCenter * (1 - Math.sin(Math.acos(Math.abs(posZ) / horizontalCenter)));
TweenLite.to(millenniumFalcon.position, speed, {
z: posZ,
y: posY
});
var mAngle = PI / 8;
var mXrotation = -posZ / horizontalCenter * 3.5 * PI / 3;
if ( ! isLooping) {
TweenLite.to(millenniumFalcon.rotation, speed, {
x: mXrotation
});
}
var posX = - (mouseY - verticalCenter) / verticalCenter * 50;
TweenLite.to(millenniumFalcon.position, speed, {
x: posX
});
}
function setAcceleration() {
var speed = 0.5;
var posZ = (mouseX - horizontalCenter) / 1.5;
var maxAngle = PI / 3;
var mXrotation = (posZ - millenniumFalcon.position.z) / horizontalCenter * maxAngle * 2;
if ( ! isLooping) {
TweenLite.to(millenniumFalcon.rotation, speed, {
x: mXrotation
});
}
}
function shoot() {
var speed = 0.5;
var blasts = new THREE.Object3D();
var blastTop = new THREE.Object3D();
var blastTopPos = new THREE.Vector3()
.setFromMatrixPosition(shooterTop.matrixWorld);
var blastWidth = 25;
var blastCenter = new THREE.Mesh(
new THREE.CylinderGeometry(.5, .5, blastWidth, 16),
new THREE.MeshLambertMaterial({
color: 0xffffff
})
);
var blastHalo = new THREE.Mesh(
new THREE.CylinderGeometry(1.6, 1.6, blastWidth + 4, 16),
haloMaterial(0xff0000)
);
blastTop.add(blastCenter);
blastTop.add(blastHalo);
blastTop.rotation.z = -PI / 2;
blastTop.position.x = blastTopPos.x + blastWidth / 2 + 20;
blastTop.position.y = blastTopPos.y + 3;
blastTop.position.z = blastTopPos.z;
blasts.add(blastTop);
var blastBottomPos = new THREE.Vector3()
.setFromMatrixPosition(shooterBottom.matrixWorld);
var blastBottom = blastTop.clone();
blastBottom.rotation.z = -PI / 2;
blastBottom.position.x = blastBottomPos.x + blastWidth / 2 + 20;
blastBottom.position.y = blastBottomPos.y - 3;
blastBottom.position.z = blastBottomPos.z;
blasts.add(blastBottom);
SCENE.add(blasts);
TweenLite.to(blasts.position, speed, {
x: 1000,
ease: Linear.ease,
onComplete: function() {
SCENE.remove(blasts)
}
});
}
function loopityLoop(e) {
if ( ! isLooping) {
isLooping = true;
var originalXrotation = millenniumFalcon.rotation.x;
var speed = 2;
var mXrotation = originalXrotation;
if (e.pageX <= horizontalCenter) {
mXrotation -= 2 * PI;
} else {
mXrotation += 2 * PI;
}
TweenLite.to(millenniumFalcon.rotation, speed, {
x: mXrotation,
ease: Back.easeOut,
onComplete: function() {
millenniumFalcon.rotation.x = originalXrotation
isLooping = false
}
});
}
}
Also see: Tab Triggers