Question

So, according to this article, you can feature test for canvas blend modes using:

Modernizr.addTest('canvasblending', function () {
   if (Modernizr.canvas === false) return false;
   var ctx = document.createElement('canvas').getContext('2d');
   ctx.globalCompositeOperation = 'screen';
   return ctx.globalCompositeOperation == 'screen';
});

Which returns a false-positive in Chrome 27.

Was it helpful?

Solution

I wrote something that does work, but it's extremely ugly, but thought it could either help someone else or we could get something better. Here's what I came up with:

Modernizr.addTest('canvasblending', function() {
  if ( ! Modernizr.canvas ) return false;
  var canvas = document.createElement('canvas');
  canvas.width = 1;
  canvas.height = 1;
  var ctx = canvas.getContext('2d');
  ctx.fillStyle = '#000';
  ctx.fillRect(0,0,1,1);
  ctx.globalCompositeOperation = 'multiply';
  ctx.fillStyle = '#fff';
  ctx.fillRect(0,0,1,1);
  return ctx.getImageData(0,0,1,1).data[0] === 0;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top