Add an example that renders a simple fractal

This works at resolutions where simplification is significantly reducing the number of points in the line.  At high resolutions, navigation becomes unresponsive.  Limiting draw instructions to a replay group's extent will fix this.
This commit is contained in:
Tim Schaub
2014-02-21 17:48:17 -07:00
parent 667f4278b3
commit ee2485534a
2 changed files with 189 additions and 0 deletions

64
examples/fractal.html Normal file
View File

@@ -0,0 +1,64 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>Fractal Example</title>
<style>
.map {
background: whitesmoke;
}
#depth {
width: 100px;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">Fractal Example</h4>
<p id="shortdesc">Example of a fractal.</p>
<label for="depth">
depth:&nbsp;
<input id="depth" type="range" min="0" max="9" step="1" value="5">
&nbsp;(<span id="count">#</span> points)
</label>
<div id="docs">
<p>See the <a href="fractal.js" target="_blank">fractal.js source</a> to see how this is done.</p>
</div>
<div id="tags">fractal, vector</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
<script src="loader.js?id=fractal" type="text/javascript"></script>
</body>
</html>

125
examples/fractal.js Normal file
View File

@@ -0,0 +1,125 @@
goog.require('ol.Feature');
goog.require('ol.Map');
goog.require('ol.View2D');
goog.require('ol.geom.LineString');
goog.require('ol.layer.Vector');
goog.require('ol.source.Vector');
var radius = 10e6;
var cos30 = Math.cos(Math.PI / 6);
var sin30 = Math.sin(Math.PI / 6);
var rise = radius * sin30;
var run = radius * cos30;
var triangle = new ol.geom.LineString([
[0, radius], [run, -rise], [-run, -rise], [0, radius]
]);
var feature = new ol.Feature(triangle);
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [feature]
})
});
var map = new ol.Map({
layers: [layer],
renderer: 'canvas',
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 1
})
});
function makeFractal(depth) {
var geometry = /** @type {ol.geom.LineString} */ (triangle.clone());
var graph = coordsToGraph(geometry.getCoordinates());
for (var i = 0; i < depth; ++i) {
var node = graph;
while (node.next) {
var next = node.next;
injectNodes(node);
node = next;
}
}
var coordinates = graphToCoords(graph);
document.getElementById('count').innerHTML = coordinates.length;
geometry.setCoordinates(coordinates);
feature.setGeometry(geometry);
}
function injectNodes(startNode) {
var endNode = startNode.next;
var start = startNode.point;
var end = startNode.next.point;
var dx = end[0] - start[0];
var dy = end[1] - start[1];
// first point at 1/3 along the segment
var firstNode = {
point: [start[0] + dx / 3, start[1] + dy / 3]
};
// second point at peak of _/\_
var r = Math.sqrt(dx * dx + dy * dy) / (2 * cos30);
var a = Math.atan2(dy, dx) + Math.PI / 6;
var secondNode = {
point: [start[0] + r * Math.cos(a), start[1] + r * Math.sin(a)]
};
// third point at 2/3 along the segment
var thirdNode = {
point: [end[0] - dx / 3, end[1] - dy / 3]
};
startNode.next = firstNode;
firstNode.next = secondNode;
secondNode.next = thirdNode;
thirdNode.next = endNode;
}
function coordsToGraph(coordinates) {
var graph = {
point: coordinates[0]
};
var length = coordinates.length;
for (var level = 0, node = graph; level < length - 1; ++level) {
node.next = {
point: coordinates[level + 1]
};
node = node.next;
}
return graph;
}
function graphToCoords(graph) {
var coordinates = [graph.point];
for (var node = graph, i = 1; node.next; node = node.next, ++i) {
coordinates[i] = node.next.point;
}
return coordinates;
}
var depthInput = document.getElementById('depth');
function update() {
makeFractal(Number(depthInput.value));
}
var updateTimer;
/**
* Regenerate fractal on depth change. Change events are debounced so updates
* only occur every 200ms.
*/
depthInput.onchange = function() {
window.clearTimeout(updateTimer);
updateTimer = window.setTimeout(update, 200);
};
update();