Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #loading
  .circle
#container
  .box
    h1
              
            
!

CSS

              
                html, body
  height: 100%;
  width: 100%;

body
  font-size: 62.5%;
  background: #FFF;
  overflow: hidden;
  color: #FFF;
  font-family: serif;
  overscroll-behavior: none;

#loading
  background: black;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  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: 100%;
  .box
    color: white;
    font-size: 2.4rem;
    position: fixed;
    z-index: 1;
    top: 50%;
    left: 10%;
    transform: translateY(-50%);
    overflow: hidden;
    h1
      color: #000;
      letter-spacing: 0.4rem;
      padding-bottom: 0.8rem;
      &::after
        content: '';
        position: absolute;
        height: 4px;
        width: 100%;
        background: #000;
        bottom: 0.8rem;
        left: 0;
    p
      font-size: 0.8rem;
    .expansion
      font-size: 6.4rem;

/** css animation */
@keyframes circle-animation
  0%
    opacity: 0;
    transform: scale(0, 0);
  50%
    opacity: 1;
    transform: scale(1, 1);
    
@media (min-width: 500px)
  h1
    font-size: 3.2rem;
              
            
!

JS

              
                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';

// vertex shader source
const vertexSphereShader = `
uniform float uTime;

varying vec2 vUv;
varying vec3 vPosition;

float PI = 3.14159265359;
float scale = 0.1;

float scaling(float t, float delta, float a, float f) {
  return ((2.0 * a) / PI) * atan(sin(2.0 * PI * t * f) / delta);
}

void main(){
  vec3 pos = position;
  
  //pos.y += sin(pos.y * 0.01 - uTime * 10.0) * 20.0;
  //pos.z += sin(pos.y * 0.01 - uTime * 10.0) * 20.0;
  
  float dist = length(pos);
  float scale = scaling(uTime * 5.0 - dist * 0.01, 0.1, 1.0, 1.0 / 4.0) * 0.3 + 1.0;
  
  pos *= scale;
  
  vec4 mvPosition = modelViewMatrix * vec4(pos, 1.0);
  
  vUv = uv;
  vPosition = position;
  
  gl_Position = projectionMatrix * mvPosition;
}

`;

// fragment shader source
const fragmentSphereShader = `
uniform float uTime;

uniform sampler2D uTexture;

varying vec2 vUv;
varying vec3 vPosition;

// Referred to 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) );
}

void main () {
  vec4 color = texture2D(uTexture, vUv);
  
  float len = distance(vUv.xy, vec2(0.5));
  
  vec3 color2 =
    pal(
      len - uTime * 5.0,
      vec3(0.5,0.5,0.5),
      vec3(0.5,0.5,0.5),
      vec3(1.0,1.0,1.0),vec3(0.3,0.20,0.20)
    );
  
  color.r *= color2.r;
  color.g *= color2.g;
  color.b *= color2.b;
  
  gl_FragColor = vec4(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a);
}
`;

/**
 * Dat class
 */
class Dat {
  constructor(sketch) {
    this.sketch = sketch;
  
    this.initialize();
  }
  
  initialize() {
    this.gui = new dat.GUI();
    this.parameters = this.setParameters();
    this.controller = this.setController();
    this.gui.close();
  }
  
  setParameters() {
    let parameters;
  
    parameters = {
      size: 50
    };

    return parameters;
  }
  
  setController() {
    let controller;
  
    controller = {
      ratio: this.gui.add(this.parameters, 'size', 10, 1000, 1)
        .onChange(() => this.sketch.initialize())
    };

    return controller;
  }
}

/**
 * Mouse class
 */
class Mouse {
  constructor(sketch) {
    this.sketch = sketch;

    this.initialize();
  }
  
  initialize() {
    this.mouse = new THREE.Vector3();
    this.touchStart = new THREE.Vector3();
    this.touchMove = new THREE.Vector3();
    this.touchEnd = new THREE.Vector3();
    
    this.setupEvents();
  }
  
  setupEvents() {
    this.sketch.renderer.domElement.addEventListener('mousemove', this.onMousemove.bind(this), false);
    this.sketch.renderer.domElement.addEventListener('touchstart', this.onTouchstart.bind(this), false);
    this.sketch.renderer.domElement.addEventListener('touchmove', this.onTouchmove.bind(this), false);
    this.sketch.renderer.domElement.addEventListener('touchend', this.onTouchend.bind(this), false);
  }
  
  onMousemove(e) {
    this.mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
    this.mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
    this.mouse.z = 0;
  }
  
  onTouchstart(e) {
    const touch = e.targetTouches[0];
  
    this.touchStart.x = touch.pageX;
    this.touchStart.y = touch.pageY;
    this.touchStart.z = 0.0;

    this.mouse.x = (touch.pageX / window.innerWidth) * 2 - 1;
    this.mouse.y = -(touch.pageY / window.innerHeight) * 2 + 1;
    this.mouse.z = 0;
  }
  
