Created
May 9, 2018 23:29
-
-
Save phi-line/35aab20549f6e3ecbd33dda305247065 to your computer and use it in GitHub Desktop.
Quadratic Mesh Circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <body> | |
| <canvas></canvas> | |
| </body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body, html, canvas { | |
| background-color: #000; | |
| } | |
| body, html { | |
| height: 100%; | |
| display: grid; | |
| } | |
| canvas { | |
| margin: auto; | |
| border-radius: 50vh; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment