css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <div id="canvasContainer" style="width:100vw;height:100vh"><canvas id="canvas" width="320" height="320"></canvas></div>
            
          
!
            
              class App {
  constructor(div) {
    this.div = div;
    this.canvas = document.getElementById("canvas");
    this.ctx = canvas.getContext('2d');
    this.dirty = true;
    this.prev = +new Date();
    this.prevPos = [0, 0];
    
    this.resize();
    
    window.addEventListener('resize', () => this.resize(), false);
    
    this.canvas.addEventListener("mousedown", (event) => {
      this.prevPos = App.getmousePos(event)
      this.touchdown(...this.prevPos);
      event.preventDefault();
    }); 
    this.canvas.addEventListener("mousemove", (event) => {
      let curPos = App.getmousePos(event);
      this.touchmove(...curPos, curPos[0]-this.prevPos[0], curPos[1]-this.prevPos[1]);
      this.prevPos = curPos;
      event.preventDefault();
    });
    this.canvas.addEventListener("mouseup", (event) => {
      this.touchup(...App.getmousePos(event));
      event.preventDefault();
    });
    this.canvas.addEventListener("touchstart", (event) => {
      this.prevPos = App.getmousePos(event)
      this.touchdown(...this.prevPos);
      event.preventDefault();
    });
    this.canvas.addEventListener("touchmove", (event) => {
      let curPos = App.getmousePos(event);
      this.touchmove(...curPos, curPos[0]-this.prevPos[0], curPos[1]-this.prevPos[1]);
      this.prevPos = curPos;
      event.preventDefault();
    });
    this.canvas.addEventListener("touchend", (event) => {
      this.touchup(...App.getmousePos(event));
      event.preventDefault();
    });
  }
  
  resize() {
    this.canvas.width  = this.div.clientWidth;
    this.canvas.height = this.div.clientHeight;
    this.dirty = true;
  }
  
  loop() {
    let now = +new Date();
    let dt = now - this.prev;
    this.prev = now;
    this.update(dt/1000)
    if (this.dirty)
      this.draw(this.ctx);
    window.requestAnimationFrame(() => this.loop());
  }
  
  update(dt) {}
  
  draw(ctx) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.rect(0, 0, 100, 100);
    ctx.fill();
  }
  
  touchdown(x, y) { console.log("down", x, y); }

  touchmove(x, y) {}

  touchup(x, y) {}

  static getmousePos(event) {
    if (event.changedTouches) {
        return [event.changedTouches[0].pageX, event.changedTouches[0].pageY];
    }
    else {
        var rect = event.target.getBoundingClientRect();
        return [event.clientX- rect.left, event.clientY - rect.top];
    }
  }
}

function pointInCircle(x, y, cx, cy, r) {
  let [vx, vy] = [x - cx, y - cy];
  return vx*vx+vy*vy < r*r;
}
  
function pointOnLine(x, y, p1x, p1y, p2x, p2y) {
  let [v1x, v1y] = [x   - p1x,   y - p1y];
  let [v2x, v2y] = [p2x - p1x, p2y - p1y];
  // Get scalar projection
  let t = (v1x * v2x + v1y * v2y) / (v2x * v2x + v2y * v2y);
  // Check if within boundaries
  if (t < 0 || t > 1)
    return false;
  // Calculate the projected point
  let [px, py] = [p1x + t * v2x, p1y + t * v2y];
  // Calculate position and check if close enough
  return pointInCircle(x, y, px, py, 5) ? [px, py] : false;
}
  
function pointInPolygon(x, y, points) {
  let [x1, y1] = [points[points.length-2], points[points.length-1]];
  let [x2, y2] = [points[0], points[1]];
  let inside = false;
  // For every edge, check for an intersection
  for (let i = 0; i < points.length; i+=2) {
    if (((y1 >= y) != (y2 >= y)) && (x < (x2 - x1) * (y - y1) / (y2 - y1) + x1)) {
      inside = !inside;
    }
    [x1, y1] = [x2, y2];
    [x2, y2] = [points[i+2], points[i+3]];
  }
  return inside;
}

