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 class="world">
<div class="world-bg"></div>
<div class="world-globe">
<div class="world-globe-pole"></div>
<div class="world-globe-doms-container"></div>
<div class="world-globe-halo"></div>
</div>
</div>
<div class="info">
<div class="info-title">CSS Non-webgl realistic globe Demo</div>
<div class="info-desc">Example for my meetup talk. Use <a href="https://github.com/edankwan/PerspectiveTransform.js" target="_blank">PerspectiveTransform</a> and visual trick to create a CSS 3D globe.</div>
<div class="info-links"><a href="https://twitter.com/edankwan" target="_blank">@edankwan</a></div>
</div>
@import "compass/css3";
html, body {
position: absolute;
width: 100%;
height: 100%;
margin: 0 0;
overflow: hidden;
font-family: 'Lato', sans-serif;
background-color: #000;
color: #fff;
}
.world {
position: absolute;
width: 100%;
height: 100%;
cursor: pointer;
cursor: move;
cursor: -moz-grab;
cursor: -webkit-grab;
cursor: grab;
}
.world-bg {
position: absolute;
width: 100%;
height: 100%;
background-position: 50% 50%;
background-size: cover;
}
.world-globe {
position: absolute;
left: 50%;
top: 50%;
width: 0;
height: 0;
}
.world-globe-pole {
position: absolute;
width: 530px;
height: 530px;
left: -265px;
top: -265px;
border-radius: 50% 50%;
background-color: #fff;
}
.world-globe-doms-container {
position: absolute;
left: 50%;
top: 50%;
width: 0;
height: 0;
}
.world-globe-halo {
position: absolute;
left: 50%;
top: 50%;
width: 730px;
height: 715px;
margin-left: -368px;
margin-top: -350px;
}
.info {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
padding: 10px 10px;
box-sizing: border-box;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
font-size: 12px;
}
.info-desc {
color: #ddd;
font-size: 10px;
}
a {
color: #ff5f5f;
}
var config = {
percent: 0,
lat: 0,
lng: 0,
segX: 14,
segY: 12,
isHaloVisible: true,
isPoleVisible: true,
autoSpin: true,
zoom: 0,
skipPreloaderAnimation: false,
goToHongKong: function() {
goTo(22.28552,114.15769);
}
};
var stats;
var imgs;
var preloader;
var preloadPercent;
var globeDoms;
var vertices;
var world;
var worldBg;
var globe;
var globeContainer;
var globePole;
var globeHalo;
var pixelExpandOffset = 1.5;
var rX = 0;
var rY = 0;
var rZ = 0;
var sinRX;
var sinRY;
var sinRZ;
var cosRX;
var cosRY;
var cosRZ;
var dragX;
var dragY;
var dragLat;
var dragLng;
var isMouseDown = false;
var isTweening = false;
var tick = 1;
var URLS = {
bg: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/6043/css_globe_bg.jpg',
diffuse: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/6043/css_globe_diffuse.jpg',
halo: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/6043/css_globe_halo.png',
};
var transformStyleName = PerspectiveTransform.transformStyleName;
function init(ref) {
world = document.querySelector('.world');
worldBg = document.querySelector('.world-bg');
worldBg.style.backgroundImage = 'url(' + URLS.bg + ')';
globe = document.querySelector('.world-globe');
globeContainer = document.querySelector('.world-globe-doms-container');
globePole = document.querySelector('.world-globe-pole');
globeHalo = document.querySelector('.world-globe-halo');
globeHalo.style.backgroundImage = 'url(' + URLS.halo + ')';
regenerateGlobe();
var gui = new dat.GUI();
gui.add(config, 'lat', -90, 90).listen();
gui.add(config, 'lng', -180, 180).listen();
gui.add(config, 'isHaloVisible');
gui.add(config, 'isPoleVisible');
gui.add(config, 'autoSpin');
gui.add(config, 'goToHongKong');
gui.add(config, 'zoom', 0, 1).listen();
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = 0;
stats.domElement.style.top = 0;
document.body.appendChild( stats.domElement );
// events
world.ondragstart = function () {return false;};
world.addEventListener('mousedown', onMouseDown);
world.addEventListener('mousemove', onMouseMove);
world.addEventListener('mouseup', onMouseUp);
world.addEventListener('touchstart', touchPass(onMouseDown));
world.addEventListener('touchmove', touchPass(onMouseMove));
world.addEventListener('touchend', touchPass(onMouseUp));
loop();
}
function touchPass(func) {
return function(evt) {
evt.preventDefault();
func.call(this, {pageX: evt.changedTouches[0].pageX, pageY: evt.changedTouches[0].pageY});
};
}
function onMouseDown(evt) {
isMouseDown = true;
dragX = evt.pageX;
dragY = evt.pageY;
dragLat = config.lat;
dragLng = config.lng;
}
function onMouseMove(evt) {
if(isMouseDown) {
var dX = evt.pageX - dragX;
var dY = evt.pageY - dragY;
config.lat = clamp(dragLat + dY * 0.5, -90, 90);
config.lng = clampLng(dragLng - dX * 0.5, -180, 180);
}
}
function onMouseUp(evt) {
if(isMouseDown) {
isMouseDown = false;
}
}
function regenerateGlobe() {
var dom, domStyle;
var x, y;
globeDoms = [];
while (dom = globeContainer.firstChild) {
globeContainer.removeChild(dom);
}
var segX = config.segX;
var segY = config.segY;
var diffuseImgBackgroundStyle = 'url(' + URLS.diffuse + ')';
var segWidth = 1600 / segX | 0;
var segHeight = 800 / segY | 0;
vertices = [];
var verticesRow;
var radius = (536) / 2;
var phiStart = 0;
var phiLength = Math.PI * 2;
var thetaStart = 0;
var thetaLength = Math.PI;
for ( y = 0; y <= segY; y ++ ) {
verticesRow = [];
for ( x = 0; x <= segX; x ++ ) {
var u = x / segX;
var v = 0.05 + y / segY * (1 - 0.1);
var vertex = {
x: - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ),
y: -radius * Math.cos( thetaStart + v * thetaLength ),
z: radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ),
phi: phiStart + u * phiLength,
theta: thetaStart + v * thetaLength
};
verticesRow.push( vertex );
}
vertices.push( verticesRow );
}
for ( y = 0; y < segY; ++y ) {
for ( x = 0; x < segX; ++x ) {
dom = document.createElement('div');
domStyle = dom.style;
domStyle.position = 'absolute';
domStyle.width = segWidth + 'px';
domStyle.height = segHeight + 'px';
domStyle.overflow = 'hidden';
domStyle[PerspectiveTransform.transformOriginStyleName] = '0 0';
domStyle.backgroundImage = diffuseImgBackgroundStyle;
dom.perspectiveTransform = new PerspectiveTransform(dom , segWidth, segHeight);
dom.topLeft = vertices[ y ][ x ];
dom.topRight = vertices[ y ][ x + 1];
dom.bottomLeft = vertices[ y + 1 ][ x ];
dom.bottomRight = vertices[ y + 1 ][ x + 1 ];
domStyle.backgroundPosition = (-segWidth * x) + 'px ' + (-segHeight * y) + 'px';
globeContainer.appendChild(dom);
globeDoms.push(dom);
}
}
}
function loop() {
requestAnimationFrame(loop);
stats.begin();
render();
stats.end();
}
function render() {
if(config.autoSpin && !isMouseDown && !isTweening) {
config.lng = clampLng(config.lng - 0.2);
}
rX = config.lat / 180 * Math. PI;
rY = (clampLng(config.lng) - 270) / 180 * Math. PI;
globePole.style.display = config.isPoleVisible ? 'block' : 'none';
globeHalo.style.display = config.isHaloVisible ? 'block' : 'none';
var ratio = Math.pow(config.zoom, 1.5);
pixelExpandOffset = 1.5 + (ratio) * -1.25;
ratio = 1 + ratio * 3;
globe.style[transformStyleName] = 'scale3d(' + ratio + ',' + ratio + ',1)';
ratio = 1 + Math.pow(config.zoom, 3) * 0.3;
worldBg.style[transformStyleName] = 'scale3d(' + ratio + ',' + ratio + ',1)';
transformGlobe();
}
function clamp(x, min, max) {
return x < min ? min : x > max ? max : x;
}
function clampLng(lng) {
return ((lng + 180) % 360) - 180;
}
function transformGlobe() {
var dom, perspectiveTransform;
var x, y, v1, v2, v3, v4, vertex, verticesRow, i, len;
if(tick ^= 1) {
sinRY = Math.sin(rY);
sinRX = Math.sin(-rX);
sinRZ = Math.sin(rZ);
cosRY = Math.cos(rY);
cosRX = Math.cos(-rX);
cosRZ = Math.cos(rZ);
var segX = config.segX;
var segY = config.segY;
for ( y = 0; y <= segY; y ++ ) {
verticesRow = vertices[y];
for ( x = 0; x <= segX; x ++ ) {
rotate(vertex = verticesRow[x], vertex.x, vertex.y, vertex.z);
}
}
for ( y = 0; y < segY; y ++ ) {
for ( x = 0; x < segX; x ++ ) {
dom = globeDoms[x + segX * y];
v1 = dom.topLeft;
v2 = dom.topRight;
v3 = dom.bottomLeft;
v4 = dom.bottomRight;
expand(v1, v2);
expand(v2, v3);
expand(v3, v4);
expand(v4, v1);
perspectiveTransform = dom.perspectiveTransform;
perspectiveTransform.topLeft.x = v1.tx;
perspectiveTransform.topLeft.y = v1.ty;
perspectiveTransform.topRight.x = v2.tx;
perspectiveTransform.topRight.y = v2.ty;
perspectiveTransform.bottomLeft.x = v3.tx;
perspectiveTransform.bottomLeft.y = v3.ty;
perspectiveTransform.bottomRight.x = v4.tx;
perspectiveTransform.bottomRight.y = v4.ty;
perspectiveTransform.hasError = perspectiveTransform.checkError();
if(!(perspectiveTransform.hasError = perspectiveTransform.checkError())) {
perspectiveTransform.calc();
}
}
}
} else {
for ( i = 0, len = globeDoms.length; i < len; i ++ ) {
perspectiveTransform = globeDoms[i].perspectiveTransform;
if(!perspectiveTransform.hasError) {
perspectiveTransform.update();
} else {
perspectiveTransform.style[transformStyleName] = 'translate3d(-8192px, 0, 0)';
}
}
}
}
function goTo(lat, lng) {
var dX = lat - config.lat;
var dY = lng - config.lng;
var roughDistance = Math.sqrt(dX * dX + dY * dY);
isTweening = true;
TweenMax.to(config, roughDistance * 0.01, {lat: lat, lng: lng, ease:'easeInOutSine'});
TweenMax.to(config, 1, {delay: roughDistance * 0.01, zoom: 1, ease:'easeInOutSine', onComplete: function(){
isTweening = false;
}});
}
function rotate(vertex, x, y, z) {
x0 = x * cosRY - z * sinRY;
z0 = z * cosRY + x * sinRY;
y0 = y * cosRX - z0 * sinRX;
z0 = z0 * cosRX + y * sinRX;
var offset = 1 + (z0 / 4000);
x1 = x0 * cosRZ - y0 * sinRZ;
y0 = y0 * cosRZ + x0 * sinRZ;
vertex.px = x1 * offset;
vertex.py = y0 * offset;
}
// shameless stole and edited from threejs CanvasRenderer
function expand( v1, v2 ) {
var x = v2.px - v1.px, y = v2.py - v1.py,
det = x * x + y * y, idet;
if ( det === 0 ) {
v1.tx = v1.px;
v1.ty = v1.py;
v2.tx = v2.px;
v2.ty = v2.py;
return;
}
idet = pixelExpandOffset / Math.sqrt( det );
x *= idet; y *= idet;
v2.tx = v2.px + x;
v2.ty = v2.py + y;
v1.tx = v1.px - x;
v1.ty = v1.py - y;
}
init();
Also see: Tab Triggers