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.
#loading
.circle
#container
body
background: #000;
overflow: hidden;
overscroll-behavior: none;
#loading
background: black;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
visibility: visible;
opacity: 1;
transition: visibility 1.6s, opacity 1.6s;
.circle
width: 50px;
height: 50px;
background: white;
border-radius: 50%;
opacity: 0;
transform: scale(0, 0);
animation: circle-animation 1.6s ease-in-out 0s infinite normal none;
#loading.loaded
visibility: hidden;
opacity: 0;
#container
width: 100%;
height: 100vh;
canvas
position: fixed;
top: 0;
left: 0;
z-index: 0;
outline: none;
/** css animation */
@keyframes circle-animation
0%
opacity: 0;
transform: scale(0, 0);
50%
opacity: 1;
transform: scale(1, 1);
/**
* This code reference to these site.
* YouTube : https://www.youtube.com/channel/UCDo7RTzizoOdPjY8A-xDR7g
* Article : https://iquilezles.org/www/articles/palettes/palettes.htm
* Site : https://al-ro.github.io/
* Thank you so much.
*/
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.121.1/build/three.module.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.121.1/examples/jsm/controls/OrbitControls.js';
//const simplex = new SimplexNoise();
/**
* Referenced | https://al-ro.github.io/
* Thank you so much :)
*/
/*
const computeCurl = (x, y, z) => {
const eps = 0.0001;
const curl = new THREE.Vector3();
//Find rate of change in YZ plane
let n1 = simplex.noise3D(x, y + eps, z);
let n2 = simplex.noise3D(x, y - eps, z);
//Average to find approximate derivative
let a = (n1 - n2)/(2 * eps);
n1 = simplex.noise3D(x, y, z + eps);
n2 = simplex.noise3D(x, y, z - eps);
//Average to find approximate derivative
let b = (n1 - n2)/(2 * eps);
curl.x = a - b;
//Find rate of change in XZ plane
n1 = simplex.noise3D(x, y, z + eps);
n2 = simplex.noise3D(x, y, z - eps);
a = (n1 - n2)/(2 * eps);
n1 = simplex.noise3D(x + eps, y, z);
n2 = simplex.noise3D(x + eps, y, z);
b = (n1 - n2)/(2 * eps);
curl.y = a - b;
//Find rate of change in XY plane
n1 = simplex.noise3D(x + eps, y, z);
n2 = simplex.noise3D(x - eps, y, z);
a = (n1 - n2)/(2 * eps);
n1 = simplex.noise3D(x, y + eps, z);
n2 = simplex.noise3D(x, y - eps, z);
b = (n1 - n2)/(2 * eps);
curl.z = a - b;
return curl;
}
*/
/** vertex shader source */
const vertexShaderSource = `
uniform float uTime;
varying vec2 vUv;
varying vec3 vPosition;
float PI = 3.14159265359;
void main(){
vUv = uv;
vPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
/** fragment shader source */
const fragmentShaderSource = `
uniform float uTime;
varying vec2 vUv;
varying vec3 vPosition;
/**
* Referenced | https://iquilezles.org/www/articles/palettes/palettes.htm
* Thank you so much :)
*/
vec3 pal( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d ) {
return a + b*cos( 6.28318*(c*t+d) );
}
/**
* Referenced | https://thebookofshaders.com/10/?lan=jp
* Thank you so much.
*/
float random (vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
void main () {
float dash = sin(vUv.x * 50.0 - uTime * 5.0);
if (dash < random(vPosition.xz)) discard;
vec3 col =
pal(
distance(vPosition, vec3(0, 0, 0)) - uTime * 0.5,
vec3(0.5,0.5,0.5),vec3(0.5,0.5,0.5),vec3(1.0,1.0,1.0),vec3(0.0,0.10,0.20)
);
gl_FragColor = vec4(col, 1.0);
}
`;
/**
* class Sketch
*/
class Sketch {
constructor() {
/** renderer */
this.renderer =
new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
document.getElementById('container').appendChild(this.renderer.domElement);
this.statsInit();
//this.setupGui();
this.init();
}
statsInit() {
this.stats = new Stats();
this.stats.setMode(0);
this.stats.domElement.style.position = 'absolute';
this.stats.domElement.style.left = '0';
this.stats.domElement.style.top = '0';
document.getElementById('container').appendChild(this.stats.domElement);
}
init() {
/** time */
this.time = new THREE.Clock(true);
/** mouse */
this.mouse = new THREE.Vector2();
/** canvas size */
this.width = window.innerWidth;
this.height = window.innerHeight;
/** scene */
this.scene = new THREE.Scene();
/** setup and render */
this.setupCanvas();
this.setupCamera();
this.setupLight();
this.setupShape();
this.setupEvents();
this.render();
}
setupCanvas() {
/** renderer */
this.renderer.setSize(this.width, this.height);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setClearColor(0x082852, 1.0);
/** style */
this.renderer.domElement.style.position = 'fixed';
this.renderer.domElement.style.top = '0';
this.renderer.domElement.style.left = '0';
this.renderer.domElement.style.zIndex = '0';
this.renderer.domElement.style.outline = 'none';
}
setupCamera() {
const fov = 70;
const fovRadian = (fov / 2) * (Math.PI / 180);
this.dist = this.height / 2 / Math.tan(fovRadian);
this.camera =
new THREE.PerspectiveCamera(
fov,
this.width / this.height,
0.001,
1000
);
this.camera.position.set(0, 0, 3);
this.camera.lookAt(new THREE.Vector3());
this.scene.add(this.camera);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
}
setupLight() {
/** directinal light */
this.directionalLight = new THREE.DirectionalLight(0xffffff);
this.scene.add(this.directionalLight);
/** point light*/
//this.pointLight = new THREE.PointLight(0xffffff, 1, this.dist);
//this.pointLight.position.set(0, this.dist, 0);
//this.scene.add(this.pointLight);
}
setupShape() {
this.shapes = new Array();
const s = new Shape(this, 0, 0, 0);
this.shapes.push(s);
}
setupGui() {
this.settings = {
scale: 3,
};
this.gui = new dat.GUI();
this.gui.add(this.settings, 'scale', 1, 10, 1).onChange(() => this.init());
}
render() {
this.stats.begin(); // -------------------- //
const time = this.time.getElapsedTime();
/** shapes */
for (let i = 0; i < this.shapes.length; i++) {
this.shapes[i].update(time);
}
this.renderer.render(this.scene, this.camera);
this.stats.end(); // -------------------- //
this.animationId = requestAnimationFrame(this.render.bind(this));
}
setupEvents() {
window.addEventListener('resize', this.resize.bind(this), false);
window.addEventListener('mousemove', this.mousemove.bind(this), false);
}
resize() {
const id = this.animationId;
cancelAnimationFrame(id);
this.init();
}
mousemove(event) {
this.mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
this.mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
}
class Shape {
constructor(sketch, x, y, z) {
this.sketch = sketch;
this.init(x, y, z);
}
init(x, y, z) {
this.material = new THREE.ShaderMaterial({
side: THREE.DoubleSide,
uniforms: {
uTime: {type: 'f', value: 0}
},
transparent: true,
blending: THREE.AdditiveBlending,
vertexShader: vertexShaderSource,
fragmentShader: fragmentShaderSource
});
for (let i = 0; i < 200; i++) {
this.points = this.getPoints(360 * Math.random());
const path = new THREE.CatmullRomCurve3(this.points);
this.geometry = new THREE.TubeBufferGeometry(path, 6, 0.005, 8, false);
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.sketch.scene.add(this.mesh);
}
}
getPoints(index) {
const points = new Array();
const rand = Math.random() / 10;
const startVec = new THREE.Vector3();
for (let i = 0; i < 100; i++) {
const x = Math.sin(i * Math.PI / 180) * Math.cos(index * Math.PI / 180) / 100;
const y = Math.sin(i * Math.PI / 180) * Math.sin(index * Math.PI / 180) / 100;
const z = rand;
const v = new THREE.Vector3(x, y, z);
startVec.add(v);
points.push(startVec.clone());
}
return points;
/*
const points = new Array();
const rand = Math.random() / 10;
let z = 0;
for (let i = 0; i < 100; i++) {
const x = Math.cos(index * Math.PI / 180) * (i / 100);
const y = Math.sin(index * Math.PI / 180) * (i / 100);
z += rand;
points.push(new THREE.Vector3(x, y, z));
}
return points;
*/
}
update(time) {
this.mesh.material.uniforms.uTime.value = time;
}
}
window.addEventListener('load', () => {
console.clear();
const loading = document.getElementById('loading');
loading.classList.add('loaded');
new Sketch();
});
Also see: Tab Triggers