gpt4 book ai didi

css - Coderwall 用 Canvas 模糊背景效果

转载 作者:技术小花猫 更新时间:2023-10-29 11:31:59 25 4
gpt4 key购买 nike

coderwall.com 如何制作这种背景效果,他们拍摄一张小图像,对其进行模糊处理,然后将其调整为视口(viewport)的 100%。这是一个例子:https://coderwall.com/p/on5ojq

我试过:

<canvas class="blur" src="https://d3levm2kxut31z.cloudfront.net/assets/locations/Mexico-1c39f581666a50a97c5130e13837ff20.jpg" width="300" height="200"></canvas>

然后添加了下面的css:

.blur {
min-width: 100%;
min-height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: -2;
opacity: 0.7;
}

但它不起作用。

请参阅以下 jsFiddle http://jsfiddle.net/RDdbt/1/

最佳答案

试试这个:

var CanvasImage=function(e,t){
this.image=t,
this.element=e,
this.element.width=this.image.width,
this.element.height=this.image.height;
var n=navigator.userAgent.toLowerCase().indexOf("chrome")>-1,
r=navigator.appVersion.indexOf("Mac")>-1;
n&&r&&(this.element.width=Math.min(this.element.width,300),this.element.height=Math.min(this.element.height,200)),
this.context=this.element.getContext("2d"),
this.context.drawImage(this.image,0,0)
};

CanvasImage.prototype={
blur:function(e){
this.context.globalAlpha=.5;
for(var t=-e;t<=e;t+=2)
for(var n=-e;n<=e;n+=2)
this.context.drawImage(this.element,n,t),
n>=0&&t>=0&&this.context.drawImage(this.element,-(n-1),-(t-1));
this.context.globalAlpha=1
}
},

$(function(){
var image,canvasImage,canvas;
$(".blur").each(function(){
canvas=this,
image=new Image,
image.onload=function(){
canvasImage=new CanvasImage(canvas,this),
canvasImage.blur(4)
},
image.src=$(this).attr("src");
});
});

http://jsfiddle.net/RDdbt/6/

关于css - Coderwall 用 Canvas 模糊背景效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13389868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com