Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <canvas id="cv-cam"></canvas>
    <button class="js-return" disabled><svg height="256" viewBox="0 0 512 512" width="256" xmlns="http://www.w3.org/2000/svg"><path d="m292.497 168.968c-21.134 0-40.287 0-57.542 0v-103.574l-234.955 190.601 234.955 190.61v-112.21h21.578c95.305 0 227.772-2.396 237.359 100.701 47.955-112.688 7.194-266.128-201.395-266.128z" fill="#4b4b4b"/></svg></button>
    <button class="js-refresh" disabled><svg height="40" viewBox="0 0 512 512" width="256" xmlns="http://www.w3.org/2000/svg"><g fill="#4b4b4b"><path d="m403.925 108.102c-27.595-27.595-62.899-47.558-102.459-56.29l2.716-51.812-102.236 53.867-27.306 14.454-5.066 2.654 8.076 4.331 38.16 20.542 81.029 43.602 2.277-42.859c28.265 7.546 53.438 22.53 73.623 42.638 29.94 29.939 48.358 71.119 48.358 116.776 0 23.407-4.843 45.58-13.575 65.687l40.37 17.532c11.076-25.463 17.242-53.637 17.242-83.219.078-57.699-23.407-110.101-61.209-147.903z"/><path d="m296.256 416.151-81.101-43.612-2.272 42.869c-28.26-7.555-53.51-22.53-73.618-42.636-29.945-29.95-48.364-71.12-48.364-116.767 0-23.427 4.844-45.522 13.576-65.697l-40.37-17.531c-11.076 25.53-17.242 53.723-17.242 83.228 0 57.679 23.407 110.157 61.21 147.893 27.595 27.594 62.899 47.548 102.453 56.202l-2.716 51.9 102.169-53.878 27.455-14.454 4.988-2.643-7.999-4.332z"/></g></svg></button>
              
            
!

CSS

              
                button {
  width: 100px;
  height: 100px;
}
svg {
  width: 50px;
  height: 50px;
}

              
            
!

