From 744b80da64bb6562db8a41d20417d2f4b036d079 Mon Sep 17 00:00:00 2001 From: Brandon Tilley Date: Sat, 21 Dec 2013 20:35:04 -0800 Subject: [PATCH] Update site --- documentation/core.html | 1 + documentation/index.html | 30 ++++++++++++ documentation/plugins.html | 93 +++++++++++++++++++++++++++++++++++++- js/lib/planetaryjs.min.js | 4 +- 4 files changed, 125 insertions(+), 3 deletions(-) diff --git a/documentation/core.html b/documentation/core.html index e5fdce7..c7c14b3 100644 --- a/documentation/core.html +++ b/documentation/core.html @@ -90,6 +90,7 @@ <body> ...

+

If you use the default topojson plugin (most people will), you'll also need to make sure world-110m.json (or some other TopoJSON data file) is available on your server. This file is also available from the download page. See the TopoJSON Plugin documentation for more information.

Core API

planetaryjs.noConflict()

In non-AMD and non-CommonJS environments, Planetary.js takes over the global planetaryjs namespace (in the browser, this means window.planetaryjs). If, for some reason, something else useful was there before you loaded Planetary.js, you can ask for it to be returned to that spot by calling planetaryjs.noConflict(). The Planetary.js library will be returned from the function, so you can continue to use the library.

