美文网首页我爱编程
canvas实现红包照片的效果

canvas实现红包照片的效果

作者: 伯纳乌的追风少年 | 来源:发表于2018-04-08 07:55 被阅读0次
image.png

如图所示,照片被高斯模糊处理了,只留下一小块清晰区域,
当点击show按钮后,照片全部变为清晰,
点击reset按钮后,照片又变成高斯模糊的样子,每次点击reset的时候,产生的清晰区域的位置都不一样。
代码如下:
主文件:index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" name="viewport"
                content="height=device-height,
                                    width=device-width,
                                    initial-scale=1.0,
                                    minimum-scale=1.0,
                                    maximum-scale=1.0,
                                    user-scalable=no"/>
    <title>Canvas玩转红包照片</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

    <link rel="stylesheet" href="blur.css">
</head>
<body>
    <div id="blur-div">
        <img id="image" src="banner.jpg" alt="">    
        <canvas id="canvas"></canvas>
        <a href="javascript:reset()" class="button" id="reset-button">RESET</a>
        <a href="javascript:show()" class="button" id="show-button">SHOW</a>
    </div>
    
    <script>
        var canvasWidth=window.innerWidth;
        var canvasHeight=window.innerHeight;
        var canvas=document.getElementById("canvas")
        var context=canvas.getContext("2d")
        canvas.width=canvasWidth
        canvas.height=canvasHeight
        var radius=50;
        var theAnimation 
        var image=new Image()
        var clippingRegion={
            x:400,
            y:200,
            r:radius
        }

        var leftMargin=0;
        var topMargin=0;

        image.src='banner.jpg'
        image.onload=function(){
            $("#blur-div").css("width",canvasWidth+"px")
            $("#blur-div").css("height",canvasHeight+"px")

            $("#image").css("width",image.width+"px")
            $("#image").css("height",image.height+"px")
            leftMargin=(image.width-canvas.width)/2
            topMargin=(image.height-canvas.height)/2
            $("#image").css("top",String(-topMargin)+"px")
            $("#image").css("left",String(-leftMargin)+"px")
            initCanvas()
        }
        function initCanvas(){
            clippingRegion={
                x:radius+Math.random()*(canvas.width-2*radius),
                y:radius+Math.random()*(canvas.height-2*radius),
                r:radius
            }
            draw(image,clippingRegion)
        }
        function setClippingRegion(clippingRegion){
            context.beginPath()
            context.arc(clippingRegion.x,clippingRegion.y,clippingRegion.r,0,Math.PI*2,false)
            context.clip()//内容只在剪辑区域中显示
        }


        function draw(image,clippingRegion){
            context.clearRect(0,0,canvas.width,canvas.height)
            context.save()
            setClippingRegion(clippingRegion)
            context.drawImage(image,leftMargin,topMargin,canvas.width,canvas.height,0,0,canvas.width,canvas.height)
            context.restore()
        }

        function show(){
            
            theAnimation = setInterval(function(){
                    if(clippingRegion.r<=2*Math.max(canvas.width,canvas.height)){
                        clippingRegion.r+=10
                        draw(image,clippingRegion)
                    }else{
                        clearInterval(theAnimation)
                    }
                    
                },30)
        }

        function reset(){
            clearInterval(theAnimation)
            initCanvas()
        }

        canvas.addEventListener("touchstart",function(e){
            e.preventDefault()
        })


    </script>
</body>
</html>

样式文件:blur.css

*{
    margin:0;
    padding: 0;
}

#blur-div{
    margin:0 auto;
    position: relative;
    overflow: hidden;
}

#canvas{
    display: block;
    margin:0 auto;
    position: absolute;
    left:0px;
    top:0px;
    z-index: 100;
    /*background: #f00;*/
}

#image{
    display: block;
    margin:0 auto;
    -webkit-filter: blur(10px);
    -moz-filter: blur(10px);
    -ms-filter: blur(10px);
    -o-filter: blur(10px);
    filter: blur(10px);

    position: absolute;
    left: 0px;
    top: 0px;
    z-index: 0;
}

.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: 50px;
    bottom:20px;
    background-color: #085;
}

#show-button:hover{
    background-color: #074;
}






背景照片:banner.jpg

banner.jpg



项目目录:

相关文章

  • canvas实现红包照片的效果

    如图所示,照片被高斯模糊处理了,只留下一小块清晰区域,当点击show按钮后,照片全部变为清晰,点击reset按钮后...

  • 红包照片四不像(三)——canvas实现

    本篇介绍使用canvas实现红包照片的效果。在前一篇已经介绍了canvas处理图片的一些接口,接下来直接用这些接口...

  • 前端效果链接

    canvas canvas实现各种点线效果 Canvas设置width与height 的问题!

  • canvas中通过globalAlpha实现渐隐效果

    canvas 想要实现渐隐效果,首先要理解显示,隐藏效果的原理,在canvas中怎么实现显示或者隐藏的效果呢?ca...

  • 基于canvas画板原理(H5)

    一个简单的小事例,利用canvas实现画板效果。实现原理:鼠标事件+canvas效果图: 代码: 如有问题欢迎交流...

  • 2017-12-28

    使用canvas和js实现烟花的效果 效果如下:

  • 微信红包照片效果实现

    微信朋友圈最近推出红包照片功能,发出的照片带有模糊的效果,只有发红包才可以看到,不然只能随机的看到一些圆形视野范围...

  • canvas2-text

    canvas画板结合JS事件实现写字效果

  • canvas烟花锦集

    canvas可以实现不同动画效果,本文主要记录几种不同节日烟花效果实现。 原文链接 实现一 效果地址 html c...

  • 【个人提升】canvas气泡漂浮动画

    canvas气泡漂浮动画实现效果思路: 1.获取鼠标移动的x、y坐标 //鼠标滑动 canvas.onmousem...

网友评论

    本文标题:canvas实现红包照片的效果

    本文链接:https://www.haomeiwen.com/subject/mftphftx.html