Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div id="canvasContainer" style="width:100vw;height:100vh"><canvas id="canvas" width="320" height="320"></canvas></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                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;
}

class GameApp extends App {
  constructor(div) {
    super(div);
    let [cx, cy] = [canvas.width*0.5, canvas.height*0.5];
    this.polygon = [cx, cy-120, cx+120, cy, cx, cy+120, cx-120, cy];
    this.px = cx;
    this.py = cy-20;
    this.grab = false;
    this.loop();
  }
  
  update(dt) {
    
  }
  
  draw(ctx) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    // Polygon
    ctx.strokeStyle = "blue";
    ctx.beginPath();
    ctx.moveTo(this.polygon[0], this.polygon[1]);
    for (let i=2; i < this.polygon.length; i+=2)
      ctx.lineTo(this.polygon[i], this.polygon[i+1]);
    ctx.closePath();
    ctx.stroke();
    
    // Vertices
    ctx.fillStyle = "blue";
    for (let i=0; i < this.polygon.length; i+=2) {
      ctx.beginPath();
      ctx.arc(this.polygon[i], this.polygon[i+1], 5, 0, 2 * Math.PI, false);
      ctx.fill();
    }
    
    ctx.strokeStyle = "green";
    ctx.beginPath();
    ctx.moveTo(0, this.py);
    ctx.lineTo(canvas.width, this.py);
    ctx.stroke();
    ctx.fillStyle = "red";
    ctx.beginPath();
    ctx.arc(this.px, this.py, 5, 0, 2 * Math.PI, false);
    ctx.fill();
    
    let inside = false;
    let x = this.px;
    let y = this.py;
    let [x1, y1] = [this.polygon[this.polygon.length-2], this.polygon[this.polygon.length-1]];
    let [x2, y2] = [this.polygon[0], this.polygon[1]];
    for (let i = 0; i<this.polygon.length; i+=2) {
      if ((y1 >= y) != (y2 >= y)) {
        let ix = (x2 - x1) * (y - y1) / (y2 - y1) + x1;
        ctx.fillStyle = x <= ix ? "purple" : "green";
        ctx.beginPath();
        ctx.arc(ix, y, 5, 0, 2 * Math.PI, false);
        ctx.fill();
        if (x <= ix)
          inside = !inside;
      }
      [x1, y1] = [x2, y2];
      [x2, y2] = [this.polygon[i+2], this.polygon[i+3]];
    }
    
    ctx.fillStyle = "black";
    ctx.fillText(inside ? "Inside" : "Outside", 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, 25)) {
      this.grabIndex = -1;
      return;
    }
    for (let i=0; i < this.polygon.length; i+=2) {
      if (pointInCircle(x, y, this.polygon[i], this.polygon[i+1], 25)) {
        this.grabIndex = i;
        return;
      }
    }
    let intersection = pointOnLine(x, y, this.polygon[this.polygon.length-2], this.polygon[this.polygon.length-1], this.polygon[0], this.polygon[1]);
    for (let i=0; i < this.polygon.length; i+=2) {
      if (intersection) {
        let [px, py] = intersection;
        console.log(`segment ${i} hit at ${px}, ${py}`);
        this.grabIndex = null;
        return;
      }
      intersection = pointOnLine(x, y, this.polygon[i], this.polygon[i+1], this.polygon[i+2], this.polygon[i+3]);
    }
    if (pointInPolygon(x, y, this.polygon)) {
      console.log(`polygon hit`);
    }
    
    this.grabIndex = null;
  }
  
  touchmove(x,y) {
    if (!this.grab)
      return;
    if (this.grabIndex == -1) {
      this.px = x;
      this.py = y;
    }
    else if (this.grabIndex != null) {
      this.polygon[this.grabIndex] = x;
      this.polygon[this.grabIndex+1] = y
    }
    this.dirty = true;
  }
  
  touchup(x, y) {
    this.grab = false;
  }
}

let app = new GameApp(document.getElementById("canvasContainer"));
              
            
!
999px

Console