Welcome to JS Bin
Load cached copy from
31
 
1
<img src="http://rem.io/rem.jpg" crossOrigin>
2
3
<script>
4
function circlify(source, width) {
5
  var canvas = document.createElement('canvas'),
6
      ctx = canvas.getContext('2d');
7
      
8
  var short = source.width < source.height ? source.width : source.height,
9
      x = (source.width - short) / 2,
10
      y = (source.height - short) / 2;
11
  canvas.width = canvas.height = width;  
12
   ctx.drawImage(source, x, y, short, short, 0, 0, width, width);
13
14
  // now cut out a circle using global composite op
15
  ctx.globalCompositeOperation = 'destination-in';
16
  
17
  ctx.beginPath();
18
  ctx.arc(width/2, width/2, width/2, 0, Math.PI * 2, true);
19
  ctx.closePath();
20
  ctx.fill();
21
  
22
  return ctx;
23
}
24
25
window.onload = function () {
26
  var img = document.querySelector('img');
27
  var smallImg = new Image();
28
  smallImg.src = circlify(img, 100).canvas.toDataURL('image/png');
29
  img.parentNode.replaceChild(smallImg, img);
30
};
31
</script>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x