html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下
! [: _' n& P& L今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下:
4 x E+ `3 j+ y4 r; P4 i& B' r2 f1 p
源码下载 , T! q9 h- y! P; l& j; u- j
html代码:
) ]: W! B" q2 J代码如下:
% S2 v3 h" } |1 J }$ r1 }1 Q2 N& W<img src=city_copy.jpg id=src_img class=hidden> 3 R* Y$ C0 w- A) K# k
<div id=container style=-webkit-perspective: 500px;>
4 Z: z g8 c7 E8 w<div> 3 Q3 p' t! R# M% z
<script src=delaunay.js></script> ! p+ S" ~; o* k! M T
<script src=tweenmax.min.js></script> 9 a5 p5 e0 J* p$ \7 [
js代码:
# J m7 |& I) j4 v, ^代码如下:$ s7 z; D6 u; U) W
// canvas settings g) v) S$ e8 P
var imagewidth = 768,
4 o$ \# j; m/ B8 z- r' U dimageheight = 485;
3 y/ j! |$ i4 w2 nvar vertices = [],
, j; m' B8 N. g/ cindices, 1 v' ]& G* g+ b3 N% }' i
boxes = [];
% Y v: W, x/ T7 jvar image, 0 m; c+ E- q( c) k
fragments = [],
. A" c$ b. U3 R' m- g& {& _container = document.getelementbyid('container');
% ^& _ k# M- A$ c& e' ^window.onload = function () {
4 D8 y t% V7 C$ m" y3 N$ |) oimage = document.getelementbyid('src_img');
# O9 y/ S; k- E3 T+ J+ `7 I1 dtriangulate(); ) j) v# X9 \4 ^8 W
makeboxes();
- L2 j+ ^1 Q7 l" o" U8 j9 qmakefragments(); ) Z& K w( P4 [' s% v
};
" X1 p& l& F- J- ?7 r, }& Z' c6 lfunction triangulate() { 6 D- b" }* C5 \4 ]" l0 c" v q1 p
var x,
8 _! h9 C7 Q9 |( L0 x6 _4 h1 |y, . K7 M f3 l# F6 y
dx = imagewidth / 8, " f- W. Z4 F. {0 a. K5 `* o' u
dy = imageheight / 8,
4 A$ K* d6 a" p- g' Woffset = 0.5; 6 v5 R# m# X. w
for (var i = 0; i <= imagewidth; i += dx) { # U5 k' U; \! g( x
for (var j = 0; j <= imageheight; j += dy) { 9 J! D* A, v& a; L, a2 p
if (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset); ' s3 D/ Z' H* P* m+ V) y
else x = i; , `4 l% i3 K6 P i& _7 ^+ h
if (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset);
2 ^/ |" h( L" Z5 j& {else y = j; 6 M( z$ B w9 K- o. Q
vertices.push([x, y]);
1 M1 k; [9 A( T: b% _}
2 b& E0 b: Z- v: b, ^} ' @8 s2 J+ @! r, x( A
indices = delaunay.triangulate(vertices); 1 `2 u9 c3 i X" u! v( c: }5 S
} & g. S+ l! C. A$ q
function makeboxes() { $ h* B& i: _/ x: |, v" }4 P
var p0, p1, p2,
- N' W/ s9 Q. k) x" oxmin, xmax,
& G) C! m) N( N d4 Xymin, ymax; ; f# n, ]+ x& }8 Y# J; o" A
for (var i = 0; i < indices.length; i += 3) {
3 ]3 n) e2 `9 jp0 = vertices[indices[i + 0]];
5 g: ?2 X0 ~& Y w" Z. ^: n! N) tp1 = vertices[indices[i + 1]]; 4 C+ ^3 r% n4 ^% m
p2 = vertices[indices[i + 2]]; ! _$ X0 t x/ ~
xmin = math.min(p0[0], p1[0], p2[0]); - _ F1 J v% t& z0 A$ a
xmax = math.max(p0[0], p1[0], p2[0]);
, g# X0 Z# W/ j# ]ymin = math.min(p0[1], p1[1], p2[1]);
* G% \- g5 b+ v5 Rymax = math.max(p0[1], p1[1], p2[1]);
/ P/ s H7 ~- [boxes.push({
& u. F- R0 u7 Y3 O8 U7 xx: xmin,
^( i- Y% a0 zy: ymin,
2 D4 [& W3 ~9 s0 Q! ^, Tw: xmax - xmin, 8 e a' y4 s& G: l/ A8 N/ X
h: ymax - ymin
* ^5 X0 K. d% ?8 ^7 H});
5 x/ Z" ?4 d* q4 |}
^9 L6 ?1 S: `: b} Q! A( k8 ~- N2 K
function makefragments() {
# L) _" F" S/ M4 M* Hvar p0, p1, p2,
# j& c5 @! `3 J+ ebox,
2 c! E/ p3 `; u' kfragment;
5 M$ j9 _/ r/ D" {& v- ~tweenmax.set(container, { perspective: 500 }); 7 L& u1 A+ } f% d/ o+ o
var tl0 = new timelinemax({ repeat: -1 }); K& \; [ @+ v, y2 Z$ L8 B+ w
for (var i = 0; i < indices.length; i += 3) {
s5 S) F7 x6 T) R4 Fp0 = vertices[indices[i + 0]];
% p. B4 [+ w. i, e% O( `- vp1 = vertices[indices[i + 1]]; 5 @( L7 Y- X' ^( n
p2 = vertices[indices[i + 2]];
$ }$ L$ F( _. l9 H4 u7 \1 b2 f& bbox = boxes[i / 3];
) R3 o; s, D# b% @8 \* }0 cfragment = new fragment(p0, p1, p2, box);
2 \1 x5 Z* U# w! i! S! ivar rx = randomrange(30, 60) * ((i % 2) ? 1 : -1); H: `6 Z# I0 u+ v/ ]8 e
var ry = randomrange(30, 60) * ((i % 2) ? -1 : 1); 2 C: b$ J* f1 ]4 |- b
var tl1 = new timelinemax();
O" w3 W( u6 H( K. v" Atweenmax.set(fragment.canvas, { 9 @8 U. ^$ P! d( w* c$ |! ]& c; q0 p
y: box.y - 1000 ! }% x- U* }# E6 o
});
* M) i: h. S. W1 ctl1.to(fragment.canvas, randomrange(0.9, 1.1), {
- E8 q- C/ A [ }! @y: box.y, ' L! ]+ L9 h7 q9 l2 Q" P
ease: back.easeout 6 }9 f) t' H) R8 D- T t
});
+ B7 P7 M8 t2 q" H5 Htl1.to(fragment.canvas, 0.5, { ) e/ L- N0 r- r3 j6 U% ?
z: -100, X9 r' O# k( @' O9 d3 M6 j; ^
ease: cubic.easein,
7 f/ @; v, E3 ]delay: 0.4 / P7 S% t/ J& k6 j( |- C
});
{( ?. s5 z: ltl1.to(fragment.canvas, randomrange(1, 1.2), { 5 I" l- j3 H0 H. g! A$ v( Y! Q
rotationx: rx, 8 K2 f% a( L! Z$ ?9 s: p1 g
rotationy: ry,
. E1 u* N$ Y& Uz: 250, 8 _8 ]+ F7 X& P7 U3 u- v$ p
alpha: 0, 5 w6 j, w& z7 T/ J
ease: cubic.easeout 0 R% e4 L; z! {
});
+ C1 k/ G. _/ F: V5 Z) f- p( dtl0.insert(tl1); - w: ?) v5 B' T% k8 }: p& x
fragments.push(fragment);
5 P5 @+ u1 b) V9 Z5 q. icontainer.appendchild(fragment.canvas); 1 G" P/ W* s3 V
} " E+ x4 B# m& l% g T: n. r
} - f. G$ B. b# K& P& v
function randomrange(min, max) {
/ ^& A9 q0 ?: `* O6 ?/ M! jreturn min + (max - min) * math.random(); |2 \$ M, x5 G) K4 G& L- S
}
2 P! O' t6 ? ^7 T: ^* y: efragment = function (v0, v1, v2, box) { # @% n. O$ \9 k2 |
this.v0 = v0; ( K5 X7 e* s2 b% @
this.v1 = v1;
' v7 @1 y; u- o& k6 q8 w- w( ethis.v2 = v2; ) w1 ]% _! _; p) ^% b' j
this.box = box;
4 y F$ k6 i7 v9 H, Pthis.canvas = document.createelement('canvas');
! |" r' D' V9 C$ nthis.canvas.width = this.box.w;
8 v% ]5 J' U7 X, Nthis.canvas.height = this.box.h; $ s4 F, q1 j' J
this.canvas.style.width = this.box.w + 'px'; ) k2 Z- B7 w2 w& W0 ~
this.canvas.style.height = this.box.h + 'px';
$ b3 m/ P4 l& A: [& Athis.ctx = this.canvas.getcontext('2d');
9 Q. P( a w1 I7 `, z& J b! W' ^tweenmax.set(this.canvas, {
" Z6 L( [' \9 ?) rx: this.box.x,
* V5 s, m: G% Py: this.box.y
9 C4 w4 h ]5 Z% @: P- E});
/ k4 k' M. Z5 X9 p Tthis.ctx.translate(-this.box.x, -this.box.y);
) p4 N- S1 L4 q! G$ hthis.ctx.beginpath(); 5 g3 R6 Q6 u7 x, i7 H6 W3 C) M: v/ |
this.ctx.moveto(this.v0[0], this.v0[1]);
6 m R9 d" `# C* Pthis.ctx.lineto(this.v1[0], this.v1[1]);
% c' M7 T6 I1 J, Y0 L/ V8 C; ]this.ctx.lineto(this.v2[0], this.v2[1]); 1 m5 ?8 R/ j) A7 s; w
this.ctx.closepath(); 0 n! e0 l2 K1 h4 h
this.ctx.clip();
0 z ~7 k9 Y# Dthis.ctx.drawimage(image, 0, 0);
$ h2 ?& X& u2 x! C& o, T" N}; //@ sourceurl=pen.js 3 j* M3 u# D( A; U
* e0 F }0 Q8 z8 t& N2 A
更多网页制作信息请查看: 网页制作 |
|