css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="svg1">
    <svg id="pine-examples"></svg>
  </div>
  <div class="svg2">
    <svg id="jump-animation"></svg>
  </div>
  <div class="svg3">
    <svg id="circles"></svg>
  </div>
<svg id="jump-animation-start"></svg>
            
          
!
            
              #circles,
    #pine-examples,
    #jump-animation-start,
    #jump-animation {
      display: block;
      margin: 0 auto;
      max-width: 400px;
      width: 100%;
    }
    
    #circles {
      max-width: 300px;
    }
    
    #pine-examples {
      margin: 20px auto;
    }
            
          
!
            
              const ancho = 37;
const space = 2;
const alto = ancho;
const duration = 1000;

const colors = ["#dbe11e", "#cedb2a", "#c2d92d", "#b9d534", "#abcf37", "#9ecb3b", "#93c73e", "#85c441", "#79c144", "#68bd46"];
const colorFromCoords = (x, y) => {
  const yOffset = [0, 1, 4, 9];
  return colors[x+ yOffset[y]];
};

const drawTriangle = (s, x, y, inverted, offsetX, offsetY = 0) => {
  let x0,x1,x2,y0,y1,y2;
  if(x % 2 === 0 && !inverted ) {
    x0 = (ancho+2*space)*x/2 + offsetX;
    x1 = (ancho+2*space)*x/2 + ancho + offsetX;
    x2 = (ancho+2*space)*x/2 + ancho / 2 + offsetX;

    y0 = (alto + space)*y + alto + offsetY;
    y1 = (alto + space)*y + alto + offsetY;
    y2 = (alto + space)*y + (space - 1) + offsetY;
  } else {
    x0 = (ancho + space*2)*((x-1)/2)+ ancho/2 + space + offsetX;
    x1 = (ancho + space*2)*((x-1)/2)+ ancho + space + offsetX;
    x2 = (ancho + space*2)*((x-1)/2)+ ancho/2 + space + ancho + offsetX;

    y0 = (alto + space)*y + offsetY;
    y1 = (alto + space)*y + alto - (space - 1) + offsetY;
    y2 = (alto + space)*y + offsetY;
  }
  return s.polygon(x0, y0, x1, y1, x2, y2);
};

const maxDelay = duration*0.7;
const maxDuration = duration + maxDelay;
const triangleGradient = (s, x, y, inverted, offsetX, delay) => {
  const color = colorFromCoords(x, y);
  const t = drawTriangle(s, x, y, inverted, offsetX)
  .attr({ fill: color, opacity: 0});

  const animation = () => {
    setTimeout(() => {
      t.stop().attr({opacity: 0}).animate({opacity: 1}, duration);
    }, delay);

    setTimeout(() => {
      t.stop().attr({opacity: 1}).animate({opacity: 0}, duration);
    }, maxDuration + (maxDelay - delay)*2);
    
    setTimeout(() => animation(), maxDuration*2 + maxDelay);
  }
  animation();
};

const gradientAnimation = (s) => {
  triangleGradient(s, 0, 3, true, ancho + space *2, 0)

  triangleGradient(s, 0, 2, false, 0, duration*0.4)
  triangleGradient(s, 1, 2, false, 0, duration*0.3)
  triangleGradient(s, 2, 2, false, 0, duration*0.2)
  triangleGradient(s, 3, 2, false, 0, duration*0.3)
  triangleGradient(s, 4, 2, false, 0, duration*0.4)

  triangleGradient(s, 0, 1, false, ancho/2 + space, duration*0.5)
  triangleGradient(s, 1, 1, false, ancho/2 + space, duration*0.6)
  triangleGradient(s, 2, 1, false, ancho/2 + space, duration*0.5)

  triangleGradient(s, 0, 0, false, ancho + space *2, duration*0.7)
};

const length = ancho + 2*Math.sqrt((ancho/2)**2 + (alto - (space - 1))**2);

const strokeTriangle = (s, x, y, inverted, offsetX) => {
  const color = colorFromCoords(x, y);
  const offsetXStart = Math.floor((Math.random() * length));
  const dashLength = length / 2;

  const triangle = drawTriangle(s, x, y, inverted, 150 + offsetX)
  .attr({fill: "none", stroke: color, strokeWidth: 1, strokeDasharray: [dashLength, length - dashLength]});

  const animation = () => {
    triangle
    .stop()
    .attr({strokeDashoffset: offsetXStart} )
    .animate({strokeDashoffset: offsetXStart + length}, 1500, null, animation);
  };
  animation();
};

