Skip to content

Instantly share code, notes, and snippets.

@mathdoodle
Last active August 29, 2015 14:22
Show Gist options
  • Save mathdoodle/ae21fb11de26e306afe1 to your computer and use it in GitHub Desktop.
Save mathdoodle/ae21fb11de26e306afe1 to your computer and use it in GitHub Desktop.
Projective Line
{
"uuid": "6ef68984-26c6-4eb9-aa94-326c901bf8be",
"description": "Projective Line",
"dependencies": {
"DomReady": "latest",
"davinci-mathbox": "1.0.14"
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MathBox - Empty Example.</title>
<!-- STYLE-MARKER -->
<!-- SCRIPTS-MARKER -->
</head>
<body>
<div id="info"></div>
<script type='text/javascript'>
try {
<!-- CODE-MARKER -->
}
catch(e) {
console.log(e);
}
</script>
</body>
</html>
DomReady.ready(function() {
ThreeBox.preload(['../shaders/MathBox.glsl.html'], init);
});
function init() {
// MathBox boilerplate
var mathbox = window['mathbox'] = mathBox({
cameraControls: true,
cursor: true,
controlClass: ThreeBox.OrbitControls,
elementResize: true,
fullscreen: true,
screenshot: true,
stats: false,
scale: 1,
});
var stage = mathbox.start();
// Viewport camera/setup
stage
// Polar viewport
.viewport({
type: 'polar',
range: [[-π, π], [-1, 1], [-4, 4]],
scale: [.8, .8, .8],
polar: 1,
rotation: [τ/4, 0, 0],
})
.camera({
orbit: 3.5,
phi: τ/4+.5,
theta: 0.1,
})
.transition(300)
// Animated curve
.curve({
domain: [-1, 1],
n: 256,
expression: rationalFunc,
lineWidth: 1,
})
// Manual conical grid lines
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [0, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [.25*π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [.5*π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [.75*π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [1.25*π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [1.5*π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 2,
domain: [-1, 1],
data: [[0, 0, -4], [1.75*π, .5, 4]],
color: 0xa0a0a0,
lineWidth: 1,
})
.curve({
n: 48,
domain: [-π, π],
expression: function (x) { return [x, .5, 4]; },
color: 0xa0a0a0,
lineWidth: 1,
});
// Rotate camera continuously
stage.world().loop().hookPreRender(function () {
stage.set('camera', { phi: +new Date() * .0003 });
});
}
// Clock that starts as soon as it is first called (per id).
var clocks: {[key:number]:number} = {};
function clock(id: number): number {
if (!clocks[id]) clocks[id] = Date.now();
return (Date.now() - clocks[id]) * .001;
}
// Map the real numbers onto a finite range
function map(x: number): number {
if (Math.abs(x) < 1) return x;
if (x > 1) return 2 - 1/x;
return -2 - 1/x;
}
// Rational function with moving poles, squished into a cone
function rationalFunc(x: number): number[] {
var poles: number[] = [];
var t = clock(1);
poles.push(Math.sin(t*.36+3) + 3);
poles.push(Math.sin(t*.71+5) + 2);
poles.push(Math.sin(t*1.13-8) - 1);
poles.push(Math.sin(t*0.87+1) - 3);
var y = 5, i = x*4;
// The underscore library is coming from the MathBox bundle, version 1.3.3
// That's a problem, because 1.8.3 dous not have loop.
window['_'].each(poles, function (z) { y /= (i - z) });
return [π/2*map(y) + t, x*.25 + .25, i];
}
html, body { height: 100%; }
body { margin: 0; padding: 0 }
canvas { display: block }
#info {
position: absolute;
left: 50%;
bottom: 50px;
z-index: 20;
width: 300px;
margin-left: -150px;
padding: 25px;
background: rgba(0, 0, 0, .5);
color: #fff;
font-family: "Lucida Grande", sans-serif;
font-size: 16px;
text-align: center;
border-radius: 3px;
text-shadow: 0px 1px 0px rgba(0, 0, 0, .4);
opacity: 0;
}
#info.transition {
-webkit-transition: opacity 300ms ease-in-out;
-moz-transition: opacity 300ms ease-in-out;
transition: opacity 300ms ease-in-out;
}
#info kbd {
background: #aaa;
box-shadow: 0px 1px 1px rgba(0, 0, 0, .3);
border-radius: 3px;
padding: 3px;
margin: 3px;
font-family: inherit;
}
.mathbox-label {
font-family: 'klavika-web', sans-serif;
font-weight: normal;
font-style: normal;
text-shadow:
3px 0px 1px rgb(255, 255, 255),
-3px 0px 1px rgb(255, 255, 255),
0px -3px 1px rgb(255, 255, 255),
0px 3px 1px rgb(255, 255, 255),
2px 2px 1px rgb(255, 255, 255),
-2px 2px 1px rgb(255, 255, 255),
2px -2px 1px rgb(255, 255, 255),
-2px -2px 1px rgb(255, 255, 255),
3px 2px 1px rgb(255, 255, 255),
-3px 2px 1px rgb(255, 255, 255),
3px -2px 1px rgb(255, 255, 255),
-3px -2px 1px rgb(255, 255, 255),
1px 3px 1px rgb(255, 255, 255),
-1px 3px 1px rgb(255, 255, 255),
1px -3px 1px rgb(255, 255, 255),
-1px -3px 1px rgb(255, 255, 255),
-1px -1px 1px rgb(255, 255, 255),
-1px 1px 1px rgb(255, 255, 255),
1px -1px 1px rgb(255, 255, 255),
1px 1px 1px rgb(255, 255, 255);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment