Skip to content

Commit

Permalink
adds word sphere for knowledge graph
Browse files Browse the repository at this point in the history
  • Loading branch information
anishd19 committed Jan 1, 2019
1 parent ae2d772 commit 36bc3b7
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 5 deletions.
28 changes: 28 additions & 0 deletions css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion css/app.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ $text-color: floralwhite;

body {
background-image: radial-gradient($primary-color 0%, $secondary-color 95%);
background-attachment: fixed;
height: 100vh;
font-family: 'Courgette', cursive;
color: $text-color;
}

.atf {
Expand All @@ -26,7 +29,7 @@ body {
font-family: 'Courgette', cursive;
letter-spacing: 0.2rem;
margin:0;
color: $text-color;
color: $text-color;background-attachment: fixed;
text-align: center;
}
h1 {
Expand Down Expand Up @@ -125,4 +128,27 @@ body {
100% {
opacity: 1;
}
}
}

.btf {
overflow: hidden;
.word-sphere {
width: 50vw;
height: 100vh;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
canvas {
margin: 0 auto;
position: relative;
right: 50px;
}
@media (max-width: 480px) {
width: 100vw;
position: relative;
right: 5px;
}
}
}
14 changes: 12 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<title>Anish Dhamodaran</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='css/app.css' />
<script type='text/javascript' src='js/jquery.min.js'></script>
<script type='text/javascript' src='js/app.js'></script>
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP"
crossorigin="anonymous">
Expand Down Expand Up @@ -56,7 +54,19 @@ <h3 id="intro5"><i class="fas fa-globe"></i>anishd19</h3>
</ul>
</div>
</section>
<section class="btf">
<div class="word-sphere">
<div><h3>Few technologies I do as a profession</h3></div>
<div class="canvas-container">
<canvas id="canvas"></canvas>
</div>
</div>
</section>
</div>

<script type='text/javascript' src='js/jquery.min.js'></script>
<script type='text/javascript' src='js/app.js'></script>

</body>

</html>
144 changes: 144 additions & 0 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
(function() {
const canvas = document.getElementById('canvas');

const texts = [
'HTML5', 'Javascript', 'C', 'C++', 'AVR C', 'Docker',
'CSS3', 'Python', 'Java', 'MongoDB', 'Bootstrap',
'ReactJS', 'jQuery', 'ExpressJS', 'Webpack', 'Gulp',
'NodeJS', 'Redux', 'Mocha', 'Chai', 'Photoshop', 'Flash'
];
const counts = [1,2,4,5,4,2,1];

const options = {
tilt: Math.PI / 9,
initialVelocityX: 0.09,
initialVelocityY: 0.09,
initialRotationX: Math.PI * 0.14,
initialRotationZ: 0,
};

wordSphere(canvas, texts, counts, options);

/**
* WordSphere
* Written by Hyojun Kim in 2017. Licensed in MIT.
*/
function wordSphere(canvas, texts, counts, options) {
const π = Math.PI; // happy math!
const {
width = 500,
height = 500,
radius = 150,
padding = 50,
fontSize = 22,
tilt = 0,
initialVelocityX = 0,
initialVelocityY = 0,
initialRotationX = 0,
initialRotationZ = 0,
} = options;

let vx = initialVelocityX, vy = initialVelocityY;
let rx = initialRotationX, rz = initialRotationZ;

// canvas setup
let ctx = canvas.getContext('2d');
ctx.textAlign = 'center';

// Hi-DPI support
canvas.width = width * 2;
canvas.height = height * 2;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
ctx.scale(2,2);

// scrolling
let clicked = false, lastX, lastY;
canvas.addEventListener('mousedown', event => {
clicked = true;
lastX = event.screenX;
lastY = event.screenY;
});
canvas.addEventListener('mousemove', event => {
if (!clicked) return;
[dx, dy] = [event.screenX - lastX, event.screenY - lastY];
[lastX, lastY] = [event.screenX, event.screenY];

// rotation update
rz += -dy * 0.01;
rx += dx * 0.01;

// velocity update
vx = dx * 0.1;
vy = dy * 0.1;

if (!looping) startLoop();
});
canvas.addEventListener('mouseup', e => clicked = false);
canvas.addEventListener('mouseleave', e => clicked = false);

function rot(x,y,t) {
return [x*Math.cos(t)-y*Math.sin(t), x*Math.sin(t)+y*Math.cos(t)];
}

function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);

let ix = 0, iz = 0, i = 1;
for (const text of texts) {
const degZ = (π/(counts.length-1)) * iz;
const degX = (2*π/counts[iz]) * ix;

let x = radius * Math.sin(degZ) * Math.cos(degX);
let y = radius * Math.sin(degZ) * Math.sin(degX);
let z = radius * Math.cos(degZ) + 8*(ix % 2) /* randomness */;

// camera transform
[y,z] = rot(y, z, tilt);
[x,z] = rot(x, z, rz);
[x,y] = rot(x, y, rx);

// convert to cartesian and then draw.
const alpha = 0.6 + 0.4 * (x/radius);
const size = fontSize + 2 + 5*(x/radius);
ctx.fillStyle = `rgba(250,250,240,${alpha})`;
ctx.font = `${size}px "Courgette", cursive, sans-serif`;
ctx.fillText(text, y + width/2, -z + height/2);

ix--;
if (ix < 0) {
iz++;
ix = counts[iz] - 1;
}
i++;
}
}

// renderer
let looping = false;
function rendererLoop() {
if (looping) window.requestAnimationFrame(rendererLoop);
render();

// deacceleration - dirty code xD
if (vx > 0) vx = vx - 0.01;
if (vy > 0) vy = vy - 0.01;
if (vx < 0) vx = vx + 0.01;
if (vy > 0) vy = vy + 0.01;
if (vx == 0 && vy == 0) stopLoop();

rz += vy * 0.01;
rx += vx * 0.01;
}

function startLoop() {
looping = true;
window.requestAnimationFrame(rendererLoop);
}

function stopLoop() {
looping = false;
}
startLoop();
}
})();

0 comments on commit 36bc3b7

Please sign in to comment.