const strokeAnimation = (s) => {
  strokeTriangle(s, 0, 3, true, ancho + space *2);

  strokeTriangle(s, 0, 2, false, 0);
  strokeTriangle(s, 1, 2, false, 0);
  strokeTriangle(s, 2, 2, false, 0);
  strokeTriangle(s, 3, 2, false, 0);
  strokeTriangle(s, 4, 2, false, 0);

  strokeTriangle(s, 0, 1, false, ancho/2 + space);
  strokeTriangle(s, 1, 1, false, ancho/2 + space);
  strokeTriangle(s, 2, 1, false, ancho/2 + space);

  strokeTriangle(s, 0, 0, false, ancho + space *2);
};

const triangleWind = (s, x, y, inverted, offsetX) => {
  const color = colorFromCoords(x, y);
  const factor = (3 - y);
  const duration = 250;

  const angle = 2 * factor;
  const tx = 4 * factor;
  const ty = 12 * factor;

  const t = drawTriangle(s, x, y, inverted, 300 + offsetX)
  .attr({ fill: color});

  const tBox = t.getBBox();
  
  const matrixLeft = new Snap.Matrix();
  matrixLeft.translate(-tx, 0);
  matrixLeft.rotate(-angle, tBox.cx, tBox.cy);

  const matrixRight = new Snap.Matrix();
  matrixRight.translate(tx, 0);
  matrixRight.rotate(angle, tBox.cx, tBox.cy);


  const animation = () => {
    t.stop().animate({transform: matrixLeft}, duration, mina.easout, () => {
      t.stop().animate({transform: matrixRight}, duration*2, mina.easout, () => {
        t.stop().animate({transform: null}, duration, mina.easout, animation);
      });
    });
  }
  animation();
};

const pinoWind = (s) => {
  triangleWind(s, 0, 3, true, ancho + space *2);

  triangleWind(s, 0, 2, false, 0);
  triangleWind(s, 1, 2, false, 0);
  triangleWind(s, 2, 2, false, 0);
  triangleWind(s, 3, 2, false, 0);
  triangleWind(s, 4, 2, false, 0);

  triangleWind(s, 0, 1, false, ancho/2 + space);
  triangleWind(s, 1, 1, false, ancho/2 + space);
  triangleWind(s, 2, 1, false, ancho/2 + space);

  triangleWind(s, 0, 0, false, ancho + space *2);
};


const triangleJump = (s, x, y, inverted, offsetX) => {
  const color = colorFromCoords(x, y);
  const t = drawTriangle(s, x, y, inverted, offsetX, 20)
  .attr({ fill: color});
  return t;
};

const jumpAnimationStart = () => {
  const s = new Snap("#jump-animation-start");
  s.attr({viewBox: "0 0 330 200"});
  jumpAnimationCore(s, false);
}

const jumpAnimation = () => {
  const s = new Snap("#jump-animation");
  s.attr({viewBox: "0 0 330 200"});
  jumpAnimationCore(s, true);
}

