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

              
                <table>
  <thead>
    <tr>
      <th>Interface</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <td>
      <button onclick="do_image_upload()">
        Upload Picture
      </button>
      <div class="imgcropper"></div>
      <button onclick="save_cropped_image()">Save Avatar</button>
    </td>
    <td>
      <img class="avatar">
      <button  onclick="clear_avatar()">Clear Avatar</button>
    </td>
  </tbody>
</table>

              
            
!

CSS

              
                table {
  width: 100%;
}
table, th, td {
  border: 1px solid #999;
  border-collapse: collapse;
}
th, td {
  padding: 5px;
  width: 50%;
}
th {
  background-color: #eee;
}
button {
  display: block;
  margin: 20px auto;
  padding: 10px 20px;
  min-width: 200px;
}
img.avatar {
  display: block;
  margin: 15px auto;
}

.imgcropper {
  width: 200px;
  margin: 10px auto;
}
              
            
!

JS

              
                window.onload = () => {
  initialize_crop_interface();
}

function initialize_crop_interface() {
  if (!window.myCropper) {
    // initialize CropInterface
    window.myCropper = new CropInterface(document.querySelector('.imgcropper'), 200, 200, 1);
  }
}

function do_image_upload() {
  get_file((content) => {
    // in case window isn't fully loaded yet
    initialize_crop_interface();
    window.myCropper.loadImage(content);
  });
}

function save_cropped_image() {
  // retrieve all the data from CropInterface
  let content = window.myCropper.getImageUrl();
  let dimensions = window.myCropper.getDimensions();
  let focus = window.myCropper.getFocus();
  let zoom = window.myCropper.getZoom();
  
  let imageScaler = new ImageScaler();
  imageScaler.loadFromDataURL(content).then(() => {
    imageScaler.setFocalPoint(focus.x, focus.y);
    imageScaler.setZoom(zoom);
    let crop = imageScaler.crop(dimensions.w, dimensions.h);
    let imageUrl = crop.toDataURL();
    save_avatar(imageUrl);
  });
}

function save_avatar(imageUrl) {
  // In a real implementation, imageURL
  // could be sent to the server to be
  // converted to a saved file
  let avatar = document.querySelector('.avatar');
  avatar.src = imageUrl;
}

function clear_avatar() {
  let avatar = document.querySelector('.avatar');
  avatar.src = '';
}

// -=Helper Functions=-
function get_file(cb) {
  let input = document.createElement('input');
  input.type = 'file';
  input.accept = 'image/x-png,image/gif,image/jpeg';
  input.onchange = e => { 
     let file = e.target.files[0]; 
     let reader = new FileReader();
     reader.readAsDataURL(file);
     reader.onload = (e) => {
        cb(e.target.result);
     };
  };
  input.click();
}
              
            
!
999px

Console