练习二:Canvas 滤镜示例

找张图片,保存为2.jpg,这里与代码文件在同目录下。

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        #container{
            width: 320px;
            height: 480px;
            margin: 48px auto;
            position: relative;
            background-color: red;
        }
        #container img {
            width: 100%;
            height: 100%;
            filter: blur(20px);
            z-index: 0;
            position: absolute;
            top: 0px;
            left: 0px;
        }
        #canvas {
            position: absolute;
            top: 0;
            left: 0;
            z-index: 100;
        }
        #bgp {
            text-align: center;
        }
        button {
            background-color: #078;
            height: 30px;
            line-height: 30px;
            color: #fff;
            border: none;
            width: 96px;
            cursor: pointer;
        }
        button:hover {
            background-color: red;
        }
    </style>
</head>
<body style="position: relative;">
    <div id="container">
            <!--将原始的图像模糊化  -->
            <img src="2.jpg">
            <!--  画布标签 :canvas,用于动画 -->
            <canvas id="canvas"></canvas>
    </div>
    <div id="bgp">          
        <button id="show">show</button>
        <button id="reset">reset</button>
    </div>

    <script type="text/javascript">
        var canvas = document.getElementById("canvas"),
        // canvas标签的DOM的getContext(contextID)方法返回一个用于在画布上绘图的环境,
        // 参数 contextID 指定了您想要在画布上绘制的类型。
        // 当前唯一的合法值是 "2d",它指定了二维绘图,并且导致这个方法返回一个环境对象,该对象导出一个二维绘图 API。
            ctx = canvas.getContext("2d"),
            canvasWidth = 320,
            canvasHeight = 480,
            radius = 35,
            clipRegion = {x: -1, y: -1, r: radius},
            show = document.getElementById("show"),
            resetButton = document.getElementById("reset"),
            img,
            timeId,
            max = Math.max(canvasWidth, canvasHeight) * 2;
        console.log("init arguments............")


        function drawClip() {
            // 清空子路径,一般用于开始路径的创建。在几次循环地创建路径的过程中,每次开始创建时都要调用beginPath函数。
            ctx.beginPath();

            ctx.arc(clipRegion.x, clipRegion.y, clipRegion.r, 0, Math.PI * 2, false);
            ctx.clip();
        }
        function draw() {
            // 清除以x,y为左上角,宽度为width,高度为height的矩形区域。
            ctx.clearRect(0,0,canvasWidth, canvasHeight);
            ctx.save();
            drawClip();
            // 把image图像绘制到画布上x,y坐标位置,图像的宽度是w,高度是h。
            ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
            ctx.restore();
            console.log("init canvas.............")
        }
        function setRandomPosition() {
            clipRegion.x = Math.random() * (canvasWidth - 2 * radius) + radius;
            clipRegion.y = Math.random() * (canvasHeight - 2* radius) + radius;
        }
        function initView() {
            canvas.width = canvasWidth;
            canvas.height = canvasHeight;
            setRandomPosition();
            img = new Image();
            img.src = "2.jpg";
            img.onload = function(e) {
                draw();
            }
        }
        initView();   // 初始化


        show.addEventListener("click", function() {
                showChange();    
        },false);
        resetButton.addEventListener("click", function() {
            reset();
        }, false);
        function showChange() {
            timeId = setTimeout(function change() {
                if(clipRegion.r > max) {
                    stopChange(timeId);
                } else {
                    clipRegion.r += 2;
                    draw(img, clipRegion);
                    timeId = setTimeout(change, 30);
                }
            }, 30);
        }
        function stopChange(timeId) {
            clearTimeout(timeId);
        }
        function reset() {
            stopChange(timeId);
            clipRegion.r = radius;
            setRandomPosition();
            draw();
        }
    </script>
</body>
</html>

 

posted on 2018-12-25 13:39  myworldworld  阅读(109)  评论(0)    收藏  举报

导航