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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1" height="1" style="visibility:hidden">
  <defs>
    <filter id="goo">
      <feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" />
      <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 18 -7" result="goo" />
      <feBlend in="SourceGraphic" in2="goo" />
  	</filter>
  </defs>
</svg>

<svg class="poolarea" xmlns="http://www.w3.org/2000/svg" width="788.9" height="335" viewBox="0 0 788.9 335">
  <g id="poolcontain">
    <path fill="#c4e3ff" d="M83.8 338.4L262.3 266.4 529.5 266.4 705.2 338.4 621.5 338.4 511.5 277.7 277.5 277.7 167.4 338.4 83.8 338.4z" />
    <path fill="#25527a" d="M221.9 338.4L277.5 298 277.5 279.4 174.3 338.4 221.9 338.4z" />
    <g id="pool_inside">
       <polygon points="221.9 338.4 277.5 323.9 277.5 279.4 174.3 338.4 221.9 338.4" fill="#25527a"/>
       <rect x="277.5" y="279.4" width="234" height="44.49" fill="#3373ab"/>
       <polygon points="614.7 338.4 511.5 279.4 511.5 323.9 567.1 338.4 614.7 338.4" fill="#5594cc"/>
     </g>
    <path fill="#f2f9ff" d="M277.5 277.7H511.5V279.40999999999997H277.5z" />
    <path fill="#f2f9ff" d="M621.5 338.4L511.5 277.7 511.5 279.4 614.7 338.4 621.5 338.4z" />
  </g>
  <g id="ladder">
    <g>
      <g>
        <path d="M568.4,429.9v42.6c0,1.2-.8,1.6-1.8,1.8H564c-1.1-.1-1.8-.6-1.8-1.8V429.9a2.6,2.6,0,0,1,2.6-2.6h.9A2.6,2.6,0,0,1,568.4,429.9Z" transform="translate(-105.5 -169.1)" fill="#859bad" />
        <path d="M566.6,429.9v44.4H564V429.9a.8.8,0,0,1,.8-.8h.9A.8.8,0,0,1,566.6,429.9Z" transform="translate(-105.5 -169.1)" fill="#ebf6ff" />
      </g>
      <g>
        <path d="M602.5,429.9v42.6c0,1.2-.8,1.6-1.8,1.8h-2.6c-1.1-.1-1.8-.6-1.8-1.8V429.9a2.6,2.6,0,0,1,2.6-2.6h.9A2.6,2.6,0,0,1,602.5,429.9Z" transform="translate(-105.5 -169.1)" fill="#859bad" />
        <path d="M600.7,429.9v44.4h-2.6V429.9a.8.8,0,0,1,.8-.8h.9A.8.8,0,0,1,600.7,429.9Z" transform="translate(-105.5 -169.1)" fill="#ebf6ff" />
      </g>
    </g>
    <path d="M566.8,467.1h0a2.5,2.5,0,0,1,2.5-2.5h26a2.5,2.5,0,0,1,2.5,2.5h0a2.5,2.5,0,0,1-2.5,2.5h-26A2.5,2.5,0,0,1,566.8,467.1Z" transform="translate(-105.5 -169.1)" fill="#859bad" />
    <path d="M568.7,468.4c-.2,0-.7,0-.7-1.3s.5-1.3.7-1.3H596c.2,0,.7,0,.7,1.3s-.5,1.3-.7,1.3Z" transform="translate(-105.5 -169.1)" fill="#ebf6ff" />
    <path d="M566.8,456.8h0a2.5,2.5,0,0,1,2.5-2.5h26a2.5,2.5,0,0,1,2.5,2.5h0a2.5,2.5,0,0,1-2.5,2.5h-26A2.5,2.5,0,0,1,566.8,456.8Z" transform="translate(-105.5 -169.1)" fill="#859bad" />
    <path d="M568.7,458.1c-.2,0-.7,0-.7-1.3s.5-1.3.7-1.3H596c.2,0,.7,0,.7,1.3s-.5,1.3-.7,1.3Z" transform="translate(-105.5 -169.1)" fill="#ebf6ff" />
  </g>
</svg>
            
          
!
            
              #poolcontain {
  position: relative;
}

