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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.


                <script type="application/processing">

Triangle[] triangles;
color[] colors = {color(216, 23, 26), color(55, 113, 152), color(107, 210, 211)}; //the ordered set of triangle colors (can be any length greater than 0)
float triBase; //length of each side of each triangle
float triHeight; //height of each triangle (corresponds to the width of each column of triangles)
boolean grid = true;

void setup() { //sets up canvas, initializes global variables and triangle grid
  size(432, 450);
  triBase = 50.0;
  triHeight = triBase/2.0 * sqrt(3);  
  int cols = int(ceil(width/triHeight)); //number of columns of triangles
  int rows = (int(ceil(height/triBase)) + int(ceil((height-(triBase/2.0)) / triBase)) + 1 ); //number of triangles per column
  triangles = new Triangle[rows * cols]; //number of triangles on the grid
  for(int c=0; c<cols; c++){ //sets up the triangle grid column by column
    for(int r=0; r<rows; r++){
      triangles[(c*rows)+r] = new Triangle((c*triHeight) + triHeight*((c+r)%2), r*(triBase/2.0), int(pow(-1, r+c)));  //alternates right-facing and left-facing triangles down each column
  //beginRecord(SVG, "filename.svg");

void draw() { //called each frame; displays each triangle in the grid
  for (int i=0; i<triangles.length; i++) {

void mousePressed() { //called when mouse is pressed; calls updateColor to update the color of the pressed triangle
    for (int i=0; i<triangles.length; i++) {
      if(triangles[i].isInside(mouseX, mouseY)){

void keyPressed(){
  if (keyCode == 32){
  } else if (keyCode == 71){
    grid = !grid;
//  println(keyCode);

//class representing each triangle in the grid
class Triangle {
  float x, y, x2, y2, x3, y3; //coordinates of the 3 vertices
  int dir; //-1 or 1, corresponding to left-facing and right-facing triangles, respectively
  int curColorIndex; //index of the current triangle color in colors array
  boolean isColored; //false when triangle is in default uncolored state, true otherwise
  Triangle(float x, float y, int dir) { //constructor to initialize instance variables; calculates the other vertex positions based on the passed in first vertex (x, y) and dir
    this.x = x;
    this.y = y;
    this.dir = dir;
    isColored = false;
    curColorIndex = colors.length;
    y2 = y + (triBase/2.0);
    y3 = y - (triBase/2.0);
    x2 = x3 = x + (triHeight*dir);  
  void display() { //draws the triangle
    } else{
      if (grid == true){
      } else {
    triangle(x, y, x2, y2, x3, y3);
  boolean isInside(int ix, int iy){ //returns true if point (ix, iy) lies inside the triangle, false otherwise
    float dx = (ix - x)/dir;
    if(dx < triHeight && dx >= 0){ //if point is in the same column as the triangle
      float dy = abs(iy - y);
      float yInside = abs(dx / sqrt(3));
      if(dy <= yInside){ //if point is in the same triangle
        return true;
    return false;
  void updateColor(){ //Updates the current color index and isColored boolean, so the next frame is drawn with the correct color
    curColorIndex = (curColorIndex + 1) % (colors.length + 1); //curColorIndex cycles from 0 through colors.length, inclusive
    isColored = (curColorIndex < colors.length); //set isColored to false when curColorIndex is out of array bounds
<canvas> </canvas>