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

              
                <canvas id="source-in"></canvas>
<canvas id="source-out"></canvas>
<canvas id="source-atop"></canvas>
<canvas id="source-over"></canvas>
<canvas id="destination-in"></canvas>
<canvas id="destination-out"></canvas>
<canvas id="destination-atop"></canvas>
<canvas id="destination-over"></canvas>
<canvas id="xor"></canvas>
<canvas id="copy"></canvas>
<canvas id="lighter"></canvas>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  overflow: auto;
}
canvas {
  float: left;
}
              
            
!

JS

              
                // Draw img to canvas like cover
function drawImageProp(ctx, img, x, y, w, h, offsetX, offsetY) {
    if (arguments.length === 2) {
        x = y = 0;
        w = ctx.canvas.width;
        h = ctx.canvas.height;
    }

    // default offset is center
    offsetX = typeof offsetX === "number" ? offsetX : 0.5;
    offsetY = typeof offsetY === "number" ? offsetY : 0.5;

    // keep bounds [0.0, 1.0]
    if (offsetX < 0) offsetX = 0;
    if (offsetY < 0) offsetY = 0;
    if (offsetX > 1) offsetX = 1;
    if (offsetY > 1) offsetY = 1;

    var iw = img.width,
        ih = img.height,
        r = Math.min(w / iw, h / ih),
        nw = iw * r,   // new prop. width
        nh = ih * r,   // new prop. height
        cx, cy, cw, ch, ar = 1;

    // decide which gap to fill    
    if (nw < w) ar = w / nw;                             
    if (Math.abs(ar - 1) < 1e-14 && nh < h) ar = h / nh;  // updated
    nw *= ar;
    nh *= ar;

    // calc source rectangle
    cw = iw / (nw / w);
    ch = ih / (nh / h);

    cx = (iw - cw) * offsetX;
    cy = (ih - ch) * offsetY;

    // make sure source rectangle is valid
    if (cx < 0) cx = 0;
    if (cy < 0) cy = 0;
    if (cw > iw) cw = iw;
    if (ch > ih) ch = ih;

    // fill image in dest. rectangle
    ctx.drawImage(img, cx, cy, cw, ch,  x, y, w, h);
}

function drawImgWithBlendMode(canvas, ctx, blend_mode){
  
    var bg_img = new Image();
    bg_img.crossOrigin = 'anonymous';
    bg_img.onload = function(img){
      drawImageProp(ctx, bg_img, 0, 0, canvas.width, canvas.width, canvas.width/2, canvas.height/2);
      ctx.globalCompositeOperation = blend_mode;
      ctx.font = '36px serif';
      ctx.fillStyle = '#12455e';
      ctx.fillText(blend_mode, 10, 50);
      
    }
    bg_img.src = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/172299/calcifer-demon.jpg';
}
var blend_modes = ['source-in', 'source-out', 'source-atop', 'source-over', 'destination-in', 'destination-out', 'destination-atop', 'destination-over', 'xor', 'copy', 'lighter'];

window.onload = function(){
  var canvases = document.querySelectorAll('canvas');
  for(var i = 0; i<canvases.length; i++){
    var current_canvas = canvases[i];
    current_canvas.width = window.innerWidth/4;
    current_canvas.height = window.innerHeight/3;
    var ctx = current_canvas.getContext('2d');
    drawImgWithBlendMode(current_canvas, ctx, blend_modes[i]);
  }
}
              
            
!
999px

Console