Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Last active July 8, 2018 12:19
Show Gist options
  • Save sebastianrothbucher/11086c633a6b974639bff49da01c60e9 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/11086c633a6b974639bff49da01c60e9 to your computer and use it in GitHub Desktop.
sandstorm twistie
<div id="demosection" class="section closed" style="width: 600px; ">
<div class="section-title" onclick="toggle()"><div class="arrow fa fa-arrow-right"></div> Click to open!</div>
<div class="section-content"><canvas id="sandstorm"></canvas>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
const toggle = () => {
let sect=document.getElementById('demosection');
if (sect.classList.contains('closed')) {
open();
} else {
close();
}
};
const close = () => {
let sect=document.getElementById('demosection');
sect.classList.add('closed');
sect.classList.remove('open');
let arrow=sect.getElementsByClassName('section-title')[0].getElementsByClassName('arrow')[0];
arrow.classList.add('fa-arrow-right');
arrow.classList.remove('fa-arrow-down');
};
const open = () => {
let sect=document.getElementById('demosection');
sect.classList.remove('closed');
sect.classList.add('open');
let arrow=sect.getElementsByClassName('section-title')[0].getElementsByClassName('arrow')[0];
arrow.classList.remove('fa-arrow-right');
arrow.classList.add('fa-arrow-down');
let strm = document.getElementById('sandstorm');
strm.style.display='initial';
let ctx = strm.getContext('2d');
let maxRadius = Math.sqrt(Math.pow(strm.width, 2), Math.pow(strm.height, 2));
ctx.fillStyle='beige';
ctx.fillRect(0, 0, strm.width, strm.height);
ctx.fillStyle=d3.color('beige').darker(0.3).rgb();
// use animation frames - uncover all in some 0.x ms
let prevRadius=0;
let start=-1;
const len = 1200; // ms
const step = (pos) => { // (pos in ms)
if (start < 0) {
start = pos;
}
let curr = (pos - start) / len;
let currRadius = Math.round(maxRadius * curr);
for (let i=0; i<strm.width; i++) {
for (let j=0; j<strm.height; j++) {
if (Math.sqrt(Math.pow(i, 2) + Math.pow(j, 2)) > prevRadius && Math.sqrt(Math.pow(i, 2) + Math.pow(j, 2)) <= currRadius) {
ctx.clearRect(i, j, 1, 1);
if(Math.random() < 0.2) {
ctx.fillRect(i+5+Math.round(Math.log(Math.random()*500)), j+5+Math.round(Math.log(Math.random()*500)), 3, 3);
}
}
}
}
if (currRadius <= (maxRadius + 1)) {
prevRadius = currRadius;
window.requestAnimationFrame(step);
} else {
strm.style.display='none';
}
};
window.requestAnimationFrame(step);
};
// when in gallery, auto-demo
window.onload=()=>{
if (location.href.indexOf('fullcpgrid')>=0) {
setTimeout(open, 500);
}
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>
body {
font-family: sans-serif;
background: beige;
}
.section-title {
cursor: pointer;
}
.section.closed .section-content {
visibility: hidden;
}
.section.open .section-content {
visibility: visible;
}
.section-content {
position: relative;
}
#sandstorm {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment