Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

              
                <h1>Personnalize colors, size, and animation duration</h1>

<div id="trianglesContainer"></div>
              
            
!

CSS

              
                body {
  background-color: #333;
  text-align:center;
}

h1 {
  color:white;
  font-family:"Raleway";
  margin:2rem;
}

#trianglesContainer {
  /* You can choose whatever size you want */
  margin:0 10vw;
  width:80vw;
  height:60vh;
  background-color:#BBB;
  
  border: 2px grey solid;
}


/* Font Raleway */
@import url(https://fonts.googleapis.com/css?family=Raleway);
              
            
!

JS

              
                // Parameters 
var 
axe = "X", // values can be "X" or "Y"
numberOfSquare = 15, // How many square on the axe
greyMinimum = 210, // Between 0 and 254
greyMaximum = 245, // Between 1 and 255
animMin = 2, // Animation durations (in seconds)
animMax = 8;


// The container in the HTML page
var $tc = $("#trianglesContainer"); 

function createTriangles(){
  let w = $tc.width(), h = $tc.height(), dx;
  let $svg = $('<svg width="'+w+'" height="'+h+'" xmlns="http://www.w3.org/2000/svg">');
  
  // Empty the container (usefull for the page resized calls)
  $tc[0].innerHTML = "";
  
  if(axe = "X")
    dx = w / numberOfSquare;
  else 
    dx = h / numberOfSquare;
  
  
  for(let i = 0; i < w / dx ; i++) {
    for(let j = 0; j < h/dx ; j++) {
      // Random Colors for animation
      let c1 = rdmColor(greyMinimum,greyMaximum);
      let c2 = rdmColor(greyMinimum,greyMaximum);
      
      // Path direction for each triangles in a square
      let d = [];
      // Middle of the current square, to make triangles from a square
      let middleX = (i*dx+dx/2), middleY = (j*dx+dx/2);
      
      // Creating the 4 paths directions
			d.push('M '+i*dx+' '+j*dx+' h '+dx+ ' L '+middleX+' '+middleY);
			d.push('M '+i*dx+' '+(j+1)*dx+' h '+dx+ ' L '+middleX+' '+middleY);
			d.push('M '+i*dx+' '+j*dx+' v '+dx+ ' L '+middleX+' '+middleY);
			d.push('M '+(i+1)*dx+' '+j*dx+' v '+dx+ ' L '+middleX+' '+middleY);
      
      d.forEach(function(val,i){
        // Animate tag
        let a = '<animate attributeName="fill" repeatCount="indefinite" dur="'+rdmInt(animMin,animMax)+'s" values="'+c1+';'+c2+';'+c1+'" />';
        // Adding the animated path to the SVG container
        $svg[0].innerHTML += ('<path d="'+val+'" fill="'+c1+'" stroke="'+c1+'">'+a+'</path>');
      });
      
      // Puttin the SVG container in the HTML page
      $tc.append($svg);
    }
  }
  // For Loops end
  
}



// Random int between min and max
function rdmInt(min, max) {
	return Math.round(Math.random() * (max - min) + min)
}

// Random RGB grey between min and max values (from 0 to 255)
function rdmColor(min, max) {
	let color = rdmInt(min,max);
	return 'rgb('+color+','+color+','+color+')';
}

// Create Triangle when the page is loaded
window.onload = createTriangles();

// Recreatee Triangles when the page is resized
$( window ).resize(createTriangles);

              
            
!
999px

Console