delaunay实现图片塌陷过渡动画效果代码

代码语言:html

所属分类:幻灯片

代码描述:delaunay实现图片塌陷过渡动画效果代码

代码标签: 塌陷 过渡 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">



    <style>
        body {
            background-color: #000;
            margin: 0;
            overflow: hidden;
        }

        canvas {
            position: absolute;
            backface-visibility: hidden;
            -webkit-backface-visibility: hidden;
            -moz-backface-visibility: hidden;
            -ms-backface-visibility: hidden;
        }

        img {
            position: absolute;
            cursor: pointer;
        }

        #container {
            position: absolute;
            width: 768px;
            height: 485px;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
        }
    </style>



</head>

<body translate="no">
    <div id="container"></div>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/delaunay-fast.1.js"></script>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/TweenMax.min.js"></script>
    <script>
        // triangulation using https://github.com/ironwallaby/delaunay

        const TWO_PI = Math.PI * 2;

        var images = [],
        imageIndex = 0;

        var image,
        imageWidth = 768,
        imageHeight = 485;

        var vertices = [],
        indices = [],
        fragments = [];

        var container = document.getElementById('container');

        var clickPosition = [imageWidth * 0.5, imageHeight * 0.5];

        window.onload = function() {
            TweenMax.set(container, {
                perspective: 500
            });

            // images from reddit/r/wallpapers
            var urls = [

                '//repo.bfw.wiki/bfwrepo/image/5fc18ea484475.png',
                '//repo.bfw.wiki/bfwrepo/image/5fc18ed75be96.png',
                '//repo.bfw.wiki/bfwrepo/image/5fc18ea484475.png',
                '//repo.bfw.wiki/bfwrepo/image/5fc18ed75be96.png',
            ],
            image,
            loaded = 0;
            // very quick and dirty hack to load and display the first image asap
            images[0] = image = new Image();
            image.onload = function() {
                if (++loaded === 1) {
                    imagesLoaded();
                    for (var i = 1; i < 4; i++) {
                        images[i] = image = new Image();

                        image.src = urls[i];
                    }
                }
            };
            image.src = urls[0];
        };

        function imagesLoaded() {
            placeImage(false);
            triangulate();
            shatter();
        }

        function placeImage(transitionIn) {
            image = images[imageIndex];

            if (++imageIndex === images.length) imageIndex = 0;

            image.addEventListener('click', imageClickHandler);
            container.appendChild(image);

            if (transitionIn !== false) {
                TweenMax.fromTo(image, 0.75, {
                    y: -1000
                }, {
                    y: 0, ease: Back.easeOut
                });
            }
        }

        function imageClickHandler(event) {
            var box = image.getBoundingClientRect(),
            top = box.top,
            left = box.left;

            clickPosition[0] = event.clientX - left;
            clickPosition[1] = event.clientY - top;

            triangulate();
            shatter();
        }

        function triangulate() {
            var rings = [{
                r: 50,
                c: 12
            },
                {
                    r: 150,
                    c: 12
                },
                {
                    r: 300,
                    c: 12
                },
                {
                    r: 1200,
                    c: 12
                } // very large in case of corner clicks
            ],
            x,
            y,
            centerX = clickPosition[0],
            centerY = clickPosition[1];

            vertices.push([centerX, centerY]);

            rings.forEach(function(ring) {
                var radius = ring.r,
                count = ring.c,
                variance = radius * 0.25;

                for (var i = 0; i < count; i++) {
                    x = Math.cos((i / count) * TWO_PI) * radius + centerX + randomRange(-variance, variance);
                    y = Math.sin((i / count) * TWO_PI) * radius + centerY + randomRange(-v.........完整代码请登录后点击上方下载按钮下载查看

网友评论0