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

Packages

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.

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

              
                - let coords = arr => arr.map(([x, y]) => ({x, y}))
- let pathDef = arr => 'M' + arr.map(({x, y}) => x + ' ' + y).join('L') + 'Z'
- let PATH = [A, B, C] = coords([
-   [0,-50], [-30, 40], [50, 50]
- ])
- let area = (a, b, c) => Math.abs((a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y))/2.)
- let roughlyEq = (a, b) => Math.abs(a-b) < 1e-11
- let inside = (P)=> roughlyEq(area(A,B,C), (area(P,B,C) + area(P,A,C) + area(P,A,B)))
- res = 16

svg(viewBox="-64 -64 128 128")  
  each pixel, idx in Array(res * res)
    - s = 128 / res
    - sh = s / 2
    - x = (idx % res) * s - 64 + sh
    - y = ((idx / res)|0) * s - 64 + sh
    - fill = inside({x,y}) ? '#f0f': '#000'
    rect(x=x - sh, y=y - sh,width=s,height=s, fill=fill, stroke='#444', vector-effect="non-scaling-stroke")
    
  g(fill="currentColor")
    circle(cx=A.x, cy=A.y, r=3)
    circle(cx=B.x, cy=B.y, r=3)
    circle(cx=C.x, cy=C.y, r=3)
    path(stroke="currentColor", fill="none", d=pathDef(PATH), vector-effect="non-scaling-stroke", stroke-width="1.5")
              
            
!

CSS

              
                body
  background #222
  color #fff
  margin 0
  height 100vh
  display flex
  justify-content center
  align-items center
  
svg
  touch-action none
  width 90vmin
  height 90vmin
  border 1px solid #fff
  
  circle
    cursor pointer
              
            
!

JS

              
                // super tiny SVG library :D
const coords = (arr) => arr.map(([x, y]) => ({ x, y }));
const pathDef = (arr) =>
  "M" + arr.map(({ x, y }) => x + " " + y).join("L") + "Z";
const area = (a, b, c) =>
  Math.abs((a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) / 2);
const roughlyEq = (a, b) => Math.abs(a - b) < 1e-11;
const inside = (P, A, B, C) =>
  roughlyEq(area(A, B, C), area(P, B, C) + area(P, A, C) + area(P, A, B));
const clamp = (x, minVal, maxVal) => Math.min(Math.max(x, minVal), maxVal);
const d = document;
const [$, $$] = ["", "All"].map((fn) => d["querySelector" + fn].bind(d));
////

const res = 16;
const $svg = $`svg`;
const $path = $`path`;
const $$pixels = $$`rect`;
const $$circles = ([$A, $B, $C] = [...$$("circle")]);

class Draggable {
  constructor(el, xProp = "cx", yProp = "cy", svg = null) {
    this.el = el;
    this.xProp = xProp;
    this.yProp = yProp;
    this.svg = svg || $("svg");
    this.subscribers = [];
  }

  init() {
    this.el.addEventListener("pointerup", this.onPointerUp);
    this.el.addEventListener("pointerdown", this.onPointerDown);
    this.el.addEventListener("pointermove", this.onPointerMove);
    return this;
  }

  subscribe(func) {
    this.subscribers.push(func);
    return this;
  }

  dispose() {
    this.el.removeEventListener("pointerup", this.onPointerUp);
    this.el.removeEventListener("pointerdown", this.onPointerDown);
    this.el.removeEventListener("pointermove", this.onPointerMove);
  }

  onPointerDown = (e) => {
    this.dragging = true;
    this.startX = e.clientX;
    this.startY = e.clientY;
    this.x0 = +this.el.getAttribute(this.xProp);
    this.y0 = +this.el.getAttribute(this.yProp);
    this.el.setPointerCapture(e.pointerId);
  };

  onPointerMove = (e) => {
    if (!this.dragging) {
      return;
    }
    const deltaX = e.clientX - this.startX;
    const deltaY = e.clientY - this.startY;
    const clientWidth = this.svg.clientWidth;
    const clientHeight = this.svg.clientHeight;
    const viewBoxWidth = this.svg.viewBox.baseVal.width;
    const viewBoxHeight = this.svg.viewBox.baseVal.height;
    const xMin = this.svg.viewBox.baseVal.x;
    const yMin = this.svg.viewBox.baseVal.y;
    const xMax = xMin + viewBoxWidth;
    const yMax = yMin + viewBoxHeight;
    const x1 = clamp(this.x0 + (deltaX * viewBoxWidth) / clientWidth, xMin, xMax);
    const y1 = clamp(this.y0 + (deltaY * viewBoxHeight) / clientHeight, yMin, yMax);
    this.el.setAttribute(this.xProp, x1);
    this.el.setAttribute(this.yProp, y1);
    for (const subscriber of this.subscribers) {
      if (typeof subscriber === "function") {
        subscriber(this.el);
      }
    }
  };

  onPointerUp = (e) => {
    this.el.releasePointerCapture(e.pointerId);
    this.dragging = false;
  };
}

// update the pixels and the path when dragging the circles
function update() {
  const points = ([A, B, C] = $$circles.map((c) => ({
    x: +c.getAttribute("cx"),
    y: +c.getAttribute("cy")
  })));
  $path.setAttribute("d", pathDef(points));
  for (let y = 0; y < res; y++) {
    for (let x = 0; x < res; x++) {
      const pxWidth = $svg.viewBox.baseVal.width / res;
      const pxHeight = $svg.viewBox.baseVal.height / res;
      const cx = $svg.viewBox.baseVal.x + x * pxWidth + pxWidth * 0.5;
      const cy = $svg.viewBox.baseVal.y + y * pxHeight + pxHeight * 0.5;
      const color = inside({ x: cx, y: cy }, A, B, C) ? "#f0f" : "#000";
      $$pixels[y * res + x].setAttribute("fill", color);
    }
  }
}

const dragA = new Draggable($A).subscribe(update).init();
const dragB = new Draggable($B).subscribe(update).init();
const dragC = new Draggable($C).subscribe(update).init();

              
            
!
999px

Console