123

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.

            
              html, body {
  margin: 0;
  padding: 0;
}

            
          
!
            
              var clicker;
var swiper;
var heinrich;
var isSwiping = false;

function setup() { 
  createCanvas(400, 500);
  clicker = new Clicker();
  swiper = new Swiper();
  heinrich = new Heinrich();
} 

function draw() { 
  background(50);
  clicker.show();
  if(isSwiping){
    swiper.show();
  }
  
  //heinrich display and walk
  heinrich.display();
  heinrich.walk();  
}

function mouseClicked(){
  if(isSwiping){
    swiper.isOver=true;
    
    //heinrich stitch
    heinrich.stitchAlongPath(swiper.turningPoints);
  }else{
    clicker.create();
    
    //heinrich go
    heinrich.go(mouseX, mouseY);
  }
}

function mouseDragged() {
  //check if mouse was moved far enough
  if(abs(mouseX-pmouseX)>1 || abs(mouseY-pmouseY)>1){
    //check if swiper is not already active or fading
    if(!swiper.isOver){ 
      swiper.evaluate();
    }
  }
  
  // prevent default
  return false;
}


//Clicker Object
Clicker = function(){
  this.clickArr=[];
  this.maxRadius = 140;
  this.strokeSize = 4;
  this.animSpeed = 3;
  this.colStart = color("white");
  this.colEnd = color(255,255,255,0);
  
  this.create = function(){
    //check for double click
    if(this.clickArr.length>0){
      if(dist(this.clickArr[this.clickArr.length-1].x, this.clickArr[this.clickArr.length-1].y, mouseX, mouseY)<10){
        this.clickArr[this.clickArr.length-1].isDoubleClick=true;
        
        //heinrich go
        heinrich.go(mouseX, mouseY, "dash");
        
        return;
      }
    }
    
    this.clickArr[this.clickArr.length] = {
      x: mouseX,
      y: mouseY,
      radius: 1,
      lerpCol: 0,
      isDoubleClick: false
    };
  };
  
  this.show = function(){
    noFill();
    strokeWeight(this.strokeSize);
    
    for(var i=0; i<this.clickArr.length; i++){  
      //visualize a click
      push();
      translate(this.clickArr[i].x, this.clickArr[i].y);
      
      this.clickArr[i].lerpCol = 1-(this.maxRadius-this.clickArr[i].radius)/this.maxRadius;
      stroke(lerpColor(this.colStart, this.colEnd, this.clickArr[i].lerpCol));
      strokeWeight(lerp(this.strokeSize, 1, this.clickArr[i].lerpCol));
      
      if(this.clickArr[i].isDoubleClick){
        //stroke("red");
        ellipse(0, 0, this.clickArr[i].radius*0.5, this.clickArr[i].radius*0.5);
        strokeWeight(10);
        this.clickArr[i].radius+=1*this.animSpeed;
        
      }
      
      
      ellipse(0, 0, this.clickArr[i].radius, this.clickArr[i].radius);
      
      pop();
      this.clickArr[i].radius+=1*this.animSpeed;
      
      if(this.clickArr[i].radius>this.maxRadius){
        //delete this click
        this.clickArr.splice(i,1);
      }
    }// ./loop
    
    noStroke();
    
  };// ./show func
  
  
};// ./Clicker obj