function projectPolygon(points, x, y, nx, ny) {
  let min = (points[0]-x)*nx+(points[1]-y)*ny;
  let max = min;
  let s;
  for (let i = 2; i < points.length; i+=2) {
    s = (points[i]-x)*nx+(points[i+1]-y)*ny;
    if (s < min) min = s;
    if (s > max) max = s;
  }
  return [min, max];
}

function polygonCollidesWith(points1, points2) {
  // We project both polygons on all normals of polygon 1
  let [x1, y1] = [points1[points1.length-2], points1[points1.length-1]];
  let [x2, y2] = [points1[0], points1[1]];
  let nx, ny, min1, max1, min2, max2;
  // For every edge of points1
  for (let i = 0; i < points1.length; i+=2) {
    // Get edge normal by rotating the vector [x2-x1, y2-y1] by 90 degrees
    [nx, ny] = [y2-y1, x1-x2];
    // Project both polygons onto the normal
    [min1, max1] = projectPolygon(points1, x2, y1, nx, ny);
    [min2, max2] = projectPolygon(points2, x2, y1, nx, ny);
    // If there is no overlap between the ranges, there is no collision
    if (max1 <= min2 || min1 >= max2)
      return false;
    [x1, y1] = [x2, y2];
    [x2, y2] = [points1[i+2], points1[i+3]];
  }
  return true;
}

function polygonsCollide(points1, points2) {
  return polygonCollidesWith(points1, points2) && polygonCollidesWith(points2, points1);
}

function polygonCollidesWithV(points1, points2) {
  // We project both polygons on all normals of polygon 1
  let [x1, y1] = [points1[points1.length-2], points1[points1.length-1]];
  let [x2, y2] = [points1[0], points1[1]];
  let nx, ny, min1, max1, min2, max2;
  let overlap, smallestOverlap, length, overlapX, overlapY;
  // For every edge of points1
  for (let i = 0; i < points1.length; i+=2) {
    // Get edge normal by rotating the vector [x2-x1, y2-y1] by 90 degrees
    [nx, ny] = [y2-y1, x1-x2];
    // Project both polygons onto the normal
    [min1, max1] = projectPolygon(points1, x2, y1, nx, ny);
    [min2, max2] = projectPolygon(points2, x2, y1, nx, ny);
    // If there is no overlap between the ranges, there is no collision
    if (max1 <= min2 || min1 >= max2)
      return false;
    overlap = Math.min(max1, max2) - Math.max(min1, min2);
    // Our scalar projection wasn't scaled yet
    length = nx*nx+ny*ny;
    overlap /= length;
    // And we need to take the length of the vector into account for the overlap length
    length = Math.sqrt(length);
    // Record smallest overlap
    if (!smallestOverlap || overlap * length < smallestOverlap) {
      smallestOverlap = overlap * length;
      overlapX = nx*overlap;
      overlapY = ny*overlap;
    }
    [x1, y1] = [x2, y2];
    [x2, y2] = [points1[i+2], points1[i+3]];
  }
  return [smallestOverlap, overlapX, overlapY];
}

function polygonsCollideV(points1, points2) {
  let overlap1 = polygonCollidesWithV(points1, points2);
  let overlap2 = polygonCollidesWithV(points2, points1);
  if (overlap1 && overlap2) {
    let overlap = overlap1[0] < overlap2[0] ? overlap1 : overlap2;
    return overlap;
  }
  else
    return false;
}

function projectCircle(cx, cy, radius, x, y, nx, ny) {
  let s = (cx-x)*nx + (cy-y)*ny;
  return [s - radius, s + radius];
}

