html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下: N: X) e7 ]6 ~8 l! N, j) S; ?( q$ P9 y
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下: B7 O J. N1 Q
, J4 {+ z& A$ v" E源码下载 0 ~2 ~) h/ |- K- }
html代码:
5 Z* w4 l' R) y4 L. x e代码如下:
. }' r2 c5 [3 C' y' Q" M5 Q<img src=city_copy.jpg id=src_img class=hidden>
4 Y4 v5 V" B# P+ T# m<div id=container style=-webkit-perspective: 500px;>
9 E; u- [# N3 r<div> ) B9 I! h; [+ @
<script src=delaunay.js></script>
1 w4 r& E. Z0 r" L+ w- ?7 ~* P q<script src=tweenmax.min.js></script> $ B9 p/ T. k0 z5 t* u' [
js代码:
8 N. K( B9 G* J. V代码如下:: s+ u9 j+ o( _' v
// canvas settings
: H& }% C1 ?; o6 r) T9 ?# Mvar imagewidth = 768,
! u0 ]9 U) H5 d+ R) O% Wimageheight = 485; B! H7 v0 k$ U0 m6 T& x
var vertices = [], W+ T. M& \/ Q: {* N) E9 a+ T$ X
indices,
- l" I( u5 P2 ], K' f2 oboxes = [];
" V- \, Z( Q9 f7 `, ^" N. kvar image, # }- F; q: m8 n ~) v4 `) d0 V
fragments = [],
5 Z/ m1 k3 S; Econtainer = document.getelementbyid('container');
+ e% v4 f8 e, a6 l# X9 Ewindow.onload = function () { 6 O( b8 V" Y9 A" B% s( ^3 g
image = document.getelementbyid('src_img'); - c% a0 d" G5 K4 S7 A9 I" h- E
triangulate(); % {; o( Z# L7 t5 g2 a
makeboxes(); , t+ ], G3 V$ {. I7 Z
makefragments(); 3 @! h u- Y; N6 g5 y" b
}; 3 J8 e8 i0 w8 F/ X7 l& y( |. k# ?
function triangulate() { o* L1 o& V& ?* Q7 s
var x, 4 Y% k3 {% Z# K4 b# `$ _
y, + B: v. @4 }0 b5 M; R0 k' Y. H
dx = imagewidth / 8, * ?! b7 m6 N; q; [6 B2 ^1 \
dy = imageheight / 8,
4 {8 _7 F2 S e9 Moffset = 0.5;
! G4 L0 s% y/ N; K, E: i6 ` zfor (var i = 0; i <= imagewidth; i += dx) { , p" i4 `* {9 r& j- N
for (var j = 0; j <= imageheight; j += dy) { 4 D; o& q5 c( G
if (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset);
6 N3 u2 P: N& E% Y; w0 x- p- j, }4 kelse x = i; , Y' I- y/ ]6 @& k8 H
if (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset);
) K2 i- z6 z' x. q; ~& `- y5 Celse y = j; ) H$ ^; G# P* }( k0 i9 p9 E; ]
vertices.push([x, y]); , h. C7 Y( I7 h7 V) v$ c( F+ r
} & i* ^0 @6 w5 B" c/ Y7 R6 f+ J% i
} 7 ~1 l8 H+ E5 g0 c
indices = delaunay.triangulate(vertices);
) Y3 p3 O1 B6 W. v" }. ?! k' z}
0 h$ V9 ?: W0 V+ G* e4 L& M: Kfunction makeboxes() { * S( N6 i$ K5 A; N1 }
var p0, p1, p2, 6 @& d t! u3 p
xmin, xmax, 9 h% j I: d, N4 e, A; g9 I3 k1 R
ymin, ymax; 2 R# t# b# r1 }/ z
for (var i = 0; i < indices.length; i += 3) { ) k& s! Z9 a5 n( S% s+ O
p0 = vertices[indices[i + 0]]; 6 ~6 U- m# J$ k) R+ W }
p1 = vertices[indices[i + 1]];
+ n3 q" m0 a* ?7 R0 Vp2 = vertices[indices[i + 2]]; : V+ ~5 G: c8 f9 I3 L
xmin = math.min(p0[0], p1[0], p2[0]);
1 j0 [' t. H2 Y2 u+ {7 z* V1 bxmax = math.max(p0[0], p1[0], p2[0]); 4 S6 V( u$ E% W
ymin = math.min(p0[1], p1[1], p2[1]); ' P+ }; z$ ?" k( E4 G( Q9 l
ymax = math.max(p0[1], p1[1], p2[1]);
5 v. w1 {5 K/ a( j3 Mboxes.push({ : r! p% z) E" J
x: xmin,
7 R* r! r* M. h% py: ymin, Z( Q* Z/ Z7 ~5 l' W
w: xmax - xmin, 0 l7 a' Y2 j5 Y7 J3 ~) W. T
h: ymax - ymin
5 B( g, i0 o' V& ?1 K});
+ Z3 m7 K. S8 G+ P0 n! y: `}
. q/ W- o6 M' H' {6 S} 7 H6 h9 d/ r8 S# T. Q7 p! `
function makefragments() { 1 p! c& [& O8 s( \8 b
var p0, p1, p2,
" _7 m( w5 P, `box, 4 U" j8 f" b- u
fragment; 0 ]5 D: P4 h/ L5 \5 r: a1 y
tweenmax.set(container, { perspective: 500 });
9 P. E9 F0 A: p9 K% K [% hvar tl0 = new timelinemax({ repeat: -1 }); 5 |. R2 G/ E+ V3 X4 t
for (var i = 0; i < indices.length; i += 3) { 6 }" Y0 T/ X& `
p0 = vertices[indices[i + 0]]; 0 \# ^0 i- I8 ?) [. f$ S
p1 = vertices[indices[i + 1]]; 1 W! d& x: @# D8 T( U! M( u5 q
p2 = vertices[indices[i + 2]]; $ q. W7 S! y. f+ }& R% I* [. l
box = boxes[i / 3];
* l) v7 n! V' c$ R! y3 ~5 z% Ifragment = new fragment(p0, p1, p2, box);
- M9 [# i, |$ u' Qvar rx = randomrange(30, 60) * ((i % 2) ? 1 : -1); 6 O- M9 i7 m- y. |8 @) A+ b3 \
var ry = randomrange(30, 60) * ((i % 2) ? -1 : 1);
+ R i. ]+ @6 [$ F3 e3 d0 fvar tl1 = new timelinemax();
% w+ N5 x* {: [- V% D- R6 atweenmax.set(fragment.canvas, { ( q4 h$ z* s+ ?. q8 S) t# _
y: box.y - 1000 $ B9 `5 b. d! e6 F' [
});
* k! u: M9 z9 y4 {tl1.to(fragment.canvas, randomrange(0.9, 1.1), {
" R( M( w# U) zy: box.y, & `& w% W8 S8 h
ease: back.easeout 7 n. a; r" n& p+ j+ Q, t7 P
}); ) i( _3 c% }/ Z+ a t5 b/ |# a
tl1.to(fragment.canvas, 0.5, { 2 L/ q" J1 }0 y* b) C8 ^# d
z: -100, . n# D O$ n$ |# y
ease: cubic.easein, 9 J: z& \7 n, F, k6 T# P4 K& f& V
delay: 0.4 4 |; u& I7 d! X/ q
});
7 N( u) T Z/ x$ O% t% x* z2 Xtl1.to(fragment.canvas, randomrange(1, 1.2), {
* T+ P8 i1 N& Jrotationx: rx, 2 ?: T* k( o4 [4 B
rotationy: ry,
" d5 I7 F: D. {; Mz: 250,
6 L7 N' z4 }3 ~alpha: 0, + i/ M5 \) G- Z' O! f2 z F3 S! c
ease: cubic.easeout & `# ~6 a% |8 a: Z2 S1 v8 \0 @
});
* Z4 A. `* T. Q. g% E i3 j) wtl0.insert(tl1); 9 k2 z9 e' K w
fragments.push(fragment); - n1 @6 [& h; z3 Z
container.appendchild(fragment.canvas); 5 W# B) Q8 P9 ]1 v7 x6 N
}
3 J) a7 j; f& x5 t( l6 p}
' ?, O# q% j8 S" cfunction randomrange(min, max) { ) K$ q: [9 K b5 S% [
return min + (max - min) * math.random();
* R& C( E7 r2 V2 w, I! p} 3 F+ [. Q- _' J
fragment = function (v0, v1, v2, box) {
9 i n: r* U4 wthis.v0 = v0;
5 N; F3 S- Q `this.v1 = v1;
, D. H7 }2 a8 F) M, Q Pthis.v2 = v2; ) Q" y8 e+ H& x5 R& m5 ]
this.box = box; 2 [1 l0 M/ E1 d8 r* o3 N
this.canvas = document.createelement('canvas'); + ~, m7 a) s# ^3 G3 ]# w( Y
this.canvas.width = this.box.w; 6 |3 n5 K) \, Q; m: u, W% Y4 u
this.canvas.height = this.box.h;
; ?" F! w4 f6 w. dthis.canvas.style.width = this.box.w + 'px'; * ]1 h3 Y2 i* {1 |9 x* X/ d7 N
this.canvas.style.height = this.box.h + 'px'; 6 D; F+ G0 p8 b1 f' i& H
this.ctx = this.canvas.getcontext('2d'); , o2 F/ ~, n3 e, Q/ e
tweenmax.set(this.canvas, { # W7 O3 t2 V4 Q1 h2 {, ~
x: this.box.x,
! D" C1 i3 X3 D5 T. G5 Ry: this.box.y - y8 b s. T/ Z2 s0 R
}); 8 s7 Y) L# P+ e4 n5 n
this.ctx.translate(-this.box.x, -this.box.y);
' K3 R9 @0 o: dthis.ctx.beginpath();
( I D; [, r+ ]( U" f' uthis.ctx.moveto(this.v0[0], this.v0[1]);
5 ] A2 c+ N" @this.ctx.lineto(this.v1[0], this.v1[1]);
( |" m1 E( O' X' R! rthis.ctx.lineto(this.v2[0], this.v2[1]);
7 i- E% f& r% M2 ^! athis.ctx.closepath();
# g9 J3 s- g$ x0 ?' O0 rthis.ctx.clip(); $ J) g7 s5 S$ m! }5 y8 j* J
this.ctx.drawimage(image, 0, 0); 0 {" |( n0 u: K8 j
}; //@ sourceurl=pen.js 5 h W/ C: d; ^! a& ]
! p4 p3 t, A$ Y
更多网页制作信息请查看: 网页制作 |
|