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

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="canvas"></canvas>

-50.times do
  .fall
    .wave ❄️
            
          
!
            
              $flakes: 52;

html, body {
  margin: 0;
  background-color: #222;
  overflow: hidden;
}

canvas {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: block;
}

.fall {
  position: absolute;
  animation: fall;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  @for $flake from 1 to $flakes {  
    &:nth-child(#{$flake}) {
      left: random(100) * 1vw;
      animation-duration: 4s + random(10)*200ms;
      animation-delay: random(100)*-100ms;
      .wave {
        animation-delay: random(100)*-20ms;
        animation-duration: 600ms + random(5)*50ms;
      }
    }
  }
}

.wave {
  color: transparent;
  text-shadow: 0 0 0 white;
  animation: wave;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes fall {
  from { transform: translateY(-20vh); }
  to { transform: translateY(110vh); }
}

@keyframes wave {
  from { transform: translateX(-3vw); }
  to { transform: translateX(3vw); }
}
            
          
!
            
              /*
  Johan Karlsson (DonKarlssonSan)

  It's an Iterated Function System (IFS)
  
  Based on the Barnsley Fern
  https://en.wikipedia.org/wiki/Barnsley_fern
  
  With coefficients stolen from
  Rick Wicklin
  http://blogs.sas.com/content/iml/2012/12/14/a-fractal-christmas-tree.html
  
*/

(function (tree) {
  var canvas;
  var ctx;

  var matrix = [
    [ 0.03,  0   , 0   , 0.1 , 0, 0,    0.02],
    [ 0.85,  0   , 0   , 0.85, 0, 1.5 , 0.6 ],
    [ 0.8 ,  0   , 0   , 0.8 , 0, 1.5 , 0.1 ],
    [ 0.2 , -0.08, 0.15, 0.22, 0, 0.85, 0.07],
    [-0.2 ,  0.08, 0.15, 0.22, 0, 0.85, 0.07],
    [ 0.25, -0.1 , 0.12, 0.25, 0, 0.3 , 0.07],
    [-0.2 ,  0.1 , 0.12, 0.2 , 0, 0.4 , 0.07]
  ];
  
  var aIndex = 0;
  var bIndex = 1;
  var cIndex = 2;
  var dIndex = 3;
  var eIndex = 4;
  var fIndex = 5;
  var probabilityIndex = 6;
  
  tree.init = function(canvasId) {
    canvas = document.getElementById(canvasId);
    canvas.width = 500;
    canvas.height = 500;
    ctx = canvas.getContext("2d");
    ctx.fillStyle = "green";
  }
  
  // This could probably be optimized...
  // Or just put the accumulated probability
  // in the last column of the matrix instead.
  function getTransformation(random) {
    var accumulatedProbability = 0;
    for(var i = 0; i < matrix.length; i++) {
      accumulatedProbability += matrix[i][probabilityIndex];
      if(accumulatedProbability >= random) {
        return matrix[i];
      }
    }
  }
  
  function drawBall(x, y) {
    ctx.save();
    var r = 10;
    // Gradient for a spherical look
    var gradient = ctx.createRadialGradient(
      x, 
      y, 
      r*0.05, 
      x, 
      y, 
      r);
    gradient.addColorStop(0, "white");
    var h = Math.round(Math.random()*360);
    gradient.addColorStop(1, `hsl(${h}, 100%, 50%)`);
    ctx.fillStyle = gradient;
    ctx.beginPath();
    ctx.arc(x, y, r, 0, Math.PI * 2);
    ctx.fill();
    ctx.restore();
  }

  
  tree.draw = function() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    var x = 0;
    var y = 0;
    var oldX;
    var oldY;
    var transform;
    var ballCoordinates = [];
    for(var i = 0; i < 100000; i++){
      oldX = x;
      oldY = y;
      transform = getTransformation(Math.random());
      x = transform[aIndex]*oldX + transform[bIndex]*oldY + transform[eIndex];
      y = transform[cIndex]*oldX + transform[dIndex]*oldY + transform[fIndex];
      var x1 = (x + 1.25) * 200; 
      var y1 = canvas.height - y * 49;
      if(Math.random() < 0.00081 && y1 < canvas.height - 30) {
        // TODO: check to see if there already is a ball nearby... 
        ballCoordinates.push([x1, y1]);
      } else {
        ctx.fillRect(x1, y1, 1, 1);
      }
    }
    ballCoordinates.forEach(b => {
      drawBall(b[0], b[1]);

    });
  }
})(window.tree = window.tree || {});

tree.init("canvas");
tree.draw();
            
          
!
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