const jumpAnimationCore = async (s, run) => {
  const plankOffsetX = 123;
  const plankOffsetY = 177;

  const tailLength = 50;
  const plankAngle = -4;
  const numberOfSpins = 1;

  const duration = 500;
  const plankLength = 170;
  const supportSize = 10;

  // Pine
  const topTriangle = triangleJump(s, 0, 0, false, ancho + space *2);
  triangleJump(s, 0, 3, true, ancho + space *2);

  triangleJump(s, 0, 2, false, 0);
  triangleJump(s, 1, 2, false, 0);
  triangleJump(s, 2, 2, false, 0);
  triangleJump(s, 3, 2, false, 0);
  triangleJump(s, 4, 2, false, 0);

  triangleJump(s, 0, 1, false, ancho/2 + space);
  triangleJump(s, 1, 1, false, ancho/2 + space);
  triangleJump(s, 2, 1, false, ancho/2 + space);

  // Plank
  const plank = s.line(0,0, plankLength, 0);
  const plankBox = plank.getBBox();
  const plankUpMtx = new Snap.Matrix();
  plankUpMtx.translate(plankOffsetX, plankOffsetY);
  plankUpMtx.rotate(plankAngle, plankBox.cx, plankBox.cy);
  plank.attr({strokeWidth: 5, stroke: "brown", transform: plankUpMtx});

  const supportRect = s.rect((plankLength - supportSize) / 2, 2, supportSize, supportSize);
  const supportRectMatrix = new Snap.Matrix();
  supportRectMatrix.translate(plankOffsetX, plankOffsetY);
  supportRect.attr({transform: supportRectMatrix});

  // Jump path
  const path = s.path("M10,44Q38,-40,89,167")
  path.attr({fill: "none"})
  const curveLength = path.getTotalLength();

  const topTriBox = topTriangle.getBBox();
  const moveToPoint = path.getPointAtLength(curveLength);
  const topTriDownMtx = new Snap.Matrix();
  topTriDownMtx.translate(moveToPoint.x - 10, moveToPoint.y - 44);
  topTriDownMtx.rotate(plankAngle, topTriBox.cx, topTriBox.cy);
  topTriangle.attr({transform: topTriDownMtx});

  const weightScaleFactor = 0.15;
  const weightUpMtx = new Snap.Matrix();
  weightUpMtx.translate(640, 264);
  weightUpMtx.scale(weightScaleFactor);
  const jsLogo = await new Promise((resolve) => Snap.load("https://anilogo-gcucwsckgj.now.sh/img/js.svg", ( fragment ) => {
    const logo = fragment.select("g");
    logo.attr({transform: weightUpMtx});
    s.append(fragment);
    resolve(logo);
  }));

  const animate = () => {
    topTriangle.attr({transform: topTriDownMtx});

    const plankDownMtx = plankUpMtx.clone();
    plankDownMtx.rotate(-2*plankAngle, plankBox.cx, plankBox.cy);

    const weightDownMtx = weightUpMtx.clone();
    weightDownMtx.translate(0, 205/weightScaleFactor);

    jsLogo
    .stop()
    .attr({transform: weightUpMtx})

    plank
    .stop()
    .attr({transform: plankUpMtx});

    setTimeout(() => {
      jsLogo.animate({transform: weightDownMtx}, duration*0.5, mina.bounce);

      setTimeout(() => {
        plank.animate({transform: plankDownMtx}, duration*0.01);

        Snap.animate(curveLength, 0, (step) => {
          const moveToPoint = path.getPointAtLength(step);

          const topTrgMtx = new Snap.Matrix();
          topTrgMtx.translate(moveToPoint.x - 10, moveToPoint.y - 44);

          const angle = (360 + plankAngle)*numberOfSpins*step/curveLength;
          topTrgMtx.rotate(angle, topTriBox.cx, topTriBox.cy);

          topTriangle.attr({transform: topTrgMtx});
        }, duration, mina.easein, () => setTimeout(() => animate(), 400));
      }, duration*0.20);
    }, 500);
  }

  if(run) {
    animate();
  }
};

const circlesAnimation = () => {
  const size=300;

  const s = new Snap("#circles");
  s.attr({viewBox: "0 0 " + size + " " + size});

  const userPath = s.path("M500,10C229.4,10,10,229.4,10,500c0,270.6,219.4,490,490,490c270.6,0,490-219.4,490-490S770.6,10,500,10z M496.2,255c70,0,126.7,56.7,126.7,126.7s-56.7,126.7-126.7,126.7s-126.7-56.7-126.7-126.7C369.5,311.7,426.3,255,496.2,255z M629.1,745h-266c-25.8,0-78.2-21.1-78.2-46.8v0c0-85.8,70.4-156,156.4-156h109.5c86,0,156.4,70.2,156.4,156C707.4,723.9,654.9,745,629.1,745z")
  .attr({fill: "white"});
  const userBox = userPath.getBBox();
  const circleBg = s.circle(userBox.cx, userBox.cy, userBox.width / 2)
    .attr({fill: "black", stroke: "black", strokeWidth: 20});

  const user = s.group(circleBg, userPath);

  const userScaleFactor = 0.09;
  const userMtx = new Snap.Matrix();
  
  userMtx.translate(size/2 - userBox.width*userScaleFactor / 2, size/2 - userBox.height*userScaleFactor/2);
  userMtx.scale(userScaleFactor);
  user.attr({transform: userMtx, cursor: "pointer"});
  const smallUserMtx = userMtx.clone().scale(0.9, 0.9, userBox.cx, userBox.cy);

  user.click(() => {
    user.animate({transform: smallUserMtx}, 200, mina.easein, () => user.animate({transform: userMtx}, 200));
    const newCircle = s.circle(size/2, size/2, 40);
    user.before(newCircle);
    newCircle.attr({fill: "rgb(244, 203, 217)", strokeWidth: "2", stroke: "red"});

    newCircle.animate({r: size/2, opacity: 0}, 2000, mina.easeout, () => newCircle.remove());
  });
};

window.onload = function() {
  const s = Snap("#pine-examples");
  s.attr({viewBox: "0 0 350 160"});
  gradientAnimation(s);
  strokeAnimation(s);

  // jumpAnimationStart();
  jumpAnimation();

  circlesAnimation();
};

            
          
!
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