diff --git a/documentation/index.html b/documentation/index.html index f9f40e9..faa14a4 100644 --- a/documentation/index.html +++ b/documentation/index.html @@ -84,6 +84,36 @@
  • Plugins describes the plugin architecture of Planetary.js and shows how you can easily build your own plugins to modify the behavior of Planetary.js
  • Built-In Plugins describes each of the built-in plugins in turn, including their public API and how to use them in a project.
  • +

    Quick Start

    +

    If you want to get up-and-running quickly, or like to experiment and figure things out, you can use this HTML and JavaScript to get a quick, simple globe working quickly.

    +

    Note that you'll need to run this page from a web server of some kind so that Planetary.js can load the TopoJSON data via Ajax (Ajax requests don't work when viewing a page directly from the filesystem).

    +
    +
    HTML
    + +
    <html>
    +<head>
    +  <script type='text/javascript' src='http://d3js.org/d3.v3.min.js'></script>
    +  <script type='text/javascript' src='http://d3js.org/topojson.v1.min.js'></script>
    +  <script type='text/javascript' src='planetaryjs.min.js'></script>
    +</head>
    +<body>
    +  <canvas id='globe' width='500' height='500'></canvas>
    +  <script type='text/javascript' src='yourApp.js'></script>
    +</body>
    +</html>
    +
    JavaScript
    + +
    var planet = planetaryjs.planet();
    +// You can remove this statement if `world-110m.json`
    +// is in the same path as the HTML page:
    +planet.loadPlugin(planetaryjs.plugins.earth({
    +  topojson: { file: 'http/path/to/world-110m.json' }
    +}));
    +// Make the planet fit well in its canvas
    +planet.projection.scale(250).translate([250, 250]);
    +var canvas = document.getElementById('globe');
    +planet.draw(canvas);
    +

    diff --git a/documentation/plugins.html b/documentation/plugins.html index 77ba231..d7cf089 100644 --- a/documentation/plugins.html +++ b/documentation/plugins.html @@ -80,7 +80,7 @@

    Loading Plugins

    Plugins are loaded either globally by planetaryjs.loadPlugin or for a specific planet instance by planet.loadPlugin. If you call draw on a planet and it has no plugins loaded at all (from either source), Planetary.js will use the default plugin stack, which consists of the earth and pings plugins.

    Anatomy of a Plugin

    -

    A plugin is simply a JavaScript function that takes a planet instance as a parameter and performs some predefined operation. The best plugins do one tiny thing. If you want a plugin to do a lot of things at once, you should build a plugin that wraps other, smaller plugins; in fact, this is exactly how the earth plugin is built. See the Earth documentation for more details.

    +

    A plugin is simply a JavaScript function that takes a planet instance as a parameter and performs some predefined operation. The best plugins do one tiny thing. If you want a plugin to do a lot of things at once, you should build a plugin that wraps other, smaller plugins; in fact, this is exactly how the earth plugin is built. See the Earth Plugin documentation for more details.

    Most of the time, a plugin will implement its behavior by registering callbacks into the planet's lifecycle hooks. For example, the following simple plugin increments the planet's projection's rotation by one degree every tick (this would make for a very fast spinning globe, but demonstrates the idea nicely enough):

    JavaScript
    @@ -113,6 +113,97 @@ planet.loadPlugin(autorotate); }; planet.loadPlugin(autorotate(5)); +

    +

    Setting Yourself Up

    +

    If you need to do some work before your plugin is ready to be used, you can add a hook to a planet's onInit hook to do the necessary setup.

    +
    +
    JavaScript
    + +
    var somePlugin = function(planet) {
    +  planet.onInit(function() {
    +    doSomeSetupWork();
    +  });
    +};
    +

    +

    If you need to do some asynchronous setup--such as fetching data with an Ajax request--before your plugin is ready, you can accept an argument to your onInit function. When you're done setting up, call this function and Planetary.js will continue to initialize the planet. If you accept the parameter but don't call it, the initialization process will stop (and your planet will not work).

    +
    +
    JavaScript
    + +
    var somePlugin = function(planet) {
    +  planet.onInit(function(done) {
    +    doSomeAsynchronousSetupWork(function() {
    +      done();
    +    });
    +  });
    +};
    +

    +

    Drawing on the Canvas

    +

    Many plugins will want to draw onto the globe's canvas; you can do so by registering a function to a planet's onDraw hook.

    +
    +
    JavaScript
    + +
    var somePlugin = function(planet) {
    +  planet.onDraw(function() {
    +    planet.withSavedContext(function(context) {
    +      context.beginPath();
    +      planet.path.context(context)({type: 'Sphere'});
    +      context.fillStyle = 'black';
    +      context.fill();
    +    });
    +  });
    +};
    +

    +

    The planet exposes properties and methods, such as context, path, and withSavedContext to assist with drawing to the canvas. The Planet API documentation goes into more detail on individual properties.

    +

    Drawing Geo Paths

    +

    As explained in the planet.path documentation on the Planet API page, planet.path is a d3.geo.path object that can be used to draw geographical geometry onto the canvas. The path will take care of transforming the coordinates to be projected onto the orthographic view of the globe.

    +

    As a demonstration of this technique, the following is a plugin that will take the land data from a TopoJSON data source (stored on planet.plugins.topojson.world), convert it to a GeoJSON feature, and draw it on the planet. This code is similar to (but slightly simplified from) how the Land plugin works.

    +
    +
    JavaScript
    + +
    var drawLand = function(planet) {
    +  planet.onDraw(function() {
    +    planet.withSavedContext(function(context) {
    +      var world = planet.plugins.topojson.world;
    +      var land = topojson.feature(world, world.objects.land);
    +
    +      context.beginPath();
    +      planet.path.context(context)(land);
    +      context.fillStyle = 'white';
    +      context.fill();
    +    });
    +  });
    +};
    +

    +

    Exposing Data and Methods

    +

    Obviously, you can use private internal variables to keep track of any data your plugin needs to operate. However, if you want to expose a public API to users of your plugin, you should avoid attaching them directly to the planet and instead attach them to the planet's plugins namespace. You should use a name specific to your plugin, and this name should be well documented in your plugin's documentation.

    +
    +
    JavaScript
    + +
    var autorotate = function(degreesPerTick) {
    +  return function(planet) {
    +    var paused = false;
    +
    +    planet.plugins.autorotate = {
    +      pause:  function() { paused = true;  },
    +      resume: function() { paused = false; }
    +    };
    +
    +    planet.onDraw(function() {
    +      if (paused) return;
    +
    +      var rotation = planet.projection.rotate();
    +      rotation[0] += degreesPerTick;
    +      if (rotation[0] >= 180) rotation[0] -= 360;
    +      planet.projection.rotate(rotation);
    +    });
    +  };
    +};
    +
    +planet.loadPlugin(autorotate(5));
    +planet.draw(canvas);
    +setTimeout(function() {
    +  planet.plugins.autorotate.pause();
    +}, 5000);

    diff --git a/js/lib/planetaryjs.min.js b/js/lib/planetaryjs.min.js index 5847624..28b9557 100644 --- a/js/lib/planetaryjs.min.js +++ b/js/lib/planetaryjs.min.js @@ -1,2 +1,2 @@ -/*! Planetary.js 0.0.0 | (c) 2013 Brandon Tilley | Released under MIT License */ -!function(n,t){"function"==typeof define&&define.amd?define(["d3","topojson"],t):"object"==typeof exports?module.exports=t(require("d3"),require("topojson")):n.planetaryjs=t(n.d3,n.topojson,n)}(this,function(n,t,o){"use strict";var e=null;o&&(e=o.planetaryjs);var i=[],r=function(t,o,e){n.timer(function(){t.context.clearRect(0,0,o.width,o.height);for(var n=0;n=o.onInit.length?r(n,t,o):i(l)};i(l)}else r(n,t,o)},a=function(n,t,o,e){l(n,o),n.canvas=t,n.context=t.getContext("2d"),u(n,t,e)},s={plugins:{},noConflict:function(){return o.planetaryjs=e,s},loadPlugin:function(n){i.push(n)},planet:function(){var t=[],o={onInit:[],onDraw:[]},e={plugins:{},draw:function(n){a(e,n,t,o)},onInit:function(n){o.onInit.push(n)},onDraw:function(n){o.onDraw.push(n)},loadPlugin:function(n){t.push(n)},withSavedContext:function(n){if(!this.context)throw new Error("No canvas to fetch context for");this.context.save(),n(this.context),this.context.restore()}};return e.projection=n.geo.orthographic().clipAngle(90).precision(0),e.path=n.geo.path().projection(e.projection),e}};return s.plugins.topojson=function(t){return function(o){o.onInit(function(e){if(t.world)o.world=t.world,setTimeout(e,0);else{var i=t.file||"world-110m.json";n.json(i,function(n,t){if(n)throw new Error("Could not load JSON "+i);o.world=t,e()})}})}},s.plugins.oceans=function(n){return function(t){t.onDraw(function(){t.withSavedContext(function(o){o.beginPath(),t.path.context(o)({type:"Sphere"}),o.fillStyle=n.fill||"black",o.fill(),0!=n.stroke&&(o.strokeStyle=n.stroke,o.stroke())})})}},s.plugins.land=function(n){return function(o){var e=null;o.onInit(function(){e=t.feature(o.world,o.world.objects.land)}),o.onDraw(function(){o.withSavedContext(function(t){t.beginPath(),o.path.context(t)(e),0!=n.fill&&(t.fillStyle=n.fill||"white",t.fill()),n.stroke&&(t.strokeStyle=n.stroke,t.stroke())})})}},s.plugins.borders=function(n){return function(o){var e=null;o.onInit(function(){var n=o.world.objects.countries;e=t.mesh(o.world,n,function(n,t){return n.id!==t.id})}),o.onDraw(function(){o.withSavedContext(function(t){t.beginPath(),o.path.context(t)(e),t.strokeStyle=n.stroke||"gray",t.stroke()})})}},s.plugins.earth=function(n){var n=n||{},t=n.topojson||{},o=n.oceans||{},e=n.land||{},i=n.borders||{};return function(n){s.plugins.topojson(t)(n),s.plugins.oceans(o)(n),s.plugins.land(e)(n),s.plugins.borders(i)(n)}},s.plugins.pings=function(){var t=[],o=function(n,o,e){var e=e||{};e.color=e.color||"white",e.ttl=e.ttl||2e3,e.angle=e.angle||5,t.push({lat:n,lng:o,time:new Date,options:e})},e=function(n,o,e){for(var r=[],l=0;l=o.onInit.length?r(n,t,o):e(l)};e(l)}else r(n,t,o)},s=function(n,t,o,i){l(n,o),n.canvas=t,n.context=t.getContext("2d"),u(n,t,i)},a={plugins:{},noConflict:function(){return o.planetaryjs=i,a},loadPlugin:function(n){e.push(n)},planet:function(){var t=[],o={onInit:[],onDraw:[]},i={plugins:{},draw:function(n){s(i,n,t,o)},onInit:function(n){o.onInit.push(n)},onDraw:function(n){o.onDraw.push(n)},loadPlugin:function(n){t.push(n)},withSavedContext:function(n){if(!this.context)throw new Error("No canvas to fetch context for");this.context.save(),n(this.context),this.context.restore()}};return i.projection=n.geo.orthographic().clipAngle(90).precision(0),i.path=n.geo.path().projection(i.projection),i}};return a.plugins.topojson=function(t){return function(o){o.plugins.topojson={},o.onInit(function(i){if(t.world)o.plugins.topojson.world=t.world,setTimeout(i,0);else{var e=t.file||"world-110m.json";n.json(e,function(n,t){if(n)throw new Error("Could not load JSON "+e);o.plugins.topojson.world=t,i()})}})}},a.plugins.oceans=function(n){return function(t){t.onDraw(function(){t.withSavedContext(function(o){o.beginPath(),t.path.context(o)({type:"Sphere"}),o.fillStyle=n.fill||"black",o.fill(),0!=n.stroke&&(o.strokeStyle=n.stroke,o.stroke())})})}},a.plugins.land=function(n){return function(o){var i=null;o.onInit(function(){var n=o.plugins.topojson.world;i=t.feature(n,n.objects.land)}),o.onDraw(function(){o.withSavedContext(function(t){t.beginPath(),o.path.context(t)(i),0!=n.fill&&(t.fillStyle=n.fill||"white",t.fill()),n.stroke&&(t.strokeStyle=n.stroke,t.stroke())})})}},a.plugins.borders=function(n){return function(o){var i=null;o.onInit(function(){var n=o.plugins.topojson.world,e=n.objects.countries;i=t.mesh(n,e,function(n,t){return n.id!==t.id})}),o.onDraw(function(){o.withSavedContext(function(t){t.beginPath(),o.path.context(t)(i),t.strokeStyle=n.stroke||"gray",t.stroke()})})}},a.plugins.earth=function(n){var n=n||{},t=n.topojson||{},o=n.oceans||{},i=n.land||{},e=n.borders||{};return function(n){a.plugins.topojson(t)(n),a.plugins.oceans(o)(n),a.plugins.land(i)(n),a.plugins.borders(e)(n)}},a.plugins.pings=function(){var t=[],o=function(n,o,i){var i=i||{};i.color=i.color||"white",i.ttl=i.ttl||2e3,i.angle=i.angle||5,t.push({lat:n,lng:o,time:new Date,options:i})},i=function(n,o,i){for(var r=[],l=0;l