html5 canvas實現圖片玻璃片段特效

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   2014   

今天要為大家帶來一款html5 canvas實現的圖片玻璃片段特效。圖片以玻璃片段的形式出現到介面中,然後似玻璃被打碎的效果漸訊息。如下:

 

線上預覽   源碼下載

 

實現代碼:

html代碼:

  <img src="city_copy.jpg" id="src_img" class="hidden">    <div id="container" style="-webkit-perspective: 500px;">        <div>            <script src="delaunay.js"></script>            <script src="TweenMax.min.js"></script>

js代碼:

               // canvas settings                var imageWidth = 768,    imageHeight = 485;                var vertices = [],    indices,    boxes = [];                var image,    fragments = [],    container = document.getElementById(‘container‘);                window.onload = function () {                    image = document.getElementById(‘src_img‘);                    triangulate();                    makeBoxes();                    makeFragments();                };                function triangulate() {                    var x,        y,        dx = imageWidth / 8,        dy = imageHeight / 8,        offset = 0.5;                    for (var i = 0; i <= imageWidth; i += dx) {                        for (var j = 0; j <= imageHeight; j += dy) {                            if (i && (i !== imageWidth)) x = i + randomRange(-dx * offset, dx * offset);                            else x = i;                            if (j && (j !== imageHeight)) y = j + randomRange(-dy * offset, dy * offset);                            else y = j;                            vertices.push([x, y]);                        }                    }                    indices = Delaunay.triangulate(vertices);                }                function makeBoxes() {                    var p0, p1, p2,        xMin, xMax,        yMin, yMax;                    for (var i = 0; i < indices.length; i += 3) {                        p0 = vertices[indices[i + 0]];                        p1 = vertices[indices[i + 1]];                        p2 = vertices[indices[i + 2]];                        xMin = Math.min(p0[0], p1[0], p2[0]);                        xMax = Math.max(p0[0], p1[0], p2[0]);                        yMin = Math.min(p0[1], p1[1], p2[1]);                        yMax = Math.max(p0[1], p1[1], p2[1]);                        boxes.push({                            x: xMin,                            y: yMin,                            w: xMax - xMin,                            h: yMax - yMin                        });                    }                }                function makeFragments() {                    var p0, p1, p2,        box,        fragment;                    TweenMax.set(container, { perspective: 500 });                    var tl0 = new TimelineMax({ repeat: -1 });                    for (var i = 0; i < indices.length; i += 3) {                        p0 = vertices[indices[i + 0]];                        p1 = vertices[indices[i + 1]];                        p2 = vertices[indices[i + 2]];                        box = boxes[i / 3];                        fragment = new Fragment(p0, p1, p2, box);                        var rx = randomRange(30, 60) * ((i % 2) ? 1 : -1);                        var ry = randomRange(30, 60) * ((i % 2) ? -1 : 1);                        var tl1 = new TimelineMax();                        TweenMax.set(fragment.canvas, {                            y: box.y - 1000                        });                        tl1.to(fragment.canvas, randomRange(0.9, 1.1), {                            y: box.y,                            ease: Back.easeOut                        });                        tl1.to(fragment.canvas, 0.5, {                            z: -100,                            ease: Cubic.easeIn,                            delay: 0.4                        });                        tl1.to(fragment.canvas, randomRange(1, 1.2), {                            rotationX: rx,                            rotationY: ry,                            z: 250,                            alpha: 0,                            ease: Cubic.easeOut                        });                        tl0.insert(tl1);                        fragments.push(fragment);                        container.appendChild(fragment.canvas);                    }                }                function randomRange(min, max) {                    return min + (max - min) * Math.random();                }                Fragment = function (v0, v1, v2, box) {                    this.v0 = v0;                    this.v1 = v1;                    this.v2 = v2;                    this.box = box;                    this.canvas = document.createElement(‘canvas‘);                    this.canvas.width = this.box.w;                    this.canvas.height = this.box.h;                    this.canvas.style.width = this.box.w + ‘px‘;                    this.canvas.style.height = this.box.h + ‘px‘;                    this.ctx = this.canvas.getContext(‘2d‘);                    TweenMax.set(this.canvas, {                        x: this.box.x,                        y: this.box.y                    });                    this.ctx.translate(-this.box.x, -this.box.y);                    this.ctx.beginPath();                    this.ctx.moveTo(this.v0[0], this.v0[1]);                    this.ctx.lineTo(this.v1[0], this.v1[1]);                    this.ctx.lineTo(this.v2[0], this.v2[1]);                    this.ctx.closePath();                    this.ctx.clip();                    this.ctx.drawImage(image, 0, 0);                }; //@ sourceURL=pen.js
View Code

註:本文愛編程原創文章,轉載請註明原文地址:http://www.w2bc.com/Article/6399

html5 canvas實現圖片玻璃片段特效

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.