Skip to content

Instantly share code, notes, and snippets.

@phi-line
Created May 9, 2018 23:29
Show Gist options
  • Save phi-line/35aab20549f6e3ecbd33dda305247065 to your computer and use it in GitHub Desktop.
Save phi-line/35aab20549f6e3ecbd33dda305247065 to your computer and use it in GitHub Desktop.

Revisions

  1. phi-line created this gist May 9, 2018.
    4 changes: 4 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <html>
    <body>
    <canvas></canvas>
    </body>
    7 changes: 7 additions & 0 deletions quadratic-mesh-circle.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Quadratic Mesh Circle
    ---------------------


    A [Pen](https://codepen.io/phi_line/pen/gzvpqL) by [phi-](https://codepen.io/phi_line) on [CodePen](https://codepen.io).

    [License](https://codepen.io/phi_line/pen/gzvpqL/license).
    66 changes: 66 additions & 0 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    const SPEED = 0.001;
    const SIZE = 1000;

    const SATURATION = 50;
    const LIGHTNESS = 60;
    const COLOR_START = 150;
    const COLOR_END = 300;

    const canvas = document.querySelector('canvas');
    const c = canvas.getContext('2d');

    function resizeCanvas() {
    let body = document.querySelector('body');
    let w = window.getComputedStyle(body).getPropertyValue('width');
    let h = window.getComputedStyle(body).getPropertyValue('height');
    canvas.width = parseInt(h, 10);
    canvas.height = parseInt(h, 10);
    }
    resizeCanvas();

    canvas.addEventListener("resize", resizeCanvas);

    canvas.addEventListener("click", e => {
    c.clearRect(0, 0, canvas.width, canvas.height);
    })

    function penDown(x, y, t) {
    c.beginPath();

    c.moveTo(x, y);

    c.quadraticCurveTo(x*Math.sin(Math.PI*t), y*Math.cos(Math.PI*t),canvas.width -x ,canvas.height - y);

    // c.quadraticCurveTo(x*Math.sin(t), y*Math.cos(t),canvas.width/2,canvas.height/2);
    c.stroke();

    }
    let date = new Date();
    function mainLoop() {
    let t = new Date().getTime() * SPEED;

    penDown(canvas.width + Math.sin(t) * SIZE, canvas.height + Math.cos(t) * SIZE, t);

    window.requestAnimationFrame(mainLoop);
    }

    mainLoop();

    let color = COLOR_START;
    let direction = true;
    function strobe(){
    let co = (direction)?color++:color--;
    c.strokeStyle = generateColor(co, SATURATION, LIGHTNESS, 100/co);
    c.shadowColor = generateColor(co, SATURATION, 70, .5);
    c.shadowBlur = 50;
    c.shadowOffsetX = 0;
    c.shadowOffsetY = 0;

    if (color <= COLOR_START || color >= COLOR_END) direction = !direction;
    }

    function generateColor(h=0, s=SATURATION, l=LIGHTNESS, a=1.0){
    return `hsla(${h}, ${s}%, ${l}%, ${a})`
    }

    setInterval(function() { strobe() }, 100/(COLOR_END+COLOR_START));
    13 changes: 13 additions & 0 deletions style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    body, html, canvas {
    background-color: #000;
    }

    body, html {
    height: 100%;
    display: grid;
    }

    canvas {
    margin: auto;
    border-radius: 50vh;
    }