一款html5 canvas實(shí)現(xiàn)的圖片玻璃碎片特效
來源:易賢網(wǎng) 閱讀:2849 次 日期:2014-11-20 10:46:06
溫馨提示:易賢網(wǎng)小編為您整理了“一款html5 canvas實(shí)現(xiàn)的圖片玻璃碎片特效”,方便廣大網(wǎng)友查閱!

html5 canvas實(shí)現(xiàn)圖片玻璃碎片特效,圖片以玻璃碎片的形式出現(xiàn)到界面中,然后似玻璃被打碎的效果漸消息,效果很不錯,喜歡的朋友可以參考下

今天要為大家?guī)硪豢頷tml5 canvas實(shí)現(xiàn)的圖片玻璃碎片特效。圖片以玻璃碎片的形式出現(xiàn)到界面中,然后似玻璃被打碎的效果漸消息。效果圖如下:

名單

源碼下載

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

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)頁制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:一款html5 canvas實(shí)現(xiàn)的圖片玻璃碎片特效
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)