html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下1 O& @) n# w/ G7 n
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下:
! N6 C7 v8 y: ]3 o4 T/ K1 o% U/ |, X
源码下载
- h4 D) e: {( X1 W" t: J& Fhtml代码: 1 x7 a( q1 P4 Q( K/ N
代码如下:
- ^. v) B! ]( T1 @) I3 B0 _<img src=city_copy.jpg id=src_img class=hidden> * e! q& w) V7 e8 h/ v$ I! f* T
<div id=container style=-webkit-perspective: 500px;> . [/ j5 D- Y" _) X* O
<div>
/ d5 X. p* e5 t9 q7 T<script src=delaunay.js></script> 2 w' `' O# g! n
<script src=tweenmax.min.js></script>
( Y7 Z' x# \# {# k+ ejs代码: # i) H3 D) O3 C4 A9 R$ P
代码如下:% t% }) S& {& V9 w7 p( r0 N* I+ n
// canvas settings
# Z% [/ M. G7 h* p. T) jvar imagewidth = 768, , \) m e. Z2 c* ]) _
imageheight = 485; . [: \; g7 U. W/ E9 k/ _, _7 o
var vertices = [], # B" _) c3 F4 n- @7 a6 E
indices,
5 P% C: M( V) xboxes = [];
- @/ n2 L) A8 Ovar image, ; M% f* G4 Z" Z, m) x- p
fragments = [], 4 l6 v# P9 T( i* F1 o1 f& m
container = document.getelementbyid('container');
0 A' l8 n S1 e @* Uwindow.onload = function () { ; e a9 T5 |) _: [6 I
image = document.getelementbyid('src_img');
2 }& u% m r0 S8 b4 z |4 z0 striangulate(); . L: P- r# {7 m! z
makeboxes();
6 m2 E, Q0 e+ Imakefragments(); . }, [6 V" U9 B3 H( W! s
};
Q( G- y; G0 nfunction triangulate() { - p: }) ^, H8 p& V( j# \2 @1 D
var x,
4 h% ?; T5 T9 o8 K+ L# e. Xy,
& y% m8 w0 k4 T$ h K$ A6 M' bdx = imagewidth / 8,
, W5 M; L7 c, [- vdy = imageheight / 8,
& N4 A$ _0 N$ a, E% ioffset = 0.5;
* e" F. O' p; D9 W' O/ b$ z: Xfor (var i = 0; i <= imagewidth; i += dx) {
# G4 e4 v1 Y/ a% Q* rfor (var j = 0; j <= imageheight; j += dy) {
/ W* v7 Q) k1 h: d: r% R& ?: Bif (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset);
* G' e( T1 V; L$ Yelse x = i; 7 [, W0 b0 O8 J4 d' d6 M
if (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset); 1 M9 y6 Q# P% \9 O
else y = j;
5 ?6 `" k' I& ^1 W$ K& `vertices.push([x, y]); ( u4 G, ~6 {3 H3 T
} / C7 I0 A X) I) x- T8 o
} 4 p, j) V9 F7 I& Y
indices = delaunay.triangulate(vertices);
6 e( ^/ S3 X" U: _" a5 N+ o; S}
) B& U# y6 Q0 X8 \function makeboxes() { - ?9 Y' m) X H; `& z( r& E+ l1 |
var p0, p1, p2,
# ~6 @6 R2 r+ E) Y0 Vxmin, xmax, / b* ?- t* W7 C" M w: K, ?
ymin, ymax;
, a- k. c$ h( X0 H% v6 tfor (var i = 0; i < indices.length; i += 3) {
4 Y7 P4 D6 R9 G$ ^, jp0 = vertices[indices[i + 0]];
# x t" ?4 m/ o) Q. H Q! kp1 = vertices[indices[i + 1]]; ) F1 ?( @1 K' P+ J
p2 = vertices[indices[i + 2]];
: z( \" G/ T, A6 {/ _4 bxmin = math.min(p0[0], p1[0], p2[0]);
& N) Z. c% P3 h! Y+ z, Txmax = math.max(p0[0], p1[0], p2[0]); ' t! h. H$ Q. T6 [
ymin = math.min(p0[1], p1[1], p2[1]);
2 c3 n4 {. w: d7 a! hymax = math.max(p0[1], p1[1], p2[1]);
* ?# |7 x4 F8 d, Hboxes.push({ / C( ~; P: d% k# @
x: xmin, % ~( }. J+ S0 L
y: ymin,
* j2 R# @+ p! {1 w+ h' sw: xmax - xmin, 2 D) y. A1 Z/ j) D1 ]
h: ymax - ymin # h7 @0 s. I# }5 {5 R( s+ f
});
% m' d( ~" T% s/ h} $ D; s8 i9 e9 E9 `6 d
}
- o$ L. x* B% Zfunction makefragments() {
; K0 ?7 D4 c+ l" |var p0, p1, p2,
9 [6 U2 b% @7 C- h0 x* D: F9 T" Kbox, 2 p: X1 R. E" T# L
fragment;
# q! r/ r, |5 b) f& Y( ktweenmax.set(container, { perspective: 500 });
9 j4 c- \% `) w% Jvar tl0 = new timelinemax({ repeat: -1 }); 8 s& b! k! B# |, R5 x( m! ?
for (var i = 0; i < indices.length; i += 3) {
$ `5 g4 P% C2 i2 l/ [/ R7 k! Ap0 = vertices[indices[i + 0]];
' @7 r/ l! J! k1 x2 @3 Rp1 = vertices[indices[i + 1]]; : q/ a' ?" B0 L
p2 = vertices[indices[i + 2]];
' ?! g. G9 r* ? \7 X Ubox = boxes[i / 3];
" P% @* ]6 W; W x' b& afragment = new fragment(p0, p1, p2, box); - Q7 m. R! X* |( S
var rx = randomrange(30, 60) * ((i % 2) ? 1 : -1);
* {3 y Z" J l3 Pvar ry = randomrange(30, 60) * ((i % 2) ? -1 : 1);
1 @* O, `+ `: r0 gvar tl1 = new timelinemax(); " i' w3 s! o3 u9 }5 V
tweenmax.set(fragment.canvas, { 0 V( L3 E8 X) x/ r* {
y: box.y - 1000
) U% G# R+ ] F7 F});
0 ~7 Z4 L/ F/ P" ^5 a2 L4 F: dtl1.to(fragment.canvas, randomrange(0.9, 1.1), {
5 ]+ y& A: S/ U5 Wy: box.y,
9 V' ~) A8 B& k' ]1 jease: back.easeout
4 W8 Y3 J( j4 J* M}); 3 D/ R& U2 }( K- A1 _7 x3 D) {2 U
tl1.to(fragment.canvas, 0.5, {
0 F o* S4 p! |5 m, s2 Hz: -100, 0 A, T8 X- p$ Z4 y- B
ease: cubic.easein, $ \2 [: v, w# A: e2 d" C! G
delay: 0.4 , X& o& f% U8 R8 b% S9 ]4 a
}); ( A( Q) b4 e9 U+ u; O
tl1.to(fragment.canvas, randomrange(1, 1.2), {
2 X& Z ^8 @+ _' u9 jrotationx: rx, , D1 B9 T# s. l
rotationy: ry, 8 s7 Q1 S" d6 d! ]0 v$ S+ D- x
z: 250,
# S+ W+ H1 U( N/ q2 @alpha: 0,
9 i# n" }- q1 U" r; C% gease: cubic.easeout % C; F' A, c# M+ Y
}); # G- f i, I9 y1 n8 t' j6 Q
tl0.insert(tl1); # E8 L* s$ a( Z% V: D' t
fragments.push(fragment); / c' y/ K" j8 f+ a
container.appendchild(fragment.canvas); 0 q( R% L2 |3 [- I. }0 h) |% L- i
}
8 d& a2 w. G0 i2 W} , Y3 @0 R" y- ]6 k& R
function randomrange(min, max) { 1 y5 w1 e- D0 C# G0 X
return min + (max - min) * math.random(); 6 U* A8 l3 u$ V) P( `
} ' f. g% n( T1 P+ n) D3 z
fragment = function (v0, v1, v2, box) { ) H \8 l# _! j: g5 L! O" j# w
this.v0 = v0;
# _1 F3 m* H' w" tthis.v1 = v1; ! i0 h. m9 g/ H2 c5 T( Y
this.v2 = v2; % M, x* s6 v; \
this.box = box; # ^/ z% ~) }& J ~" ^1 q, s* O
this.canvas = document.createelement('canvas'); , v, B/ h8 w5 {! {& p% e _
this.canvas.width = this.box.w;
" c9 K+ t8 M9 M/ k; i# w# M+ jthis.canvas.height = this.box.h;
. J7 q$ E8 q. j. U: sthis.canvas.style.width = this.box.w + 'px'; ) M+ R6 `" t) f, _2 _+ n
this.canvas.style.height = this.box.h + 'px'; 0 ~9 J$ a: E: L/ \( ?( }# b) n
this.ctx = this.canvas.getcontext('2d'); : q) D" J. L% _* B5 K
tweenmax.set(this.canvas, {
Z4 \. n1 ?! q+ ~, `4 ax: this.box.x,
/ L8 i2 @6 J3 X% O0 q ?y: this.box.y
' X8 ^/ \8 a% D0 ]});
& `% \7 F( Q3 ?& Y2 T! }this.ctx.translate(-this.box.x, -this.box.y); ; g& s! L* r6 A. l) M3 c, b. u
this.ctx.beginpath(); ! p# ?, ]( I) h) k9 Z- {+ { m
this.ctx.moveto(this.v0[0], this.v0[1]); 1 Q: t4 p0 R* F2 e' c) I- X- r" F
this.ctx.lineto(this.v1[0], this.v1[1]); . l0 @4 {4 J; d
this.ctx.lineto(this.v2[0], this.v2[1]); . e* I/ T9 b( F- t& {" X7 n+ J
this.ctx.closepath();
: B# J% `" [5 ethis.ctx.clip();
. u1 K7 V- e( R5 |( p5 g' {this.ctx.drawimage(image, 0, 0);
" X: x# R! q2 s/ |" l8 b}; //@ sourceurl=pen.js
( @: B. [" C# v; s* U$ T- I! M' U, w
更多网页制作信息请查看: 网页制作 |
|