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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <div>
  <h3>mask base clip</h3>
  <canvas id="clip-cvs"></canvas>  
</div>

<div>
  <h3>mask base globalCompositeOperation</h3>
  <canvas id="gco-cvs"></canvas>
</div>

              
            
!

CSS

              
                
              
            
!

JS

              
                  async function clipMask({ img, WIDTH, HEIGHT, windowRect }) {
    const cvs = document.getElementById("clip-cvs");
    const ctx = cvs.getContext("2d");
    const { x, y, width, height } = windowRect;
    cvs.width = WIDTH;
    cvs.height = HEIGHT;
    ctx.drawImage(img, 0, 0, WIDTH, HEIGHT);
    ctx.save();
    ctx.fillStyle = "rgba(0,0,0,0.6)";
    ctx.fillRect(0, 0, WIDTH, HEIGHT);
    ctx.rect(x, y, width, height);
    ctx.clip();
    ctx.drawImage(img, 0, 0, WIDTH, HEIGHT);
    ctx.restore();
  }

  async function gCOMask({ img, WIDTH, HEIGHT, windowRect }) {
    const cvs = document.getElementById("gco-cvs");
    const ctx = cvs.getContext("2d");
    const { x, y, width, height } = windowRect;
    cvs.width = WIDTH;
    cvs.height = HEIGHT;
    ctx.save();
    ctx.fillStyle = "rgba(0, 0, 0, 0.6)";
    ctx.fillRect(0, 0, WIDTH, HEIGHT);//绘制全屏半透明蒙层
    ctx.globalCompositeOperation = "destination-out";
    ctx.fillStyle = "#000"; // 随便什么颜色都行
    ctx.fillRect(x, y, width, height);// 这次绘制的区域将会变透明
    ctx.restore();
    ctx.globalCompositeOperation = "destination-over";
    ctx.drawImage(img, 0, 0, WIDTH, HEIGHT);
  }

  !async function() {
    const imgUrl = "https://files.1card1.cn/mps/0/20191111/2bd92a35ff644b478c6becdf753fc43a.jpg";
    const windowRect = {
      x: 200, y: 150,
      width: 400,
      height: 200
    };
    const img = await loadImage(imgUrl);
    if (!img) return;

    const WIDTH = img.width / 2;
    const HEIGHT = img.height / 2;

    const options = {
      img, WIDTH, HEIGHT, windowRect
    };

    clipMask(options);
    gCOMask(options);
  }();

 function loadImage(src) {
    return new Promise(resolve => {
      const img = new Image();
      img.src = src;
      img.addEventListener("load", () => resolve(img));
      img.addEventListener("error", () => resolve(null));
    });
  }
              
            
!
999px

Console