html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下& T2 `8 S' v+ k! g3 I
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下:
7 l( V( d* |/ O
0 X8 ~: ^9 I" P& a& U1 w1 `源码下载
! m- c7 c4 ~- A1 S. X K& {, D) khtml代码: - a$ ], Y0 o; G; G
代码如下:
/ n1 k+ \( J+ ?& l, i7 P8 _8 i<img src=city_copy.jpg id=src_img class=hidden>
x( `. A" N" `6 |<div id=container style=-webkit-perspective: 500px;>
. J- _5 u$ f9 E2 E3 j<div> : l" p3 c! {4 O# D H7 w1 g$ `
<script src=delaunay.js></script>
' m1 |9 _5 I5 k0 W( E/ s<script src=tweenmax.min.js></script> 8 O/ [5 y/ |. F: g6 T
js代码:
; W& M, d7 G+ V* Z* ?% ]+ x代码如下:
1 o& I* s* [" E4 S+ ~% ~$ y7 `7 ]// canvas settings
- Q9 c6 J e- W1 q4 M/ Z1 }% ]% Svar imagewidth = 768,
6 W2 U8 d' r2 Z s% a& Ximageheight = 485; ! r5 c7 u/ ^' S8 L5 x$ s, b7 @
var vertices = [],
, l% v' c4 e. n d$ D) |indices,
! Z% L; V: Y! xboxes = [];
1 C% h! z. \/ D4 O& A" Yvar image,
# L1 Q9 y8 O" q7 ifragments = [], }* }: E8 l+ V& I$ Z
container = document.getelementbyid('container'); , F' K! `; Q/ N
window.onload = function () { ' @2 q& \& p& k! o0 A- t
image = document.getelementbyid('src_img');
, T& B- A' c) qtriangulate();
( h! i0 d: `$ }makeboxes();
9 p, M4 |9 {* w: C7 Bmakefragments();
, E* S+ _7 I$ E5 ?- Y* H) L& Y}; 3 |& N7 m$ n @5 c1 O/ r$ l
function triangulate() { 6 o# `1 P, g, U( u1 W, X3 q* ]7 B
var x, * \: K' |0 a8 D
y, & |- a0 z4 H# M3 b* q' t/ \
dx = imagewidth / 8,
7 V3 R# u) _7 N. @- L* E7 vdy = imageheight / 8,
$ @/ z9 S2 g2 X+ }offset = 0.5;
: G" I4 Y, \$ u/ J9 mfor (var i = 0; i <= imagewidth; i += dx) { 1 ]/ P9 z8 E& [8 O3 H. F
for (var j = 0; j <= imageheight; j += dy) {
! y( }9 p& k7 v* L/ G6 Eif (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset);
9 P* D: w3 x2 @9 Telse x = i; 9 z: {* s6 b) J7 Q/ [4 B
if (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset); * z9 y- h3 Q/ y( g6 l) |( e
else y = j;
. W* @8 D$ [- `vertices.push([x, y]);
" B$ U L$ d/ Y+ o& Y0 g, u D+ l} 1 [( M# H, ^, |/ q- I+ W+ s
} , [. i4 z- R0 c4 Q9 Z! w
indices = delaunay.triangulate(vertices);
1 m5 \5 V9 U# K3 Q! M+ V# e: Z} # r8 {- w0 ?+ B" I/ u& Y
function makeboxes() { 4 V M6 Z1 R: p" X
var p0, p1, p2,
% ?2 }; ~ g3 C1 a5 R; D# H4 h. w/ Qxmin, xmax, K9 k# W7 ]* z- p
ymin, ymax;
. ]# O+ T" ]6 T. X$ I afor (var i = 0; i < indices.length; i += 3) { U+ d3 b/ n I# ?
p0 = vertices[indices[i + 0]];
, X- [6 y" y( C, L+ j! _3 ep1 = vertices[indices[i + 1]]; & i9 `4 t" @( K; W6 w$ B) G
p2 = vertices[indices[i + 2]];
2 k3 x. H' q0 Y w9 w! [xmin = math.min(p0[0], p1[0], p2[0]);
2 X' g( _: C% Y x6 f% i2 R0 Y) mxmax = math.max(p0[0], p1[0], p2[0]); Y% N, b( e x9 v8 A7 A# E1 C
ymin = math.min(p0[1], p1[1], p2[1]); / [( r! v: p1 e/ F
ymax = math.max(p0[1], p1[1], p2[1]); 1 y! B% {9 x- m' d9 p" P
boxes.push({ 8 U3 `- W; ^4 H, c
x: xmin,
% Y: E0 G, z# z- }$ hy: ymin,
9 a1 M# z. X# z5 W4 ~) K R' jw: xmax - xmin, 8 R F- r8 W1 C( ^$ Q1 G) m; \7 i: V
h: ymax - ymin
6 n- d# n9 q5 I2 k! D});
/ h* W" ~: l5 N$ ^: k" d/ w}
6 e$ i I8 L7 r/ J} 4 K- X' a. l6 l/ b# w. S
function makefragments() { 0 A" ^2 k! M; x! R
var p0, p1, p2,
2 Z& @4 `& f o; Z9 E- Qbox, 9 Q( c. K2 Z' q+ l/ c
fragment; 3 f8 z) J9 g- D" Z' w
tweenmax.set(container, { perspective: 500 });
. @0 e+ W& C9 x3 K5 Bvar tl0 = new timelinemax({ repeat: -1 });
7 C; q: y# o! W5 x! i3 dfor (var i = 0; i < indices.length; i += 3) {
9 j- }" o, o2 Bp0 = vertices[indices[i + 0]]; ) ^ s5 f$ Z1 b! C
p1 = vertices[indices[i + 1]]; 5 A. t0 s: d. \4 g
p2 = vertices[indices[i + 2]]; 8 o9 S8 c" {8 H/ H% F
box = boxes[i / 3]; 1 H. d( {+ ~5 W8 f4 ~
fragment = new fragment(p0, p1, p2, box); & \# s7 ]$ v: V3 G
var rx = randomrange(30, 60) * ((i % 2) ? 1 : -1); # E4 `' Q; }* L" W
var ry = randomrange(30, 60) * ((i % 2) ? -1 : 1);
) `' P; P3 j9 J$ rvar tl1 = new timelinemax(); - @- f, w0 ^: [
tweenmax.set(fragment.canvas, {
' @9 a: Y' L$ C H( d: v, j) Dy: box.y - 1000
! Z) Z$ d/ }' a( W});
- Y& ]$ \5 A: G2 _4 I; a. gtl1.to(fragment.canvas, randomrange(0.9, 1.1), { * i7 G' c. C, j z. Q" ?, W3 r
y: box.y, : G9 _$ q* U" P
ease: back.easeout
) T4 k! S5 n) T4 @});
: ?4 s5 f- _& t4 ^; ]4 h7 s+ S; atl1.to(fragment.canvas, 0.5, {
: @8 x, b/ [6 d: O& g2 k$ m5 T$ s, Sz: -100, ) n) |! A0 P( }
ease: cubic.easein,
" z- T, F9 Q( B" Rdelay: 0.4 ( b* A. C0 y& e' `* i7 B
}); / m* _+ G8 i9 V% K( d- x
tl1.to(fragment.canvas, randomrange(1, 1.2), {
3 G k8 Z7 ^6 y0 rrotationx: rx,
; L V7 U8 l8 e* E% wrotationy: ry, ; C+ J/ f. ]/ l7 \7 g
z: 250,
* U( L0 c2 n1 L% O6 C5 R& Q# zalpha: 0,
) Q1 U5 I# _) N v, Zease: cubic.easeout - Z& H3 E. ~, Y! N
});
9 R9 e" g7 D' L) u, Ltl0.insert(tl1);
3 K4 _1 ^% p C n2 `. }( Gfragments.push(fragment);
. N( B0 |+ d- ycontainer.appendchild(fragment.canvas);
& i1 @ L# a0 X* B5 o4 x l+ @}
8 n* L! T+ {5 d K8 b* A}
7 D2 z" n0 I* W" I7 f) Cfunction randomrange(min, max) { 7 z& ?! A- g: _" K2 \# u
return min + (max - min) * math.random();
1 t ~$ y2 { _' n0 e) V}
# }! v0 \3 k6 Q+ Zfragment = function (v0, v1, v2, box) {
! \( i& _- h5 q0 ?+ D' V4 _. f! ethis.v0 = v0;
7 d# k/ K: {! F1 Z; U& q+ E/ bthis.v1 = v1;
) n2 R4 n4 y" Bthis.v2 = v2;
/ C7 n: e! `' @8 athis.box = box;
. F1 x* [3 H* Y' x& t! v o4 ythis.canvas = document.createelement('canvas');
* X3 Y0 w+ C! n; T5 g" ^( vthis.canvas.width = this.box.w;
/ U0 e0 y. V! L3 _/ B* Pthis.canvas.height = this.box.h;
- h# k4 x( J; v3 Y$ nthis.canvas.style.width = this.box.w + 'px';
3 {" ~' v d- M& }. ` m, K: Wthis.canvas.style.height = this.box.h + 'px'; / R$ G* ~8 k$ A7 W% z/ N
this.ctx = this.canvas.getcontext('2d'); 6 Y4 O& M1 b( M/ d4 y4 v, {
tweenmax.set(this.canvas, { % J e5 B8 G6 [, @0 Z; x
x: this.box.x, 5 z, e- \' v2 S; @1 t8 o1 G1 ]1 g
y: this.box.y " w. [, C% I/ @0 {; |
});
, J6 J0 _" _! z4 W2 rthis.ctx.translate(-this.box.x, -this.box.y);
1 Q+ ?. j& s# C' ]/ P2 L4 Sthis.ctx.beginpath();
0 R, `( U( q& f2 Nthis.ctx.moveto(this.v0[0], this.v0[1]);
2 |" e4 \* U2 hthis.ctx.lineto(this.v1[0], this.v1[1]); + N" [: f0 a( K4 l# }2 E
this.ctx.lineto(this.v2[0], this.v2[1]);
. A* j5 ?1 D; K: ^, D3 {this.ctx.closepath();
7 h4 l. M2 O% ?& j* Y, zthis.ctx.clip();
$ p8 C$ E. R6 c, Bthis.ctx.drawimage(image, 0, 0);
' [( I' M7 Q6 t4 ~" w9 E}; //@ sourceurl=pen.js
& o' C! e% ^' M5 D7 U9 u4 g) B0 F6 A! C0 J0 b- ~
更多网页制作信息请查看: 网页制作 |
|