JS

              
                window.addEventListener('DOMContentLoaded', () => {
  function drew() {
    const cvs = document.getElementById('cv-cam');
    const ctx = cvs.getContext('2d');
    const refreshBtn = document.querySelector('.js-refresh');
    const returnBtn = document.querySelector('.js-return');
    const canvasProps = {
      width: 0,
      height: 0,
    };
    const drawRectProps = {
      x: 0,
      y: 0,
      width: 0,
      height: 0,
      style: '#000',
      lineWidth: 4,
    };
    const rectMoveProps = {
      id: 0,
      startX: 0,
      startY: 0,
      endX: 0,
      endY: 0,
      x: 0,
      y: 0,
      width: 0,
      height: 0
    }
    let canvasFlag = '';
    let i = 0;
    const result = {};
    const items = new Map();
    const retrunItems = new Map();

    function rectHit(x, y, w, h, point) {
      let positiveX = x < point.x && point.x < x + w;
      let positiveY = y < point.y && point.y < y + h;
      let negativeX = x > point.x && point.x > x + w;
      let negativeY = y > point.y && point.y > y + h;
      
      return (positiveX && positiveY) || (positiveX && negativeY) || (negativeX && positiveY) || (negativeX && negativeY);
    }

    function drawRect(e) {
      canvasFlag = 'rect'
      drawRectProps.x = e.offsetX;
      drawRectProps.y = e.offsetY;

      cvs.addEventListener('mousemove', getDrewRectPos, false);
    }
    
    function moveRect(e, id) {
      let item = items.get(id);
      canvasFlag = 'move'
      rectMoveProps.id = id;
      rectMoveProps.startX = e.offsetX;
      rectMoveProps.startY = e.offsetY;
      rectMoveProps.x = item.x;
      rectMoveProps.y = item.y;
      rectMoveProps.width = item.width;
      rectMoveProps.height = item.height;

      items.delete(rectMoveProps.id);
      cvs.addEventListener('mousemove', getMoveRectPos, false);
    }

    function getDrewRectStart(e) {
      e.preventDefault();
      e.stopPropagation();

      const point = {
        x: e.offsetX,
        y: e.offsetY
      };
      if(items.size) {
        for (let [key, item] of items) {
          result.truth = rectHit(item.x, item.y, item.width, item.height, point);
          if(result.truth) {
            result.id = key;
            break;
          }
        };

        if(result.truth) {
          moveRect(e, result.id)
        } else {
          drawRect(e);
        }
      } else {
        drawRect(e);
      }
    }
    
    function getDrewRectPos(e) {
      e.preventDefault();
      e.stopPropagation();

      drawRectProps.width = e.offsetX - drawRectProps.x;
      drawRectProps.height = e.offsetY - drawRectProps.y;

      ctx.clearRect(0, 0, canvasProps.width, canvasProps.height);
      if(items.size) {
        items.forEach(item => {
          ctx.strokeRect(item.x, item.y, item.width, item.height);
        })
      }
      ctx.strokeRect(drawRectProps.x, drawRectProps.y, drawRectProps.width, drawRectProps.height);
    }

    function getMoveRectPos(e) {
      e.preventDefault();
      e.stopPropagation();

      let moveX = e.offsetX - rectMoveProps.startX;
      let moveY = e.offsetY - rectMoveProps.startY;

      rectMoveProps.endX = rectMoveProps.x + moveX;
      rectMoveProps.endY = rectMoveProps.y + moveY;

      ctx.clearRect(0, 0, canvasProps.width, canvasProps.height);

      items.forEach(item => {
        ctx.strokeRect(item.x, item.y, item.width, item.height);
      })
      ctx.strokeRect(rectMoveProps.endX, rectMoveProps.endY, rectMoveProps.width, rectMoveProps.height);
    }

    function getDrewRectEnd(e) {
      e.preventDefault();
      e.stopPropagation();

      switch (canvasFlag) {
        case 'move':
          items.set(rectMoveProps.id, {
            x: rectMoveProps.endX,
            y: rectMoveProps.endY,
            width: rectMoveProps.width,
            height: rectMoveProps.height
          });

          cvs.removeEventListener('mousemove', getMoveRectPos, false);
          break;
        case 'rect':
          items.set(i, {
            x: drawRectProps.x,
            y: drawRectProps.y,
            width: drawRectProps.width,
            height: drawRectProps.height
          });
          i++;
          cvs.removeEventListener('mousemove', getDrewRectPos, false);
          break;
        default:
          break;
      }
      
      retrunItems.set(retrunItems.size, new Map(items));
      
      if(retrunItems.size) {
        returnBtn.disabled = false;
        refreshBtn.disabled = false;
      }
    }
    
    refreshBtn.addEventListener('click', () => {
      items.clear();
      ctx.clearRect(0, 0, canvasProps.width, canvasProps.height);
      retrunItems.set(retrunItems.size, new Map(items));
      refreshBtn.disabled = true;
    })

    returnBtn.addEventListener('click', () => {
      items.clear();
      if(retrunItems.size-1) {
        retrunItems.get(retrunItems.size - 2).forEach((item, key) => {
          items.set(key, item)
        })
        retrunItems.delete(retrunItems.size - 2);
        ctx.clearRect(0, 0, canvasProps.width, canvasProps.height);
        items.forEach(item => {
          ctx.strokeRect(item.x, item.y, item.width, item.height);
        })
      } else {
        retrunItems.clear();
        ctx.clearRect(0, 0, canvasProps.width, canvasProps.height);
        returnBtn.disabled = true;
        refreshBtn.disabled = true;
      }
    })

    canvasProps.width = window.innerWidth;
    canvasProps.height = window.innerHeight;
    cvs.width = canvasProps.width;
    cvs.height = canvasProps.height;
    ctx.strokeStyle = drawRectProps.style;
    ctx.lineWidth = drawRectProps.lineWidth;

    cvs.addEventListener('mousedown', getDrewRectStart, false);
    cvs.addEventListener('mouseup', getDrewRectEnd, false);
  }
  drew();
})

              
            
!
999px

Console