function polygonCircleCollideV(points, cx, cy, radius) {
  // We project both polygons on all normals of polygon 1
  let [x1, y1] = [points[points.length-2], points[points.length-1]];
  let [x2, y2] = [points[0], points[1]];
  let nx, ny, min1, max1, min2, max2;
  let overlap, smallestOverlap, length, overlapX, overlapY;
  // For every edge of points
  for (let i = 0; i < points.length; i+=2) {
    // Get edge normal by rotating the vector [x2-x1, y2-y1] by 90 degrees
    [nx, ny] = [y2-y1, x1-x2];
    length = nx*nx+ny*ny;
    length = Math.sqrt(length);
    nx /= length;
    ny /= length;
    // Project both polygons onto the normal
    [min1, max1] = projectPolygon(points, x2, y1, nx, ny);
    [min2, max2] = projectCircle(cx, cy, radius, x2, y1, nx, ny);
    // If there is no overlap between the ranges, there is no collision
    if (max1 <= min2 || min1 >= max2)
      return false;
    overlap = Math.min(max1, max2) - Math.max(min1, min2);
    // Record smallest overlap
    if (!smallestOverlap || overlap < smallestOverlap) {
      smallestOverlap = overlap;
      overlapX = nx*overlap;
      overlapY = ny*overlap;
    }
    [x1, y1] = [x2, y2];
    [x2, y2] = [points[i+2], points[i+3]];
  }
  // For the axis from the circle center to the nearest point
  let index = 0;
  let nearest = cx*points[0]+cy*points[1];
  let distance;
  for (let i = 2; i < points.length; i+=2) {
    distance = cx*points[i]+cy*points[i+1];
    if (distance < nearest) {
      index = i;
      nearest = distance;
    }
  }
  let [px, py] = [points[index], points[index+1]];
  [nx, ny] = [cx-px, cy-py];
  length = nx*nx+ny*ny;
  length = Math.sqrt(length);
  nx /= length;
  ny /= length;
  // Project both polygons onto the normal
  [min1, max1] = projectPolygon(points, px, py, nx, ny);
  [min2, max2] = projectCircle(cx, cy, radius, px, py, nx, ny);
  // If there is no overlap between the ranges, there is no collision
  if (max1 <= min2 || min1 >= max2)
    return false;
  overlap = Math.min(max1, max2) - Math.max(min1, min2);
  // Record smallest overlap
  if (overlap < smallestOverlap) {
    smallestOverlap = overlap;
    overlapX = nx*overlap;
    overlapY = ny*overlap;
  }
  return [smallestOverlap, overlapX, overlapY];
}

function polygonCenter(points) {
  let [centerX, centerY] = [points[0], points[1]];
  for (let i=2; i < points.length; i+=2) {
    centerX += points[i];
    centerY += points[i+1];
  }
  centerX /= points.length / 2;
  centerY /= points.length / 2;
  return [centerX, centerY];
}

class GameApp extends App {
  constructor(div) {
    super(div);
    let [cx, cy] = [canvas.width*0.5, canvas.height*0.5];
    this.polygons = [[cx, cy-120, cx+120, cy, cx, cy+120, cx-120, cy]];
    this.px = cx;
    this.py = cy;
    this.radius = 40;
    this.grab = false;
    this.loop();
  }
  
  update(dt) {
    
  }
  
  draw(ctx) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    // Polygons
    ctx.strokeStyle = "blue";
    for (let points of this.polygons) {
      ctx.beginPath();
      ctx.moveTo(points[0], points[1]);
      for (let i=2; i < points.length; i+=2)
        ctx.lineTo(points[i], points[i+1]);
      ctx.closePath();
      ctx.stroke();
    }
    
    // Vertices
    ctx.fillStyle = "blue";
    for (let points of this.polygons) {
      for (let i=0; i < points.length; i+=2) {
        ctx.beginPath();
        ctx.arc(points[i], points[i+1], 5, 0, 2 * Math.PI, false);
        ctx.fill();
      }
    }
    
    // Circle
    ctx.beginPath();
    ctx.arc(this.px, this.py, this.radius, 0, 2 * Math.PI, false);
    ctx.stroke();
    