.poolarea {
  position: absolute;
  left: 50%;
  transform: translate3d(-400px, -90px, 0);
}
            
          
!
            
              let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"),
    svgNS = svg.namespaceURI,
    vbx = document.createElementNS(svgNS, "viewBox"),
    g = document.createElementNS(svgNS, "g"),
    color = '#73d2ff',
    width = 800,
    height = 300,
    xA = new Array(),
		yA = new Array(),	
		xmin = -10,
		xmax = 10,	
		ymin = -20,
		ymax = 20,	
		xorig = 300,
		yorig = 100,
		n = 0,	
		bk,
		ball,
    ball2,
		xscal,
		yscal;

//set the SVG and the body
document.body.style.background = '#222';
const poolC = document.getElementById("poolcontain");
append(poolC, g);
setAttributes(g, {
  "class": "elastic",
  "filter": "url('#goo')",
  "style": "transform:translate3d(600px, 344px, 0) rotate(180deg) scale(0.5)"
});

//***** Create the Bk *****//
function Background(color) {
	this.panel = document.createElementNS(svgNS, "path");
  this.color = color;
  this.y = 0;
  this.y2 = 0;
  this.inc = 150;
  this.start = 175;
}

Background.prototype.draw = function() {
  append(g, this.panel);
  setAttributes(this.panel, {
  	"d": `M ${this.start} 100 Q ${this.start+50} ${this.y2} ${this.start+75} 100 Q ${width/2} ${this.y} ${width-(this.inc+75)} 100 Q ${width-this.inc} ${this.y2} ${width-this.inc} 100 L ${width} 0 L 0 0 Z`,
    "fill": this.color,
  });
};

//***** Create the Ball *****//
function Ball (radius, color) {
	this.circ = document.createElementNS(svgNS, "circle");
  this.radius = radius;
  this.color = color;
  this.y = 0;
}

Ball.prototype.draw = function() {
  append(g, this.circ);
  setAttributes(this.circ, {
  	"r": this.radius,
    "cx": width/2,
    "cy": this.y,
    "fill": this.color,
  });
};

//***** Set up the Motion *****//
window.onload = init; 
 
function init() {
	plotter();
	placeBall();
	placeBk();
	window.requestAnimationFrame(step);
} 

function plotter(){
	xscal = (xmax-xmin)/(width/4);
	yscal = (ymax-ymin)/(height/4);	
	for (let i = 0; i <= 1500; i++){
		//set the boundary for how much plays here
		xA[i] = (i - 0) * 0.02;
		yA[i] = f(xA[i]);						   
	}
}

function f(x) {
	let y;
	y = (x+3.6)*(x+2.5)*(x+1)*(x-0.5)*(x-2)*(x-3.5)*Math.exp(-x*x/4);
	return y;
}

function placeBk() {
	let orig = yA[0]/yscal + yorig;
	bk = new Background(color);
	bk.x = xA[0]/xscal + xorig;
	bk.y2 = orig;
	bk.y = orig;
	bk.draw();				
}

function placeBall() {
	//ball 1
	ball = new Ball(12, '#59c1f2');
	ball.x = xA[0]/xscal + xorig;
	ball.draw();
	//ball 2	
	ball2 = new Ball(5, '#bee5f7');
	ball2.x = xA[0]/xscal + xorig;
	ball2.draw();					
}

//use rAF to animate but put a boundary on it so it doesn't run forever
function step(timestamp) {
	//console.log(bk.x);
  if (bk.x < 440){
    moveBk();
    moveBall();
    window.requestAnimationFrame(step);
  }
}	

function moveBk() {
	bk.x = xA[n]/xscal + xorig;
	bk.y2 = yA[n]/(yscal*4) + yorig;
	bk.y = -yA[n]/(yscal/1) + yorig;
	bk.draw();
	n++;
} 

function moveBall() {
	let yMove = yA[n]/(yscal/1) + (yorig - 30);
	let yMove2 = (yA[n]/yscal)*2 + (yorig - 50);
	ball.circ.style.transform = `translate3d(0, ${yMove}px, 0`;
	ball2.circ.style.transform = `translate3d(0, ${yMove2}px, 0`;
	n++;
} 

//***** helper functions *****//

//function to set multiple attributes at once
function setAttributes(el, attrs) {
  for (let key in attrs) {
    el.setAttribute(key, attrs[key]);
  }
}

//function to append children because typing
function append(el, addition) {
  el.appendChild(addition);
}
 
            
          
!
999px
Loading ..................

Console