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

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

              
                <input type="file" id="add-image-input" accept="image/*" hidden />
<label id="add-image-button" for="add-image-input">Add image(s)</label>
<div id="stencil-container">
</div>
<div id="paper-container"></div>
<a target="_blank" href="https://www.jointjs.com">
  <img id="logo" src="https://assets.codepen.io/7589991/JOINT_JS_LOGO_SYMBOL_RGB-pdf.svg" width="200" height="50"></img>
</a>
              
            
!

CSS

              
                #stencil-container {
  position: absolute;
  left: 0;
  top: 30px;
  width: 120px;
  bottom: 0;

  .content {
    overflow-y: scroll;
  }
}

#add-image-button {
  width: 120px;
  height: 30px;
  line-height: 30px;
  position: absolute;
  top: 0;
  left: 0;
  background-color: #222;
  color: #fff;
  border: 2px solid lightgray;
  box-sizing: border-box;
  text-align: center;
  display: block;
  font-size: 12px;
  font-family: sans-serif;
  &:hover {
    background-color: #333;
  }
}

#paper-container {
  position: absolute;
  right: 0;
  top: 0;
  left: 120px;
  bottom: 0;
}

#logo {
  position: absolute;
  bottom: 20px;
  right: 0;
}

              
            
!

JS

              
                const { dia, ui, shapes } = joint;

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

document.getElementById("paper-container").appendChild(paper.el);

const stencil = new ui.Stencil({
  paper,
  usePaperGrid: true,
  width: 100,
  height: "100%",
  height: null,
  paperOptions: () => {
    return {
      model: new dia.Graph({}, { cellNamespace: shapes }),
      cellViewNamespace: shapes,
      background: {
        color: "#FCFCFC"
      }
    };
  },
  layout: {
    columns: 1,
    rowHeight: "compact",
    rowGap: 10,
    columnWidth: 100,
    marginY: 10,
    // reset defaults
    resizeToFit: false,
    dx: 0,
    dy: 0
  }
});

stencil.render();
document.getElementById("stencil-container").appendChild(stencil.el);

stencil.load([
  {
    type: "standard.BorderedImage",
    size: { width: 80, height: 80 },
    attrs: {
      image: { href: "https://picsum.photos/id/56/800/800" }
    }
  },
  {
    type: "standard.BorderedImage",
    size: { width: 80, height: 60 },
    attrs: {
      image: { href: "https://picsum.photos/id/314/800/600" }
    }
  }
]);

document
  .getElementById("add-image-input")
  .addEventListener("change", async () => {
    const files = Array.from(event.target.files);
    const promises = files.map((file) => readImageFile(file));
    const result = await Promise.all(promises);
    const images = result.filter((image) => image !== null);
    const stencilGraph = stencil.getGraph();
    stencilGraph.addCells(images);
    stencil.layoutGroup(stencilGraph);
    stencil.fitPaperToContent(stencil.getPaper());
  });

function readImageFile(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.addEventListener(
      "load",
      () => {
        const img = new Image();
        img.onload = () => {
          const { naturalWidth, naturalHeight } = img;
          const maxImageBBox = new g.Rect(0, 0, 80, 80);
          const scale = maxImageBBox.maxRectUniformScaleToFit(
            new g.Rect(0, 0, naturalWidth, naturalHeight),
            new g.Point(0, 0)
          );
          const el = createImage(
            reader.result,
            naturalWidth * scale,
            naturalHeight * scale
          );
          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.on("element:pointerclick", (elementView) => {
  const lightbox = new ui.Lightbox({
    image: elementView.model.attr("image/href"),
    downloadable: true
  });
  lightbox.open();
});

              
            
!
999px

Console