Giter Site home page Giter Site logo

binarymuse / planetary.js Goto Github PK

View Code? Open in Web Editor NEW
1.6K 53.0 180.0 1.87 MB

:earth_americas: Awesome interactive globes for the web

Home Page: http://planetaryjs.com

License: MIT License

JavaScript 52.47% CSS 43.23% HTML 4.30%
topojson javascript interactive-globes d3 visualization

planetary.js's Introduction

Planetary.js

Planetary.js is a JavaScript library for building awesome interactive globes, like this one:

Planetary.js Screenshot

Planetary.js is based on D3.js and TopoJSON. It has built-in support for zoom, rotation, mouse interaction, and displaying animated "pings" at any coordinate. Via plugins, Planetary.js can be extended to do whatever you want!

Examples, documentation, and more can be found at planetaryjs.com.

Requirements

Download

Download Planetary.js from the Planetary.js web site.

Quick Start

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.

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 (yourApp.js):

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);

Congratulations! You've rendered your first globe.

Documentation

In-depth documentation can be found at planetaryjs.com.

Building

Building the project requires Node.js. Once you've installed the project's dependencies with npm install, you can build the JavaScript to the dist directory with npm run build.

License

Planetary.js is licensed under the MIT license. See the LICENSE file for more information.

planetary.js's People

Contributors

binarymuse avatar christiannaths avatar darul75 avatar hb9cwp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

planetary.js's Issues

Not working add not defined error

I am working on following code (function() {
var globe = planetaryjs.planet();
// Load our custom autorotate plugin; see below.
globe.loadPlugin(autorotate(10));
// The earth plugin draws the oceans and the land; it's actually
// a combination of several separate built-in plugins.
//
// Note that we're loading a special TopoJSON file
// (world-110m-withlakes.json) so we can render lakes.
globe.loadPlugin(planetaryjs.plugins.earth({
topojson: { file: '/world-110m-withlakes.json' },
oceans: { fill: '#000080' },
land: { fill: '#339966' },
borders: { stroke: '#008000' }
}));
// Load our custom lakes plugin to draw lakes; see below.
globe.loadPlugin(lakes({
fill: '#000080'
}));
// The pings plugin draws animated pings on the globe.
globe.loadPlugin(planetaryjs.plugins.pings());
// The zoom and drag plugins enable
// manipulating the globe with the mouse.
globe.loadPlugin(planetaryjs.plugins.zoom({
scaleExtent: [100, 300]
}));
globe.loadPlugin(planetaryjs.plugins.drag({
// Dragging the globe should pause the
// automatic rotation until we release the mouse.
onDragStart: function() {
this.plugins.autorotate.pause();
},
onDragEnd: function() {
this.plugins.autorotate.resume();
}
}));
// Set up the globe's initial scale, offset, and rotation.
globe.projection.scale(175).translate([175, 175]).rotate([0, -10, 0]);

// Every few hundred milliseconds, we'll draw another random ping.
var colors = ['red', 'yellow', 'white', 'orange', 'green', 'cyan', 'pink'];
setInterval(function() {
var lat = Math.random() * 170 - 85;
var lng = Math.random() * 360 - 180;
var color = colors[Math.floor(Math.random() * colors.length)];
globe.plugins.pings.add(lng, lat, { color: color, ttl: 2000, angle: Math.random() * 10 });
}, 150);

var canvas = document.getElementById('rotatingGlobe');
// Special code to handle high-density displays (e.g. retina, some phones)
// In the future, Planetary.js will handle this by itself (or via a plugin).
if (window.devicePixelRatio == 2) {
canvas.width = 800;
canvas.height = 800;
context = canvas.getContext('2d');
context.scale(2, 2);
}
// Draw that globe!
globe.draw(canvas);

// This plugin will automatically rotate the globe around its vertical
// axis a configured number of degrees every second.
function autorotate(degPerSec) {
// Planetary.js plugins are functions that take a planet instance
// as an argument...
return function(planet) {
var lastTick = null;
var paused = false;
planet.plugins.autorotate = {
pause: function() { paused = true; },
resume: function() { paused = false; }
};
// ...and configure hooks into certain pieces of its lifecycle.
planet.onDraw(function() {
if (paused || !lastTick) {
lastTick = new Date();
} else {
var now = new Date();
var delta = now - lastTick;
// This plugin uses the built-in projection (provided by D3)
// to rotate the globe each time we draw it.
var rotation = planet.projection.rotate();
rotation[0] += degPerSec * delta / 1000;
if (rotation[0] >= 180) rotation[0] -= 360;
planet.projection.rotate(rotation);
lastTick = now;
}
});
};
};

// This plugin takes lake data from the special
// TopoJSON we're loading and draws them on the map.
function lakes(options) {
options = options || {};
var lakes = null;

return function(planet) {
  planet.onInit(function() {
    // We can access the data loaded from the TopoJSON plugin
    // on its namespace on `planet.plugins`. We're loading a custom
    // TopoJSON file with an object called "ne_110m_lakes".
    var world = planet.plugins.topojson.world;
    lakes = topojson.feature(world, world.objects.ne_110m_lakes);
  });

  planet.onDraw(function() {
    planet.withSavedContext(function(context) {
      context.beginPath();
      planet.path.context(context)(lakes);
      context.fillStyle = options.fill || 'black';
      context.fill();
    });
  });
};

};
})();
Please help

