一个简单的canvas模糊背景图方法
blur(document.getElementById('logo'),'http://s.segmentfault.com/img/logo.png',2); function blur(element, src, strength){ var image = new Image(); image.onload = function(e){ var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); canvas.width = this.width; canvas.height = this.height; context.drawImage(this, 0, 0); context.globalAlpha = 0.5; // Higher alpha made it more smooth // Add blur layers by strength to x and y // 2 made it a bit faster without noticeable quality loss for (var y = -strength; y <= strength; y += 2) { for (var x = -strength; x <= strength; x += 2) { context.drawImage(canvas, x, y); } } context.globalAlpha = 1; element.style.backgroundImage = 'url('+canvas.toDataURL()+')'; } image.src = src; }
相关推荐
jinxiutong 2020-07-26
northwindx 2020-05-31
jinxiutong 2020-05-10
大地飞鸿 2020-11-12
星星有所不知 2020-10-12
MIKUScallion 2020-07-05
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25
MIKUScallion 2020-05-25
xdyangxiaoromg 2020-05-10
大地飞鸿 2020-05-06
northwindx 2020-04-25