    ctx.fillStyle = "black";
    let overlap = polygonCircleCollideV(this.polygons[0], this.px, this.py, this.radius);
    if (overlap) {
      let [distance, vx, vy] = overlap;
      ctx.fillText(`Colliding with ${distance} pixels`, 10, 10);
      let [centerX, centerY] = polygonCenter(this.polygons[0]);
      let [px, py] = [this.px-centerX, this.py-centerY];
      if (vx*px+vy*py < 0) {
        vx = -vx;
        vy = -vy;
      }
      ctx.strokeStyle = "green";
      ctx.beginPath();
      ctx.moveTo(centerX, centerY);
      ctx.lineTo(centerX+px, centerY+py);
      ctx.stroke();
      ctx.strokeStyle = "red";
      ctx.beginPath();
      ctx.moveTo(this.px, this.py);
      ctx.lineTo(this.px+vx, this.py+vy);
      ctx.stroke();
    }
    else
      ctx.fillText("Not colliding", 10, 10);
    
    const url = "www.fromatogra.com";
    ctx.fillText(url, (canvas.width-ctx.measureText(url).width)*0.5, 10);
    
    this.dirty = false;
  }
  
  touchdown(x, y) {
    this.grab = true;
    if (pointInCircle(x, y, this.px, this.py, this.radius)) {
      this.grabInfo = [];
      return;
    }
    for (let points of this.polygons) {
      for (let i=0; i < points.length; i+=2) {
        if (pointInCircle(x, y, points[i], points[i+1], 25)) {
          this.grabInfo = [points, "vertex", i];
          return;
        }
      }
    }
    for (let points of this.polygons) {
      let segment = points.length-2;
      let intersection = pointOnLine(x, y, points[segment], points[segment+1], points[0], points[1]);
      for (let i=0; i < points.length; i+=2) {
        if (intersection) {
          let [px, py] = intersection;
          console.log(`segment ${segment} hit at ${px}, ${py}`);
          this.grabInfo = [points, "edge", segment];
          return;
        }
        segment = i;
        intersection = pointOnLine(x, y, points[i], points[i+1], points[i+2], points[i+3]);
      }
    }
    for (let points of this.polygons) {
      if (pointInPolygon(x, y, points)) {
        console.log(`polygon hit`);
        this.grabInfo = [points, "polygon"];
        return;
      }
    }
    
    this.grabInfo = null;
  }
  
  touchmove(x,y, dx, dy) {
    if (!this.grab)
      return;
    if (this.grabInfo != null) {
      if (this.grabInfo.length == 0) {
        this.px += dx;
        this.py += dy;
        this.dirty = true;
        return;
      }
      
      let [points, type, index] = this.grabInfo;
      if (type == "vertex") {
        points[index] += dx;
        points[index+1] += dy;
      }
      else if (type == "edge") {
        points[index] += dx;
        points[index+1] += dy;
        if (index+2 < points.length) {
          points[index+2] += dx;
          points[index+3] += dy;
        }
        else {
          points[0] += dx;
          points[1] += dy;
        }
      }
      else if (type = "polygon") {
        for (let i=0; i < points.length; i+=2) {
          points[i] += dx;
          points[i+1] += dy;
        }
      }
      this.dirty = true;
    }
  }
  
  touchup(x, y) {
    if (!this.grab)
      return;
    
    let overlap = polygonCircleCollideV(this.polygons[0], this.px, this.py, this.radius);
    if (overlap) {
      let [distance, vx, vy] = overlap;
      let [centerX, centerY] = polygonCenter(this.polygons[0]);
      let [px, py] = [this.px-centerX, this.py-centerY];
      if (vx*px+vy*py < 0) {
        vx = -vx;
        vy = -vy;
      }
      this.px += vx;
      this.py += vy;
      this.dirty = true;
    }
    
    this.grab = false;
  }
}

let app = new GameApp(document.getElementById("canvasContainer"));
            
          
!
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.
Loading ..................

Console