canvas实现的红包照片自适应效果-创新互联
效果图
公司主营业务:网站设计制作、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出绥宁免费做网站回馈大家。html结构(index.html)
content=" height = device-height,
width = device-width,
initial-scale = 1.0,
minimum-scale = 1.0,
maximum-scale = 1.0,
user-scalable = no
" />
css样式(blur.css)
*{
margin: 0;
padding: 0;
}
#blur-div{
margin: 0 auto;
position: relative;
overflow: hidden;
}
#blur-image{
display: block;
margin: 0 auto;
filter:blur(20px);
-webkit-filter:blur(20px);
-moz-filter:blur(20px);
-ms-filter:blur(20px);
-o-filter:blur(20px);
position: absolute;
left: 0px;
top: 0px;
z-index: 0;
}
#blur-canvas{
display: block;
margin: 0 auto;
position: absolute;
left: 0px;
top: 0px;
z-index: 100;
}
.button{
display: block;
position: absolute;
z-index: 200;
width: 100px;
height: 30px;
color: white;
text-decoration: none;
text-align: center;
line-height: 30px;
border-radius: 5px;
}
#reset-button{
left: 50px;
bottom: 20px;
background-color: #058;
}
#reset-button:hover{
background-color: #047;
}
#show-button{
right: 100px;
bottom: 20px;
background-color: #085;
}
#show-button:hover{
background-color: #074;
}
js脚本(blur.js)
var canvasWidth = window.innerWidth;
var canvasHeight = window.innerHeight;
var canvasObj = document.getElementById("blur-canvas");
var contextObj = canvasObj.getContext("2d");
canvasObj.width = canvasWidth;
canvasObj.height = canvasHeight;
var imageObj = new Image();
var radius = 50;
var clippingRegion = {x:-1,y:-1,r:radius}
var leftMargin = 0,topMargin = 0;
imageObj.src = "20170122161717.png";
imageObj.onload = function(e){
$("#blur-div").css("width",canvasWidth+"px");
$("#blur-div").css("height",canvasHeight+"px");
$("#blur-image").css("width",imageObj.width+"px");
$("#blur-image").css("height",imageObj.height+"px");
leftMargin = (imageObj.width - canvasObj.width) / 2;
topMargin = (imageObj.height - canvasObj.height) / 2;
$("#blur-image").css("top",String(-topMargin)+"px");
$("#blur-image").css("left",String(-leftMargin)+"px");
initCanvas();
}
function initCanvas(){
var theleft = leftMargin < 0 ? -leftMargin:0;
var thetop = topMargin < 0 ? -topMargin:0;
var rx = Math.random()*(canvasObj.width - 2*radius-2*theleft)+radius+theleft;
var ry = Math.random()*(canvasObj.height - 2*radius-2*thetop)+radius+thetop;
clippingRegion = {x:rx,y:ry,r:radius};
drawImage(imageObj,clippingRegion);
}
function drawImage(imageObj,clippingRegion){
contextObj.clearRect(0,0,canvasObj.width,canvasObj.height);
contextObj.save();
setClippingRegion(clippingRegion);
// contextObj.drawImage(imageObj,0,0);
contextObj.drawImage(imageObj,Math.max(leftMargin,0),Math.max(topMargin,0),Math.min(canvasObj.width,imageObj.width),Math.min(canvasObj.height,imageObj.height),leftMargin < 0 ? -leftMargin:0,topMargin < 0 ? -topMargin:0,Math.min(canvasObj.width,imageObj.width),Math.min(canvasObj.height,imageObj.height));
contextObj.restore();
}
function setClippingRegion(clippingRegion){
contextObj.beginPath();
contextObj.arc(clippingRegion.x,clippingRegion.y,clippingRegion.r,0,Math.PI*2,false);
contextObj.clip();
}
function reset(){
initCanvas();
}
function show(){
document.getElementById("reset-button").style.display="none";
var animation = setInterval(function(){
clippingRegion.r += 20;
if (clippingRegion.r > 2*Math.max(canvasObj.width,canvasObj.height)) {
clearInterval(animation);
document.getElementById("reset-button").style.display="block";
}
drawImage(imageObj,clippingRegion);
},30);
}
canvasObj.addEventListener("touchstart",function(e){
e.preventDefault();
});
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
本文题目:canvas实现的红包照片自适应效果-创新互联
当前路径:http://azwzsj.com/article/diieii.html