Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='map'></div>
              
            
!

CSS

              
                body {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  color: #555;
  background-color: #555;
  background-image:
  url("https://github.com/dabrorius/codepen-assets/raw/master/flights-map/bg.png");
}

.land {
  fill: #222;
  stroke-width: 1px;
  stroke: #333;
}

#map {
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  position: fixed;
}
              
            
!

JS

              
                let parent = d3.select('#map')
let parentBox = parent.node().getBoundingClientRect()

let width
let height

let svg = parent.append('svg').attr('style', 'width: 100%; height: 100%;')

const zagreb = {name: 'Zagreb', position: [15.981919000000062, 45.8150108]}
const london = {name: 'London', position: [-0.1277582, 51.5073509]}
const zadar = {name: 'Zadar', position: [15.231364799999938, 44.119371]}
const osijek = {name: 'Osijek', position: [18.69551439999998, 45.5549624]}
const amsterdam = {name: 'Amsterdam', position: [4.895167899999933, 52.3702157]}
const belgrade = {name: 'Belgrade', position: [20.44892159999995, 44.786568]}
const sofia = {name: 'Sofia', position: [23.321867500000053, 42.69770819999999]}
const paris = {name: 'Paris', position: [2.3522219000000177, 48.85661400000001]}
const montpellier = {name: 'Montpellier', position: [3.8767159999999876, 43.610769]}
const bruxelles = {name: 'Bruxelles', position: [4.3517, 50.8503]}

const flights = [
  [zadar, london],
  [london, osijek],
  [zagreb, amsterdam],
  [zagreb, belgrade],
  [belgrade, sofia],
  [zagreb, paris],
  [paris, montpellier],
  [zagreb, bruxelles]
]

let activeFlightIndex = null
let activeFlight = flights[0]
let changeEvery = 5000
let transitionDuration = 1000
const labelOffset = {x: 5, y: 5}

const activeColor = '#BD1550'
const inactiveColor = '#999999'

let focusRegion = {type: 'FeatureCollection', features: [{type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[15, 34], [15, 58], [60, 65]]] }}]}
d3.json('https://raw.githubusercontent.com/dabrorius/codepen-assets/master/flights-map/world.geojson', function (error, data) {
  if (error) { throw (error) }

  var projection = d3.geoMercator()
  var path = d3.geoPath().projection(projection)
  let land = svg.append('path').attr('class', 'land').attr('d', path(data))

  let line = svg.selectAll('line').data([{cities: activeFlight}]).enter()
    .append('line')
    .attr('stroke', activeColor)

  svg.selectAll('circle').data(activeFlight).enter()
    .append('circle')
    .attr('r', 0)
    .attr('fill', inactiveColor)

  svg.selectAll('text').data(activeFlight).enter()
    .append('text')
    .attr('font-size', 12)
    .attr('fill', activeColor)

  fitToScreen()

  d3.timer(function (elapsed) {
    let currentIndex = Math.floor(elapsed / changeEvery) % flights.length
    if (currentIndex !== activeFlightIndex) {
      activeFlightIndex = currentIndex
      activeFlight = flights[activeFlightIndex]

      svg.selectAll('circle').data(activeFlight)
        .transition()
          .attr('fill', inactiveColor)
        .transition()
          .attr('r', 3)
          .duration(transitionDuration)
          .attr('cx', (d) => { return projection(d.position)[0] })
          .attr('cy', (d) => { return projection(d.position)[1] })
        .transition()
          .attr('fill', activeColor)

      svg.selectAll('line').data([{cities: activeFlight}])
      .transition()
        .attr('opacity', 0)
      .transition()
        .duration(transitionDuration)
        .attr('x1', (d) => { return projection(d.cities[0].position)[0] })
        .attr('y1', (d) => { return projection(d.cities[0].position)[1] })
        .attr('x2', (d) => { return projection(d.cities[1].position)[0] })
        .attr('y2', (d) => { return projection(d.cities[1].position)[1] })
      .transition()
        .attr('opacity', 1)

      svg.selectAll('text').data(activeFlight)
        .transition()
          .attr('opacity', 0)
        .transition()
          .duration(transitionDuration)
          .attr('x', (d) => { return projection(d.position)[0] + labelOffset.x })
          .attr('y', (d) => { return projection(d.position)[1] - labelOffset.y })
          .text((d) => { return d.name })
        .transition()
          .attr('opacity', 1)
    }

    let dashingSpeed = 600
    let dashOffset = elapsed / dashingSpeed % 100
    line.attr('style', `stroke-dasharray:5,5;stroke-dashoffset:${dashOffset}%`)
  })

  function fitToScreen () {
    parentBox = parent.node().getBoundingClientRect()
    if (width !== parentBox.width || height !== parentBox.height) {
      width = parentBox.width
      height = parentBox.height
      projection.fitExtent([[0, 0], [width, height]], focusRegion)
      land.attr('d', path(data))

      svg.selectAll('circle').data(activeFlight)
        .attr('cx', (d) => { return projection(d.position)[0] })
        .attr('cy', (d) => { return projection(d.position)[1] })

      svg.selectAll('line').data([{cities: activeFlight}])
        .attr('x1', (d) => { return projection(d.cities[0].position)[0] })
        .attr('y1', (d) => { return projection(d.cities[0].position)[1] })
        .attr('x2', (d) => { return projection(d.cities[1].position)[0] })
        .attr('y2', (d) => { return projection(d.cities[1].position)[1] })

      svg.selectAll('text').data(activeFlight)
        .attr('x', (d) => { return projection(d.position)[0] })
        .attr('y', (d) => { return projection(d.position)[1] })
    }
  }

  window.onresize = fitToScreen
})

              
            
!
999px

Console