//Swiper Object
Swiper = function(){
  this.staticSwipePoints = [];
  this.turningPoints = [];
  this.startPos = createVector(0,0);
  this.changeDirDist = 30;
  this.strokeSize = 4;
  this.colStart = color("white");
  this.colEnd = color(255,255,255,0);
  this.colDebugLine = color(255,255,255,10);
  this.isOver=false;
  this.timer=0;
  this.timeLimit=0.5;
  
  this.evaluate = function(){
    if(!isSwiping){
      isSwiping = true;
      this.startPos = createVector(mouseX, mouseY);
      append(this.turningPoints, this.startPos);
    }
    
    //create a new point after a certain distance
    if(dist(this.startPos.x, this.startPos.y, mouseX, mouseY) > this.changeDirDist){
      append(this.staticSwipePoints, this.startPos);
      
      //CHECK if direction is turned around
      if(this.staticSwipePoints.length>2){
        this.max=this.staticSwipePoints.length-1;
        this.lastX=this.staticSwipePoints[this.max].x;
        this.lastY=this.staticSwipePoints[this.max].y;
          
        if(!this.invX && this.lastX<mouseX){
          append(this.turningPoints, createVector(this.lastX, this.lastY));
          this.invX=true;
        }
        if(this.invX && this.lastX>mouseX){
          append(this.turningPoints, createVector(this.lastX, this.lastY));
          this.invX=false;
        }
        if(!this.invY && this.lastY<mouseY){
          append(this.turningPoints, createVector(this.lastX, this.lastY)); 
          this.invY=true;
        }
        if(this.invY && this.lastY>mouseY){
          append(this.turningPoints, createVector(this.lastX, this.lastY));
          this.invY=false;
        }
      }
        
      //set new startpos
      this.startPos = createVector(mouseX, mouseY);
    }
  }
   
  this.show = function(){
    this.mX, this.mY;
    noFill();
    strokeWeight(this.strokeSize);
    ellipseMode(CENTER);
    stroke(this.colStart);
    
    if(this.isOver){
      //lerp col to end
      this.lerpValue=1-((this.timeLimit*60-this.timer)/(this.timeLimit*60));
      stroke(lerpColor(this.colStart, this.colEnd, this.lerpValue));
      strokeWeight(lerp(this.strokeSize, 1, this.lerpValue));

      //increase timer and add last mousePos to array
      this.timer++;
      if(!this.isLastMousePosAdded){
        append(this.turningPoints, createVector(mouseX, mouseY));
        this.isLastMousePosAdded=true;
      }
    }else{
      this.mX=mouseX;
      this.mY=mouseY;
    }
    
    //show main line
    if(this.turningPoints.length>0){
      for(var i=0; i<this.turningPoints.length-1; i++){
          //draw static lines
          this.x1=this.turningPoints[i].x;
          this.y1=this.turningPoints[i].y;
          this.x2=this.turningPoints[i+1].x;
          this.y2=this.turningPoints[i+1].y;
          line(this.x1, this.y1, this.x2, this.y2);
      }
      this.x1=this.turningPoints[this.turningPoints.length-1].x;
      this.y1=this.turningPoints[this.turningPoints.length-1].y;
      line(this.x1, this.y1, this.mX, this.mY);
    }
    
    //uncomment for debugging:
    //this.debugPointsBetween();
    
    if(this.timer>this.timeLimit*60){
      //clear points
      this.staticSwipePoints = [];
      this.turningPoints = [];
      this.isOver = false;
      this.isLastMousePosAdded = false;
      this.timer = 0;
      isSwiping = false;
    }
    
    noStroke();
  }
  
  this.debugPointsBetween = function(){
    //debug: show all the small in between pts and circles
    if(this.staticSwipePoints.length>0){
      stroke(this.colDebugLine);
      this.max = this.staticSwipePoints.length-1;
      
      for(var i=0; i<this.max; i++){
        //draw static lines
        this.x1=this.staticSwipePoints[i].x;
        this.y1=this.staticSwipePoints[i].y;
        this.x2=this.staticSwipePoints[i+1].x;
        this.y2=this.staticSwipePoints[i+1].y;
        line(this.x1, this.y1, this.x2, this.y2);
        
        ellipse(this.x1, this.y1, 20, 20);
        stroke(this.colDebugLine);
      }// ./loop  
      
      //draw connection to current line
      this.xLast = this.staticSwipePoints[this.max].x;
      this.yLast = this.staticSwipePoints[this.max].y;
      line(this.startPos.x, this.startPos.y, this.xLast, this.yLast);
      ellipse(this.xLast, this.yLast, 20, 20);
    }// ./if
    
    //draw dynamic line
    line(this.startPos.x, this.startPos.y, mouseX, mouseY);
    ellipse(this.startPos.x, this.startPos.y, 20, 20);
    

    noStroke();
  }

}// ./Swiper obj

//Heinrich Object
Heinrich = function(){
  this.x = 100;
  this.y = 100;
  this.goal = createVector(50,50);
  this.targetDistance;
  this.angleMouse;
  this.angleGoal;
  this.stitchPathArray = [];
  this.stitchPathNum = 0;
  this.isIdle;
  
  this.display = function(){
    fill("orange");
    rectMode(CENTER);
    
    push();
    translate(this.x,this.y);
    
    this.angleMouse = atan2(mouseY-this.y, mouseX-this.x)+PI/2;  
    this.angleGoal = atan2(this.goal.y-this.y, this.goal.x-this.x)+PI/2; 
    
    
    if(this.isIdle){
      //lerp from 0.1 to 1.0
      if(this.lerper>0.9){
        this.lerper=1;
      }else{
        this.lerper+=0.1;
      }
      //smoothly look at mouse again
      rotate(lerp(this.angleGoal, this.angleMouse, this.lerper));  
    }else{
      //keep looking at goal when walking
      rotate(this.angleGoal);
      this.lerper=0;
    }
    
    if(this.isDashing){
      rect(0, 0, 10, 35);
      rect(0, 0-10, 5, 35);
    }else{
      rect(0, 0, 30, 30);
      rect(0, 0-5, 10, 30);
    }
    
    pop();
  }
  
  this.go = function(x, y, moveType){
    if(moveType=="dash"){
      this.isDashing=true;
    }
    if(moveType=="stitch"){
      this.isStitching=true;
    }else{
      this.isStitching=false;
    }
    this.goal = createVector(x, y);
  }
   
  this.walk = function(){
    this.targetDistance=dist(this.x, this.y, this.goal.x, this.goal.y);
    if(this.targetDistance>10){
      this.isIdle=false;
      
      if(this.isDashing){
        this.x = lerp(this.goal.x, this.x, 0.9);
        this.y = lerp(this.goal.y, this.y, 0.9); 
      }else{
        //lerped speed
        this.x = lerp(this.goal.x, this.x, 0.95);
        this.y = lerp(this.goal.y, this.y, 0.95);
        
        //constant speed
        //this.x+=norm((this.goal.x-this.x), 0, this.targetDistance)*3.0;
        //this.y+=norm((this.goal.y-this.y), 0, this.targetDistance)*3.0;
      }  
    }else{
      if(this.isDashing){
        this.isDashing=false;
      }
      
      if(this.isStitching){
        if(this.stitchPathArray[this.stitchPathNum+1]){
          this.stitchPathNum++;
          this.go(this.stitchPathArray[this.stitchPathNum].x, this.stitchPathArray[this.stitchPathNum].y, "stitch");
        }else{
          this.isStitching=false;
        }// ./if pathNum+1 exists
      }else{
        //finished
        this.stitchPathArray = [];
        this.stitchPathNum = 0;
        
        //look at mouse
        this.isIdle=true;
      }// ./if stitching
      
    }
  }
  
  this.stitchAlongPath = function(stitchPathArray){
    this.stitchPathArray = stitchPathArray;
    this.stitchPathNum=0;
    this.go(this.stitchPathArray[0].x, this.stitchPathArray[0].y, "stitch");
  }
  
}
            
          
!
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.

Console