Skip to content

Getting Started

Aaron Echler edited this page Feb 19, 2024 · 5 revisions

Getting Started

Template Code (Fixed Origin)

function setup() {
    createCanvas(400, 400);
    rectMode(CENTER);
    angleMode(DEGREES);
}
  
function draw() {
    background(0);
    translate(width/2, height/2);
    rotate(50);
    rect(width/2, height/2, 100, 50);
}
  

Dist function

  • Get the distance from a point.
function draw() {
    background(220);
    let distance = dist(mouseX, mouseY, width/2, height/2);
    translate(width/2, height/2);
   
    if(distance < distMouse){
        angle+=1;
    }
    rotate(angle);
    rect(0, 0, 100, 50);
}

Previous Mouse Position

  • You can access the previous mouse position compared to the current one.
function setup() {
  createCanvas(400, 400);
  background(255);
}

function draw() {
  stroke(0);
  if (mouseIsPressed) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}