cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Canvas generated planets

A Pen By Captain Anonymous

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <canvas id="c" width="1200" height="500"></canvas>
            
          
!
            
              body{background:black;}
            
          
!
            
              var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

function mkGradient(opts){
    opts.ctx.rect(0, 0, opts.r2*2, opts.r2*2);
    
  var g = opts.ctx.createRadialGradient(opts.r2*(opts.s ? .75:1), opts.r2*(opts.s ? .75:1), opts.r1, opts.r2*(opts.s ? .75:1), opts.r2*(opts.s ? .75:1), opts.r2);
    
    g.addColorStop(0, opts.c1);
    g.addColorStop(1, opts.c2);
    
    
    opts.ctx.fillStyle = g;
    opts.ctx.fill();
}

function mkPlanet(opts){
    var canvas = document.createElement('canvas');
    canvas.width = opts.r*2.4;
    canvas.height = opts.r*2.4;
    var ctx = canvas.getContext('2d');
    
    // Halo
    mkGradient({
        ctx: ctx,
        r1: opts.r,
        r2: opts.r*1.2,
        c1: opts.h1,
        c2: opts.h2
    });
    
    // Translate to edge of halo.
    var offset = opts.r*1.2-opts.r;
    ctx.translate(offset, offset);

    /// Create planet outline clip
    ctx.beginPath();
    ctx.arc(opts.r, opts.r, opts.r, 0, 2 * Math.PI);
    ctx.closePath();
    ctx.clip();

    // Fill the ocean
    mkGradient({
        ctx: ctx,
        r1: 0,
        r2: opts.r,
        c1: opts.o2,
        c2: opts.o1
    });
    // Add some continents
    for(var i=0;i<opts.r*20; i++){
        ctx.globalAlpha = Math.random()*.1
        var x = Math.random()*opts.r*2;
        var y = Math.random()*opts.r*2
        ctx.beginPath();
        ctx.arc(x, y, Math.random()*(opts.r/5), 0, 2 * Math.PI);
        ctx.fillStyle = i > opts.r*10 ? opts.c3 : opts.c2;
        ctx.fill();
    }  
    ctx.globalAlpha = 1.0
    
    // shadow
    if(opts.s){
      mkGradient({
          ctx: ctx,
          r1: opts.r*.4,
          r2: opts.r,
          c1: 'rgba(0,0,0,0)',
          c2: 'rgba(0,0,0,.9)',
          s: true
      });
    }
    //atmoshpere
    mkGradient({
          ctx: ctx,
          r1: opts.r*.95,
          r2: opts.r,
          c1: 'rgba(255,255,255,0)',
          c2: 'rgba(255,255,255,.6)'
     });
    return canvas;
}

var earth = mkPlanet({
    r:100,
    o1: '#8a7359', // ocean 1
    o2: '#4c522c', // ocean color 2
    c2: '#1d80ef', // land color 1
    c3: '#080930', // land color 2
    h1: '#48d6f9', // halo color 1
    h2: 'rgba(134,59,249,0)', // halo color 2
    s: true
});

var europa = mkPlanet({
    r:50,
    o1: '#9f998d',
    o2: '#9f998d',
    c2: '#74301b',
    c3: '#6d6b79',
    h1: 'rgba(255,255,255,.5)',
    h2: 'rgba(134,59,249,0)',
    s: true
});


var mars = mkPlanet({
    r:90,
    o1: '#a56349',
    o2: '#a56349',
    c2: '#f7a27d',
    c3: '#603f36',
    h1: 'rgba(255,122,0,.5)',
    h2: 'rgba(255,12,0,0)',
    s: true
});


var sun = mkPlanet({
    r:200,
    o1: '#e91f00',
    o2: '#e91f00',
    c2: '#ffa614',
    c3: '#faf100',
    h1: 'rgba(255,122,0,1)',
    h2: 'rgba(255,12,0,0)',
    s: false
});

ctx.drawImage(sun,0,0);
ctx.drawImage(earth,500,120);
ctx.drawImage(mars,750,130);
ctx.drawImage(europa,1000,180);


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console