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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <canvas id="canvas" width="320" height="240"></canvas>
<button id="btnShare">共有</button>

              
            
!

CSS

              
                
              
            
!

JS

              
                window.onload = () => {
  const canvas = document.getElementById("canvas");
  const context = canvas.getContext("2d");
  context.beginPath();
  context.rect(10, 10, 310, 230);
  context.fillStyle = "#0000FF";
  context.fill();
  context.closePath();
  
  document.getElementById("btnShare").onclick = () => {
    share();
  };
};

/**
 * 画像付きでWebページを共有する
 */
const share = () => {
  const canvas = document.getElementById("canvas");
  const dataURL = canvas.toDataURL("image/png");
  const blob = toBlob(dataURL);

  const imageFile = new File([blob], "image.png", {
    type: "image/png",
  });
  navigator.share({
    text: "共有テスト",
    url: "https://codepen.io/de_teiu_tkg/pen/dyWaaNP",
    files: [imageFile],
  }).then(() => {
    console.log("共有成功.");
  }).catch((error) => {
    console.log(error);
  });
};

/**
 * Base64形式の画像データをBlobに変換する
 * @param {String} base64 Base64形式の画像データ
 * @returns {Blob} Blob形式の画像データ
 */
const toBlob = (base64) => {
  const decodedData = atob(base64.replace(/^.*,/, ""));
  const buffers = new Uint8Array(decodedData.length);
  for (let i = 0; i < decodedData.length; i++) {
    buffers[i] = decodedData.charCodeAt(i);
  }
  try {
    const blob = new Blob([buffers.buffer], {
      type: "image/png",
    });
    return blob;
  } catch (e) {
    return null;
  }
};
              
            
!
999px

Console