  onTouchmove(e) {
    const touch = e.targetTouches[0];

    this.touchMove.x = touch.pageX;
    this.touchMove.y = touch.pageY;
    this.touchMove.z = 0.0;

    this.touchEnd.x = this.touchStart.x - this.touchMove.x;
    this.touchEnd.y = this.touchStart.y - this.touchMove.y;
    this.touchEnd.z = 0.0;

    this.mouse.x = (touch.pageX / window.innerWidth) * 2 - 1;
    this.mouse.y = -(touch.pageY / window.innerHeight) * 2 + 1;
    this.mouse.z = 0;
  }
  
  onTouchend(e) {
    this.touchStart.x = null;
    this.touchStart.y = null;
    this.touchStart.z = null;

    this.touchMove.x = null;
    this.touchMove.y = null;
    this.touchMove.z = null;

    this.touchEnd.x = null;
    this.touchEnd.y = null;
    this.touchEnd.z = null;
  }
}

/**
 * class Sketch
 */
class Sketch {
  constructor() {
    this.createCanvas();
    this.setupEvents();
    //this.setupStats();
    
    this.time = new THREE.Clock(true);
    this.mouse = new Mouse(this);
    //this.dat = new Dat(this);
    
    this.initialize();
  }
  
  createCanvas() {
    this.renderer =
      new THREE.WebGLRenderer({
        antialias: true,
        alpha: true
      });
    
    document.getElementById('container').
      appendChild(this.renderer.domElement);
  }
  
  setupStats() {
    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);
  }
  
  initialize() {
    if (this.animationId) {
      cancelAnimationFrame(this.animationId);
    }
    
    this.width = Math.ceil(window.innerWidth);
    this.height = Math.ceil(window.innerHeight);

    this.scene = new THREE.Scene();
    
    this.setupCanvas();
    this.setupCamera();
    this.setupLight();
    this.setupShape();
    
    this.draw();
  }
  
  setupCanvas() {
    this.renderer.setSize(this.width, this.height);
    //this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setPixelRatio(1.0);
    this.renderer.setClearColor(0xFFFFFF, 1.0);
    
    this.renderer.domElement.style.position = 'fixed';
    this.renderer.domElement.style.top = '0';
    this.renderer.domElement.style.left = '0';
    this.renderer.domElement.style.width = '100%';
    this.renderer.domElement.style.height = '100%';
    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.01,
        this.dist * 3
      );
    
    this.camera.position.set(0, 0, this.dist);
    this.camera.lookAt(new THREE.Vector3());
    
    this.scene.add(this.camera);
    
    //this.controls = new OrbitControls(this.camera, this.renderer.domElement);
  }
  
  updateCamera(time) {
    this.camera.position.set(
      Math.cos(-time * 0.3) * this.dist,
      Math.sin( time * 0.3) * this.dist,
      Math.sin(-time * 0.3) * this.dist
    );
    this.camera.lookAt(new THREE.Vector3());
  }
  
  setupLight() {
    // directinal light
    this.directionalLight = new THREE.DirectionalLight(0xffffff);
    this.scene.add(this.directionalLight);

    // point light
    this.spotLight = new THREE.SpotLight(0xffffff);
    this.spotLight.position.set(this.dist, this.dist, this.dist);
    this.scene.add(this.spotLight);
  }
  
  setupShape() {
    this.shapes = new Array();
    
    const s = new Shape(this, 0, 0, 0);
          
    this.shapes.push(s);
  }
  
  draw() {
    //this.stats.begin();
    
    const time = this.time.getElapsedTime();
    
    for (let i = 0; i < this.shapes.length; i++) {
      this.shapes[i].render(time * 0.1);
    }
    
    this.updateCamera(time);
    
    this.renderer.render(this.scene, this.camera);
    
    //this.stats.end();
    this.animationId = requestAnimationFrame(this.draw.bind(this));
  }
  
  setupEvents() {
    window.addEventListener('resize', this.onResize.bind(this), false);
  }
  
  onResize() {
    this.initialize();
  }
}

/**
 * shape class
 */
class Shape {
  /**
   * @constructor
   * @param {object} sketch - canvas
   */
  constructor(sketch, x, y, z) {
    this.sketch = sketch;
    this.position = new THREE.Vector3(x, y, z);
    this.initialize();
  }
  