country names

I'm just wondering if it's possible to identify specific countries based on name (for example to fill certain countries with a different color).
In d3.js examples there is an additional tsv file loaded for this purpose, like here: http://bl.ocks.org/mbostock/4183330

Can this be done with planetary.js ?

thanks

Remove Pings or redraw canvas?

Is there a method to remove any or all the pings? Or is there a method to redraw the canvas and reset everything including removing all the pings?

3d to 2d

Is possible to convert the 3D globe to a 2D flat map?

Pass the lineWidth as an option for the ping plugin

Depending on the size and the color, pings can be really difficult to see.

It would be good if this plugin had an option to pass the lineWidth like so:
line 267:

options.lineWidth = options.lineWidth || config.lineWidth || 1;

and line 301:

context.lineWidth = ping.options.lineWidth;

missing plugins?

Not sure if plugins aren't exported or I've missed a setup step (have scoured docs).
I can get a basic planet on the screen but I get an error when trying to use a plugin like in the docs.

image

I just have this import

import * as Planetaryjs from "planetary.js";

and then applying the plugin like so:

const planet = Planetaryjs.planet();
const canvas = document.getElementById("globe");
planet.loadPlugin(Planetaryjs.plugins.autocenter({ extraHeight: -120 }));

Abandoned Project?

Hey, it looks great and the example is awesome, but it feels like this project is abandoned. Is there any predessor?

Allow more control over the animation loop

doDrawLoop currently decides how often to redraw planets (based on d3.timer). As per part of #12, it would be nice to allow end-users to customize the draw ticks. We should extract the main draw body:

planet.context.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < hooks.onDraw.length; i++) {
  hooks.onDraw[i]();
}

and allow end-users to call it whenever they want.

Flat Map

Is there any option to use the globe in a 'flat' view? And if so how can I draw an Arc between 2 points and not just a straight line

Something like this:

7nVfx

map tiles

Skinning map tiles (perhaps after porting to WebGL) onto the globe would be awesome. They'd change depending on the zoom level.

npm package or?

Is there an NPM package or could there be one? I want to use this on a react project, adding a <script> js source is suboptimal.

webgl

It'd be awesome to convert this to WebGL so that it's buttery smooth even on older phones if they have a GPU.

I tested on my Lumia 1020 Windows Phone, and the fps was low. I mean, it's Windows, but still. IE11 supports WebGL (most of it), so the globe would probably be smooth on IE11 mobile if it were in WebGL. Many of the Three.js WebGL demos run buttery smooth on my Windows Phone.

Microsoft Edge is about to land on my phone in a day or two, which will have even better WebGL support!

Drawing lines between two points

Could you please provide a basic example on how to draw lines between two points. I have tried this, but the lines are straight and not projected (curved):

planet.onDraw(function () {
    planet.withSavedContext(function (context) {
        var arc = d3.geo.greatArc().source([lng1, lat1]).target([lng2, lat2])();
        context.beginPath();
        planet.path.context(context)(arc);
        context.stroke();
        context.closePath();
    });
});

[SOLVED] TopoJson.V1.Min.Js error on Object.t

I'm receiving 2 of the same errors when attempting to add the rotating earth.
Uncaught TypeError: Cannot read property 'type' of undefined
at Object.t [as feature] (topojson.v1.min.js:47)

I want to fiddle around in the javascript file, but I fear I may end up making things worse. Any help would be great. Thanks in advance!

topo1
topo2

plugins and drawing

The top of planetary.js holds doDrawLoop. It would be very valuable if one could refine it such that you specify a timeout after which you call requestAnimationFrame instead. This will decrease the load for complex web based applications.

Also I think the startDraw function has a bug. The folowing code should not belong in there. It may be neccesary to init the plugins separate from the stopped state.
if (planet.stopped !== true) {
initPlugins(planet, localPlugins);
}

I had > 100 plugins after a few minutes and the whole system came to a crawl. I fixed it by calling
globe.stop ( );
globe.draw ( canvas );
anytime

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.