html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下" O* c4 L$ ]$ H7 t
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下:
* I2 T! v( F _: X, a8 S6 F. P0 C5 L7 ?" K
0 h. W! w* W# M8 k* k7 @/ d& i源码下载 5 v* O) r6 a+ T V' s+ J
html代码: , r/ H6 D. n- y& X, t
代码如下:
; S- H! n6 ^8 h( }<img src=city_copy.jpg id=src_img class=hidden>
! \% @- A& A3 y; D<div id=container style=-webkit-perspective: 500px;> # b0 K3 N. F6 N- D y9 A
<div>
* W- S- i7 ^' S6 q/ H2 X2 g<script src=delaunay.js></script>
9 ?1 S X) E( }% ~, r% Q<script src=tweenmax.min.js></script> ; a4 Q" z, z& R N
js代码: r. y u! ^: T3 C2 t7 [5 z
代码如下:1 T0 D3 t' M1 M" o- }
// canvas settings % X4 V$ q( `/ y
var imagewidth = 768,
* M' K3 q0 M3 H' o5 rimageheight = 485;
( u% L4 l" B/ A0 K, [" v0 \3 m |var vertices = [],
+ s) t# M* ^8 U5 Pindices,
' N) S3 t: x+ v/ M5 N0 y& cboxes = [];
5 C6 ], a4 }- l, m- d0 zvar image,
+ _& v9 H! d H ]9 Dfragments = [], 9 c" r6 J: S/ Q' P9 g
container = document.getelementbyid('container');
. u- H# u6 P, v2 P& d+ zwindow.onload = function () {
/ j; `1 s: `" \- Aimage = document.getelementbyid('src_img'); 9 k( w0 ]- x' A+ F4 s" Y# @ b0 \
triangulate(); 9 _7 @' @1 L. ]
makeboxes();
+ y; s0 e) @( H5 O, Xmakefragments();
B; h/ s" w- f7 S};
0 |# j* a4 D$ c2 M; F* Q Gfunction triangulate() {
" V& X) w, J% R" `3 ?5 uvar x,
* Q% e, f/ N( w( o* L. sy, & W9 v9 s% Q( y2 i% U
dx = imagewidth / 8,
4 D5 L, R, T5 \ tdy = imageheight / 8,
5 f' z: E( X7 I" t, N% r, Qoffset = 0.5; ) k( ^+ ~2 J7 u0 |& E) X1 ]& C
for (var i = 0; i <= imagewidth; i += dx) { 3 s1 h( t& S1 i: Y. j( {9 C
for (var j = 0; j <= imageheight; j += dy) { 1 _; \) {; s( M+ X
if (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset); # C! W6 U2 i% C7 P* o% w5 n: I" B; J
else x = i; & S) n9 x! m, X+ {- i" h
if (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset); 5 K; n4 ^" ]+ u' Q
else y = j; 1 b& Y. ~5 i( K& w$ s
vertices.push([x, y]); : t6 H( H5 e) D9 \$ J& v
} % {; ?/ x% G' c% ^5 i
}
`) s. R, x b9 r) e: iindices = delaunay.triangulate(vertices);
9 X, C/ v9 O' ~% ~+ M" v5 y} ! D' [7 I& {9 S. @ y* Q
function makeboxes() {
3 G' @2 a* t0 [6 Ivar p0, p1, p2,
1 D# P: H/ z0 m0 Y! Kxmin, xmax, ) F B* P0 U, S/ z
ymin, ymax;
8 e/ @% e, Y @: ?% j+ mfor (var i = 0; i < indices.length; i += 3) { % K6 T" p. K% Q [3 q
p0 = vertices[indices[i + 0]]; ! D- ]* ]' X" t; ~/ k. \
p1 = vertices[indices[i + 1]]; 1 Y5 a/ R8 P% G
p2 = vertices[indices[i + 2]];
9 Z. D7 E5 f/ x1 s1 N) Q1 ~" [. oxmin = math.min(p0[0], p1[0], p2[0]); 6 R( v# Z1 O, v5 O
xmax = math.max(p0[0], p1[0], p2[0]);
& N4 s0 w6 b$ L1 Mymin = math.min(p0[1], p1[1], p2[1]); 6 |0 w8 g" Q0 I: x ` V
ymax = math.max(p0[1], p1[1], p2[1]);
" R9 n) |- ~, ?, Tboxes.push({ ! W; t, u6 U9 l0 {5 P
x: xmin,
: f% ^+ }+ v+ }% v: h8 u+ z" q7 L/ my: ymin, : S0 \ e" ?4 Q# ~
w: xmax - xmin,
9 n [; }7 d# L: f9 n" w3 Oh: ymax - ymin
; l' z" N5 X% l A4 Q* ^});
4 n1 a" b* Z# j2 x8 \: Z* ~} & h) w5 @0 N, O0 B
}
& e9 i1 X2 Y% L/ }. x! \$ @# Efunction makefragments() { $ k& O- x) h1 g3 f- z! |
var p0, p1, p2, ; S4 d7 C7 R0 E5 c0 h( R
box,
0 D' h+ `6 {' u0 F# t; xfragment;
. ?% ?7 x. G3 U; X, B: `8 t) G5 a+ Ctweenmax.set(container, { perspective: 500 });
5 y' ?+ ?. `3 ^8 _' r% \8 wvar tl0 = new timelinemax({ repeat: -1 }); + T# k) [+ B3 C1 V; ?
for (var i = 0; i < indices.length; i += 3) { $ s, T# I* a/ ^. Q0 a% a4 R
p0 = vertices[indices[i + 0]];
( _8 F: u. Y' `3 ?6 Dp1 = vertices[indices[i + 1]];
G$ B" P' K1 X% V) G/ K/ ]2 B9 Sp2 = vertices[indices[i + 2]]; $ M& G Z- ]9 Q! K( Z: i/ l9 ~
box = boxes[i / 3]; 9 k6 ?; b0 Z9 |1 p& W
fragment = new fragment(p0, p1, p2, box); 5 g: q i! H$ `4 ]* p {
var rx = randomrange(30, 60) * ((i % 2) ? 1 : -1); ; j7 r# U! M6 S. o! p: u
var ry = randomrange(30, 60) * ((i % 2) ? -1 : 1);
3 F. L8 ?- ~8 x% k; n* cvar tl1 = new timelinemax();
4 I; @( |! `1 L& }7 P5 k! E) |tweenmax.set(fragment.canvas, {
1 E& [& f% N1 B( M, o" m$ b* l8 a7 My: box.y - 1000 , }4 ?1 |4 N/ `- G
});
" `& n! B" e6 e6 g/ G/ `tl1.to(fragment.canvas, randomrange(0.9, 1.1), { . u+ A% z3 ^9 J, g3 n5 ]
y: box.y,
* p5 o& A7 |/ mease: back.easeout * W5 D' i5 h: W
});
1 ~, D6 g) M+ X& g- \tl1.to(fragment.canvas, 0.5, { $ u9 }. B! _% w7 d n" ?5 h
z: -100,
; g( [4 `! e: ^0 |6 r& M5 k3 Hease: cubic.easein,
& O2 k% L1 Z& d' I2 Wdelay: 0.4 . O4 r4 e& @% r& T, |( d/ P4 Y2 P
}); % t# I- ^, G, T- ^
tl1.to(fragment.canvas, randomrange(1, 1.2), {
" c( D( L5 B9 J1 t2 x* \% Crotationx: rx,
' J" \9 y2 M3 N; C b; Qrotationy: ry,
* A2 O+ p; |" W0 i: t6 L+ B# [4 H3 @z: 250,
+ X; w7 i' n* ?; B4 aalpha: 0, . f* D+ H* b/ E* |! B# ~! g: B. a
ease: cubic.easeout D2 l. z* T6 a; N* X4 W
});
8 q( b+ B7 w) m8 q1 J4 otl0.insert(tl1); 7 Y6 \+ y0 D! {% i' a' x1 T
fragments.push(fragment);
g1 U# {8 ? W5 Ucontainer.appendchild(fragment.canvas); ; A1 F; s" Q5 q
} 7 _8 Q) w2 r- y" }0 T
}
. G$ n8 ^$ m/ I. wfunction randomrange(min, max) {
6 N4 w7 i, w, M. \3 \: \ ? F0 treturn min + (max - min) * math.random(); : h9 o/ y+ l$ T9 L& h) S2 b% F: j0 y, ]
} + M- u+ _: J# B0 I
fragment = function (v0, v1, v2, box) { 6 q' Q4 Q }& H1 r/ Z
this.v0 = v0; - Y* N* _. r- T
this.v1 = v1; + ]8 p+ [$ S) ]0 ~- ^8 b
this.v2 = v2; ' \" S$ J$ o. t5 Z
this.box = box; 1 Z, `, f9 R- v# x f2 f1 q# `
this.canvas = document.createelement('canvas'); . t) V- X/ a# z9 @
this.canvas.width = this.box.w;
! b% ]4 Z4 O1 bthis.canvas.height = this.box.h;
9 E5 e/ Q* A* C% Ythis.canvas.style.width = this.box.w + 'px';
& ~2 N+ J% [/ l# ` d; @; rthis.canvas.style.height = this.box.h + 'px';
4 L4 x; M6 D. B" othis.ctx = this.canvas.getcontext('2d'); ) I9 X0 r2 m0 y* a1 W
tweenmax.set(this.canvas, { 4 Q! H. z4 ^6 c3 g/ g
x: this.box.x, 5 r/ u( M! k" H# z
y: this.box.y , s* Q+ Q- v3 w4 d1 P4 ^
});
2 f$ f8 q" G/ i0 a7 Qthis.ctx.translate(-this.box.x, -this.box.y);
$ [8 j6 t {6 ]% t sthis.ctx.beginpath();
5 A: X5 u1 S$ K( X7 J# othis.ctx.moveto(this.v0[0], this.v0[1]); ' Y) ?, f: K; G2 S8 D
this.ctx.lineto(this.v1[0], this.v1[1]); " c; C( \: b) }; ]( ]2 Y, q0 i. w
this.ctx.lineto(this.v2[0], this.v2[1]); 1 ]( U% T, M2 ~- G; _' W. _4 m3 c
this.ctx.closepath(); ( m; f0 F" E" S* L
this.ctx.clip(); ' I8 @( n* g( g5 w; D
this.ctx.drawimage(image, 0, 0);
2 E5 l2 X3 m% [/ Q0 a8 v2 l& M}; //@ sourceurl=pen.js
/ I( c B0 o* R5 ^& T9 d" D! e) n/ z- ?$ {" ^
更多网页制作信息请查看: 网页制作 |
|