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

              
                canvas(id="c")
.stamp
  .circle
    p#circle-content EXPERIMENTS . EXPERIMENTS .
  p 2018

              
            
!

CSS

              
                body
  font-family 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif
  background-color #e0dad5
  margin 0
  padding 0
  min-height 100%
  min-width 100%
  width 100%
  height 100%
  overflow hidden

canvas
  width 100%
  height 100%

.stamp
  position absolute
  right 6vmax
  top 6vmax
  //z-index 1
  p
    color #ee3d3d
    font-size 2vmax
    font-family 'Asul', sans-serif
    margin 0
  .circle
    position absolute
    width 1px
    height 1px
    display block
    left 50%
    top 50%
    background-color transparent
    animation rotating 9.5s linear infinite
    span
      font-family: 'Rubik', sans-serif;
      font-weight bold
      font-size 0.8vmax
      color #ee3d3d
      height 5vmax
      position absolute
      display inline-block
      left 0
      bottom 0
      transform-origin: bottom center

for i in (1..28)
  .char{i}
    transform rotate((i * 13) deg)

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

              
            
!

JS

              
                let parameters = {
    size: 30,
    radius: 1,
    proximity: 125,
    growth: 60,
    ease: 0.075,
    stats: false,
};

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

class Circle {
    constructor(radius, x, y) {
        this._radius = radius;
        this.radius = radius;
        this.growthValue = 0;
        this.position = new Point(x, y);
    }

    /**
     * @param {CanvasRenderingContext2D} context
     * @param {number} ease
     */
    draw(context, ease) {
        this.radius += ((this._radius + this.growthValue) - (this.radius)) * ease;
        context.moveTo(this.position.x, this.position.y);
        context.arc(this.position.x, this.position.y, this.radius, 0, 2 * Math.PI);
    }

    addRadius(value) {
        this.growthValue = value;
    }

    get x() {
        return this.position.x;
    }

    set x(value) {
        this.position.x = value;
    }

    get y() {
        return this.position.y;
    }

    set y(value) {
        this.position.y = value;
    }
}

class Stamp {
    constructor(id) {
        const el = document.getElementById(id);
        const parent = el.parentElement;
        parent.removeChild(el);
        const chars = el.innerText.split("");
        chars.push(" ");
        for (let i = 0; i < chars.length; i++) {
            const span = document.createElement("span");
            span.innerText = chars[i];
            span.className = `char${i + 1}`;
            parent.appendChild(span);
        }
    }
}

function init(){
   new Stamp("circle-content");
   const stats = new Stats();
   stats.showPanel(0);
  
  buildGUI();
    let imageLoaded = false;
    const canvas = document.getElementById("c");
    const image = new Image();
    let circles = [];
    const context = canvas.getContext("2d");
    window.addEventListener("resize", resizeHandler);
    window.addEventListener("mousemove", mouseMoveHandler);
    window.addEventListener("touchmove", touchMoveHandler);
    resizeHandler();
    loadImage();
    build();

    function build() {
        circles = [];
        const {size, radius} = parameters;
        const columns = Math.ceil(window.innerWidth / size) + 1;
        const rows = Math.ceil(window.innerHeight / size) + 1;
        const amount = Math.ceil(columns * rows);
        for (let i = 0; i < amount; i++) {
            const column = i % columns;
            const row = ~~(i / columns);
            circles.push(new Circle(radius, size * column, size * row));
        }
    }

    function mouseMoveHandler(event) {
        proximityHandler(event);
    }
  
    function touchMoveHandler(event) {
        proximityHandler(event.touches[0])
    }

    function proximityHandler(event) {
        const {proximity, growth} = parameters;
        for (let c of circles) {
            let distance = Math.sqrt(Math.pow(c.x - event.clientX, 2) + Math.pow(c.y - event.clientY, 2));
            let d = map(distance, c._radius, c._radius + proximity, growth, 0);
            if (d < 0) d = 0;
            c.addRadius(d);
        }
    }

    function animate() {
        stats.begin();
        context.clearRect(0, 0, window.innerWidth, window.innerHeight);
        context.save();
        context.beginPath();
        context.fillStyle = "#000000";
        context.clearRect(0, 0, window.innerWidth, window.innerHeight);
        for (let circle of circles) {
            circle.draw(context, parameters.ease);
        }
        if (imageLoaded) {
            drawImage();
        } else {
            context.fill();
        }
        context.restore();
        stats.end();
        requestAnimationFrame(animate);
    }

    function buildGUI() {
        const gui = new dat.GUI({closed: true});
        gui.closed = true;
        const sizeController = gui.add(parameters, "size", 5, 80);
        const radiusController = gui.add(parameters, "radius", 0.5, 20);
        gui.add(parameters, "proximity", 0, 300);
        gui.add(parameters, "growth", 1, 150);
        gui.add(parameters, "ease", 0.02, 0.15);
        const statsController = gui.add(parameters, "stats");

        sizeController.onFinishChange(() => {
            build();
        });

        radiusController.onFinishChange(() => {
            build();
        });

        statsController.onChange((value) => {
            if (value) {
                document.body.appendChild(stats.dom);
            } else {
                stats.dom.parentNode.removeChild(stats.dom);
            }
        })
    }

    function drawImage() {
        context.clip();
        const {naturalHeight, naturalWidth} = image;
        const ratio = findPreferredRatio(naturalWidth, naturalHeight, window.innerWidth, window.innerHeight);
        const w = naturalWidth * ratio;
        const h = naturalHeight * ratio;
        const x = window.innerWidth / 2 - w / 2;
        const y = window.innerHeight / 2 - h / 2;
        context.drawImage(image, 0, 0, naturalWidth, naturalHeight, x, y, w, h);
    }

    function resizeHandler() {
        resizeCanvas(canvas);
        build();
    }

    function loadImage() {
        image.onload = function () {
            imageLoaded = true;
        };
        image.src = "https://assets.codepen.io/128966/img_repulsion.jpg";
    }

    animate();
}

init();

function normalize(value, min, max) {
    return (value - min) / (max - min);
}

function interpolate(value, min, max) {
    return min + (max - min) * value;
}

function map(value, min1, max1, min2, max2) {
    return interpolate(normalize(value, min1, max1), min2, max2);
}

function findPreferredRatio(width, height, maxWidth, maxHeight) {
    let dw = maxWidth / width;
    let dh = maxHeight / height;
    return dw > dh ? dw : dh;
}

function resizeCanvas (canvas) {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
}
              
            
!
999px

Console