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

              
                <div id="paper-container"></div>
<span>drag & drop any number of images from your OS to the canvas</span>
<a target="_blank" href="https://www.jointjs.com">
  <img id="logo" src="https://assets.codepen.io/7589991/jointjs-logo.svg" width="200" height="50"></img>
</a>
              
            
!

CSS

              
                body {
  text-align: center;
  font-family: sans-serif;
}

#paper-container {
  position: absolute;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0;
  margin: 30px;
  border: 2px solid lightgray;

  &.drag-n-drop {
    border: 2px solid #6fc8a6;

    .joint-paper {
      background-color: #b3e2d0 !important;
    }
  }
}

#logo {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background-color: #ffffff;
  border: 1px solid #d3d3d3;
  padding: 5px;
  box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.3);
}

              
            
!

JS

              
                const { dia, shapes } = joint;

const paperContainer = document.getElementById("paper-container");

const graph = new dia.Graph({}, { cellNamespace: shapes });
const paper = new dia.Paper({
  model: graph,
  cellViewNamespace: shapes,
  width: "100%",
  height: "100%",
  gridSize: 20,
  async: true,
  sorting: dia.Paper.sorting.APPROX,
  background: { color: "#F3F7F6" }
});

paperContainer.appendChild(paper.el);

function dragoverHandler(evt) {
  evt.preventDefault();
}

function dragenterHandler(evt) {
  evt.preventDefault();
  paperContainer.classList.add("drag-n-drop");
}

function dragleaveHandler(evt) {
  evt.preventDefault();
  paperContainer.classList.remove("drag-n-drop");
}

async function dropHandler(evt) {
  evt.preventDefault();
  evt.stopPropagation();
  const files = Array.from(event.dataTransfer.files);
  const promises = files.map((file) => readImageFile(file));
  const result = await Promise.all(promises);
  const images = result.filter((image) => image !== null);
  const shift = 20;
  let { x, y } = paper.clientToLocalPoint(evt.clientX, evt.clientY);
  images.forEach((image, index) =>
    image.position(x - shift * index, y - shift * index)
  );
  graph.addCells(images);
  paperContainer.classList.remove("drag-n-drop");
}

function readImageFile(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.addEventListener(
      "load",
      () => {
        const img = new Image();
        img.onload = () => {
          const el = createImage(
            reader.result,
            img.naturalWidth / 4,
            img.naturalHeight / 4
          );
          resolve(el);
        };
        img.onerror = function () {
          resolve(null);
        };
        img.src = reader.result;
      },
      false
    );
    reader.readAsDataURL(file);
  });
}

function createImage(href, width, height) {
  return new joint.shapes.standard.BorderedImage({
    size: { width, height },
    attrs: {
      image: {
        href,
        preserveAspectRatio: "none"
      }
    }
  });
}

paper.el.addEventListener("dragover", dragoverHandler);
paper.el.addEventListener("dragenter", dragenterHandler);
paper.el.addEventListener("dragleave", dragleaveHandler);
paper.el.addEventListener("drop", dropHandler);

              
            
!
999px

Console