  /**
   * initialize shape
   */
  initialize() {
    this.createTexture();
    this.updateTexture();
    
    // sphere
    this.sphereGeometry = new THREE.BoxGeometry(200, 200, 200, 24, 24, 24);
    this.sphereMaterial = new THREE.ShaderMaterial({
      side: THREE.DoubleSide,
      uniforms: {
        uTime: {type: 'f', value: 0},
        uTexture: {type: 't', value: this.returnTexture()}
      },
      vertexShader: vertexSphereShader,
      fragmentShader: fragmentSphereShader
    });
    this.sphereMesh = new THREE.Mesh(this.sphereGeometry, this.sphereMaterial);
    this.sphereMesh.position.set(this.position.x, this.position.y, this.position.z);
    
    this.sketch.scene.add(this.sphereMesh);
  }
  
  /**
   * return texture
   * @return {object} texture - texture made by canvas
   */
  createTexture() {
    this.canvas = document.createElement('canvas');
    this.ctx = this.canvas.getContext('2d');
    
    const length = 500;
    
    this.canvas.width = length;
    this.canvas.height = length;
    
    this.pdm = new PixcelDataMap(this.ctx, length, length);
    
    this.updateTexture();
  }
  
  returnTexture() {
    const texture = new THREE.CanvasTexture(this.canvas);
    
    texture.needsUpdate = true;
    
    return texture;
  }
  
  updateTexture() {
    this.pdm.render();
  }
  
  /**
   * render shape
   * @param {number} time - time 
   */
  render(time) {
    this.updateTexture();
    
    this.sphereMesh.material.uniforms.uTexture.value = this.returnTexture();
    
    this.sphereMesh.material.uniforms.uTime.value = time;
  }
}

(() => {
  window.addEventListener('load', () => {
    console.clear();

    const loading = document.getElementById('loading');
    loading.classList.add('loaded');

    new Sketch();
  });
})();

/**
 * create multiple array
 */
const createMultipleArray = (one, two) => {
  const arr = new Array(one);
  
  for (let y = 0; y < one; y++) {
    arr[y] = new Array(two);
    for (let x = 0; x < two; x++) {
      arr[y][x] = 0;
    }
  }
  return arr;  
};

/**
 * get random number
 */
const getRandomNumber = (min, max) => {
  
  return Math.floor(Math.random() * (max + 1 - min)) + min;
};

/**
 * class PixcelDataMap
 * Referred to Cellular Automaton
 * Generative Art: A Practical Guide Using Processing / Matt Pearson
 * Thank you so much.
 */
class PixcelDataMap {
  constructor(ctx, width, height) {
    this.ctx = ctx;
    this.width = width;
    this.height = height;
    this.mod = 3;
    
    this.state = createMultipleArray(this.height, this.width);
    
    this.d = this.ctx.createImageData(this.width, this.height);
    
    this.setupData();
  }
  
  setupData() {
    for (let y = 0; y < this.height; y++) {
      for (let x = 0; x < this.width; x++) {
        if (y === this.height / 2 && x === this.width / 2) {
          this.state[y][x] = 1;
        }
      }
    }
  }
  
  drawData() {
    this.ctx.putImageData(this.d, 0, 0);
  }
  
  updateData() {
    for (let y = 0; y < this.height; y++) {
      for (let x = 0; x < this.width; x++) {
        const i = y * this.width + x;
        
        this.d.data[4 * i + 0] = 0xff;
        this.d.data[4 * i + 1] = 0xff;
        this.d.data[4 * i + 2] = 0xff;
        if (this.state[y][x] === 1) {
          this.d.data[4 * i + 3] = 0;
        } else {
          this.d.data[4 * i + 3] = 255;
        }
      }
    }
  }
  
  // Referred to 数学から創るジェネラティブアート ―Processingで学ぶかたちのデザイン / 巴山 竜来
  // ありがとうございます。
  updateState() {
    const array = createMultipleArray(this.height, this.width);

    for (let y = 0; y < this.height; y++) {
      for (let x = 0; x < this.width; x++) {
        let total = 0;
        let above = y - 1;
        let below = y + 1;
        let left = x - 1;
        let right = x + 1;

        if (above < 0) above = this.height - 1;
        if (below == this.height) below = 0;
        if (left < 0) left = this.width - 1;
        if (right == this.width) right = 0;

        const t = this.state[above][x]; // top
        const r = this.state[y][right]; // right
        const b = this.state[below][x]; // bottom
        const l = this.state[y][left]; // left
        const tl = this.state[above][left]; // top left
        const bl = this.state[below][left]; // bottom left
        const br = this.state[below][right]; // bottom right
        const tr = this.state[above][right]; // top right
        
        total = t + r + b + l + tl + bl + br + tr;
        array[y][x] = total % this.mod;
      }
    }
    
    this.state = array;
  }
  
  render() {
    this.drawData();
    this.updateData();
    this.updateState();
  }
}
              
            
!
999px

Console