Skip to content

Commit

Permalink
Clean up vtkjs example code
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Oct 24, 2018
1 parent 0ecd708 commit 7448f43
Showing 1 changed file with 18 additions and 49 deletions.
67 changes: 18 additions & 49 deletions examples/vtkjs/main.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,38 @@
var exampleDebug = {};

// Run after the DOM loads
$(function () {

This comment has been minimized.

Copy link
@manthey

manthey Oct 24, 2018

Contributor

We no longer need to wrap examples in a function, so this could be unwrapped.

This comment has been minimized.

Copy link
@zachmullen

zachmullen Oct 24, 2018

Author Contributor

Is there some other mechanism in place to ensure the following?

  • Example code runs after the DOM is loaded
  • Variables defined in examples don't pollute global scope

Sorry for the naive question, I haven't dug into the infrastructure of how examples are loaded.

This comment has been minimized.

Copy link
@manthey

manthey Oct 24, 2018

Contributor

Yes to the first, no to the second.

Example code is explicitly loaded after the example framework to ensure that it loads after the DOM is loaded.

Do we care if the examples pollute the global scope? As an learning tool, letting some of the example variables be in global scope might be desirable so someone can play with the results in the console. To this end, some examples had explicitly added data to the global scope (even if wrapped).

'use strict';

var capitals;
$.when(
/* Fetch capitals */
$.ajax({url: 'capitals.json'}).done(function (resp) {
capitals = resp;
})
).then(function () {

// Set map defaults to a reasonable center and zoom level
var mapParams = {
$.ajax({url: 'capitals.json'}).done(function (capitals) {
// Create a map object with reasonable center and zoom level
var map = geo.map({
node: '#map',
center: {x: 0, y: 0},
zoom: 2.5,
clampBoundsX: false,
clampBoundsY: false,
clampZoom: false,
discreteZoom: false
};
// Set the tile layer defaults
var layerParams = {
});
// Add the tile layer with the specified parameters
map.createLayer('osm', {
zIndex: 0,
minLevel: 4,
keepLower: true,
wrapX: false,
wrapY: false
};
// Create a map object
var map = geo.map(mapParams);
// Add the tile layer with the specified parameters
var osmLayer = map.createLayer('osm', layerParams);
// Set the vtk feature layer params
var vtkLayerParams = {
renderer: 'vtkjs'
};
});
// Create a vtk point feature layer
var vtkLayer = map.createLayer('feature', vtkLayerParams);
var pointFeature = vtkLayer
.createFeature('point', {
selectionAPI: true,
style: {
radius: 100, // sphere radius (~0.1km)
fillColor: 'red',
fillOpacity: function () {
return Math.random();
}
}
})
.data(capitals) // bind data
var vtkLayer = map.createLayer('feature', { renderer: 'vtkjs' });
vtkLayer.createFeature('point', {
selectionAPI: true,
style: {
radius: 100, // sphere radius (~0.1km)
fillColor: 'red',
fillOpacity: Math.random
}
})
.data(capitals) // bind data
.position(function (d) {
return {x: d.longitude, y: d.latitude}; // position accessor
return {x: d.longitude, y: d.latitude}; // position accessor
})
.draw();

// Make variables available as a global for easier debug
exampleDebug.map = map;
exampleDebug.mapParams = mapParams;
exampleDebug.layerParams = layerParams;
exampleDebug.osmLayer = osmLayer;
exampleDebug.vtkLayerParams = vtkLayerParams;
exampleDebug.vtkLayer = vtkLayer;
exampleDebug.pointFeature = pointFeature;
});
});

0 comments on commit 7448f43

Please sign in to comment.