html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下! U) [8 V% X" o( n, N
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下: ! g! `/ r7 x$ X$ H$ T) [
9 B: A; O; q5 L7 T V源码下载
9 s5 O$ E- d" V/ N; I( nhtml代码: " y0 K8 k' t2 Y5 T8 y; b6 G8 Z
代码如下:$ ^2 S& Y) h( {' n$ X
<img src=city_copy.jpg id=src_img class=hidden> % Z0 b9 [' L$ W
<div id=container style=-webkit-perspective: 500px;> O7 O! f" | ~$ i4 J
<div>
7 q' j5 ?/ l# y4 K3 D<script src=delaunay.js></script> 3 n* O: J# F* G8 ?2 d
<script src=tweenmax.min.js></script> " }3 F) Z1 E6 {
js代码: B# Y; x7 g: o- T+ G, h. Q4 z! {5 K
代码如下:
) D0 l( g. [/ \% p1 B' k// canvas settings ' U- }& b2 h" c" B! h( Y5 q9 f
var imagewidth = 768,
2 U+ q. A0 Z9 {3 Q+ J: Y& ?imageheight = 485; 5 E! |; t. a9 g. r5 J, @: Q0 Q
var vertices = [],
/ g! g6 P8 F7 E5 L% e* A eindices, 3 h+ h; H, _: g: n2 r0 H6 a5 [5 d- G
boxes = []; / b3 T" R% x& a$ J8 `/ B
var image,
6 J+ o+ n% ~( u* L. Cfragments = [],
2 V; G& Q! y# [+ }& I' I( ccontainer = document.getelementbyid('container');
% l0 S- W8 V' s( Y6 Lwindow.onload = function () {
% V, p# n* A! ^2 Y. |! H4 U1 H5 Aimage = document.getelementbyid('src_img');
2 N7 Q8 o% d# { htriangulate();
" C) q% e5 D Amakeboxes(); ( ?* X% t! s0 G
makefragments(); & V! k- F' W7 i( ], ?
}; , O5 s. }; w. q0 b) p6 C$ c
function triangulate() { ) m/ i* k1 `1 X7 S0 e7 N
var x, $ Y5 P. j o$ {$ _5 r/ a
y, ! p* C3 M- s4 _$ `2 |3 J( r, I
dx = imagewidth / 8,
' G3 D; j2 p9 idy = imageheight / 8, 2 ]- H2 e/ l1 Z1 w5 Y' u
offset = 0.5; / [4 Z* H" I! m
for (var i = 0; i <= imagewidth; i += dx) {
" V9 ?$ y4 _8 b. c) V% Wfor (var j = 0; j <= imageheight; j += dy) {
, ^, [ I5 Z- d: k) `- x6 f' Cif (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset); ) l, X1 E% P) k/ ]7 v0 T
else x = i;
9 p; H( n1 r; v# kif (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset); ! ?2 b' u& ~- l; g8 A( T1 ^
else y = j; ' }- p7 l* \7 J* N8 u1 ]
vertices.push([x, y]); 1 b: D0 J' }7 c8 F
} # G h' `, J4 h. B
}
+ f' o8 }/ N! ~, uindices = delaunay.triangulate(vertices);
% c/ D8 O2 ^; \3 h( [& I}
) j+ D3 I; r. H; B& Yfunction makeboxes() {
$ Z9 l, v; u9 d4 vvar p0, p1, p2,
5 j2 u9 ~6 \. ], g* A6 m3 Zxmin, xmax, * p& [0 @ f8 v Q3 n3 V2 a
ymin, ymax;
4 W# Z/ x# {( A4 b9 ?+ Xfor (var i = 0; i < indices.length; i += 3) { 9 Q5 e' q: b! `
p0 = vertices[indices[i + 0]]; ' B: W; H5 t+ K1 q9 q; @/ ~
p1 = vertices[indices[i + 1]];
- o5 d" d! q+ T; g/ hp2 = vertices[indices[i + 2]];
. k+ g1 f1 Z4 I% ixmin = math.min(p0[0], p1[0], p2[0]); # k! \6 W+ b8 y: v/ s
xmax = math.max(p0[0], p1[0], p2[0]); + h2 ^ \( C! }; l9 u
ymin = math.min(p0[1], p1[1], p2[1]); 8 _: h/ s1 r# B& z6 [: ~% U
ymax = math.max(p0[1], p1[1], p2[1]); ! ]9 ^5 h' C( D1 \
boxes.push({
1 ^2 x* B2 ~$ |x: xmin,
+ @8 l: n+ }% i& A" c1 dy: ymin, , ?1 w5 `$ _. _4 u/ u9 p
w: xmax - xmin,
# W) t9 `) j, G# Q' g9 Ah: ymax - ymin 5 {! p1 r( e+ {
});
+ u. t# ^, A1 ^7 y) u" m+ B/ i2 }} $ c& y6 i1 I/ W
} " W' A1 c; k ~ L; w. x- F; T1 I
function makefragments() {
4 k7 {- i2 O7 i( i1 Y tvar p0, p1, p2, 7 k4 O- \( L' z+ @$ M* [" M
box, 2 \) B2 G1 B$ M- w; f
fragment;
) b W/ i/ n& Rtweenmax.set(container, { perspective: 500 });
1 t% _. {$ h) d, fvar tl0 = new timelinemax({ repeat: -1 }); ( v3 S4 d6 [' ^/ W" U4 b
for (var i = 0; i < indices.length; i += 3) {
; n E% F( T+ {p0 = vertices[indices[i + 0]]; ! W2 a1 v: D! X5 j. g1 K
p1 = vertices[indices[i + 1]];
) a# m7 W$ @" e9 c" j+ C9 hp2 = vertices[indices[i + 2]]; / g" B9 [- f! L& V. D$ q5 Z
box = boxes[i / 3]; b. z* G+ w4 |* T! i
fragment = new fragment(p0, p1, p2, box);
6 |) ~/ H; i! ~( r) I5 avar rx = randomrange(30, 60) * ((i % 2) ? 1 : -1); 3 F" a- V, Z }7 W' R
var ry = randomrange(30, 60) * ((i % 2) ? -1 : 1); 2 H2 m+ j* q$ C Z8 B
var tl1 = new timelinemax();
C' I3 O U& A! l& Ctweenmax.set(fragment.canvas, {
2 U/ L% ?9 }" b% i) Q. ky: box.y - 1000 ) R1 i2 n8 D5 I( m3 a% d
});
r8 C% N7 Q! Mtl1.to(fragment.canvas, randomrange(0.9, 1.1), {
1 A2 P4 r! [# Z. G& q- t. Gy: box.y,
}5 @ j6 I% F# @6 G% gease: back.easeout & f4 W$ o+ Z$ s$ Z9 O1 _
});
- y# m' q" y8 d. N% W- h0 ztl1.to(fragment.canvas, 0.5, {
: d1 f% V9 Q; @4 X; T( A/ u$ m+ rz: -100,
/ e: Y9 n5 _ F' s7 O+ `ease: cubic.easein, 8 Z/ D0 u5 P H% f/ ~+ W/ D& R
delay: 0.4 , e2 w( S7 V% l/ c; ]( d, }. V* v; V
}); ' f6 x, z6 h0 b, d- h' }
tl1.to(fragment.canvas, randomrange(1, 1.2), {
6 {1 n; Q: f1 ^& `2 yrotationx: rx,
1 R: V, {" q9 B4 ^1 e: z! | Arotationy: ry,
" V$ @# z) G( {) k' ~8 z! q3 L1 tz: 250,
! f- Q& O: u- @ V% Halpha: 0, , y9 f! r* j) }- z3 {( }
ease: cubic.easeout
6 f, W, S1 U" }- W}); ( x1 @7 v2 f7 c4 O! b# U: ]
tl0.insert(tl1); ) M7 H0 q. ~/ g+ V1 Z
fragments.push(fragment);
& z* z8 p) I% \- c; d& c) V+ m4 bcontainer.appendchild(fragment.canvas); " _! n \9 j8 h3 y3 _3 A3 S
} ( r' ]! P' R: q0 j' d) \- G( p
} , i0 N, M8 A; r! H
function randomrange(min, max) {
E" T1 a8 R) zreturn min + (max - min) * math.random(); 5 k8 C. d. |4 @1 w: \2 P3 b! w; Y
} 7 }2 \; } M7 V$ T
fragment = function (v0, v1, v2, box) {
\2 Q) H1 t# |this.v0 = v0;
t& S& w: {9 m" A% tthis.v1 = v1; + T! A, `* w; K* l
this.v2 = v2; $ W, X/ ], @* j& t. ?2 U
this.box = box; $ T: N9 z! ^+ ]; l3 w* s7 _, k
this.canvas = document.createelement('canvas'); 7 d, r8 w0 ^7 N
this.canvas.width = this.box.w;
+ B0 l9 y% f) z# e9 J2 e0 N zthis.canvas.height = this.box.h; 0 F) h8 M/ @+ d0 k2 H6 R
this.canvas.style.width = this.box.w + 'px'; $ [* T8 a; q9 ~; j5 b' s
this.canvas.style.height = this.box.h + 'px';
2 k. Z/ P& C: m! R" m, dthis.ctx = this.canvas.getcontext('2d');
7 j) }: \7 T4 d# _! f8 ptweenmax.set(this.canvas, { 0 d( Y8 i+ P5 r! |3 d6 I
x: this.box.x,
. v0 \3 Y" W4 w ]- v. {, Dy: this.box.y 2 D/ P- [% c# T) F0 J) h5 S9 g
});
6 Y9 Y. g3 K4 H; ~8 p; m4 c" mthis.ctx.translate(-this.box.x, -this.box.y);
8 c' @% x+ m9 P L% }0 }3 L' dthis.ctx.beginpath();
; Z1 \! W. ]) V3 rthis.ctx.moveto(this.v0[0], this.v0[1]); 2 \0 X" z3 i7 I. S6 B
this.ctx.lineto(this.v1[0], this.v1[1]);
+ d) r, k- v+ g V3 Othis.ctx.lineto(this.v2[0], this.v2[1]);
0 i. j+ b- D$ c4 W, y/ P& O3 Othis.ctx.closepath();
R' L8 w( T" G* Nthis.ctx.clip(); ; n$ Y: c/ G6 Z ` y- L+ \# i/ w
this.ctx.drawimage(image, 0, 0); : a8 a# v9 e1 k5 e
}; //@ sourceurl=pen.js 2 X# T+ i) c# f+ s% W/ n
, T: z- o3 `- Q: z/ B
更多网页制作信息请查看: 网页制作 |
|