html5 canvas实现图片玻璃碎片特效,图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息,效果很不错,喜欢的朋友可以参考下* T) N( m: o/ d
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下:
- E- C1 h9 J! X D, A# z8 x" }7 [5 G2 `" C
源码下载 6 n; x+ ]- e" X! Z- e( I
html代码: 3 u1 i( U# f6 Z- S3 A" r
代码如下:& `( n2 [! B; b; w# ]. \
<img src=city_copy.jpg id=src_img class=hidden>
/ T9 M. l/ X; x% ?<div id=container style=-webkit-perspective: 500px;>
W- D# b0 f( Z- B<div> 7 v' U* E, M. K0 E
<script src=delaunay.js></script>
3 |. A% u5 i, D; X<script src=tweenmax.min.js></script> % J5 W+ }% D* b$ R5 @; L
js代码:
7 ?2 u% h0 L7 F) ?! b# o3 F代码如下:2 h8 Q( S3 E! d* O+ h
// canvas settings
9 o8 W1 ~( t2 \2 L2 C3 e1 tvar imagewidth = 768, ; Y: T' p; L. R) z! k
imageheight = 485; 0 q8 V2 }3 T' N% y% T. j
var vertices = [],
O9 S$ N+ o3 a2 z: oindices, - l3 v& e7 D- g6 b/ O
boxes = []; : ~6 j8 h# I% l* J8 m$ Q! s1 f# n6 c# ]
var image, / a. ]9 F, w3 t$ l% Q; v' y6 ~6 K
fragments = [],
, |; x- ], b% I0 [7 _container = document.getelementbyid('container');
5 B5 m% M) o" J5 Dwindow.onload = function () { # m6 F/ J9 ^0 x, K
image = document.getelementbyid('src_img');
+ Y2 P9 W ^8 d: C4 xtriangulate();
6 ?* Y6 G. F6 c+ e& O2 ~makeboxes();
3 h; F' U# W8 ?/ u0 U/ v* X9 vmakefragments(); 7 V q/ \7 O0 d6 W
};
, ^" _; K" O/ F3 ?function triangulate() {
+ t7 Y" Q4 g) r/ o; U: m' L: @var x,
: d1 F7 R- o G* Qy,
( H( l" }* o m% K* p; a- idx = imagewidth / 8, ) q5 m' l/ z: q, P8 D. f2 n
dy = imageheight / 8, & D5 A+ G9 |$ j: e6 h- P, L
offset = 0.5; . k0 m' f3 Y# G( U3 N- Y% b# ^% a) |+ U
for (var i = 0; i <= imagewidth; i += dx) { - }8 Q1 ~) I" f) d" W: F% \9 Y3 T6 Z
for (var j = 0; j <= imageheight; j += dy) {
0 R- S4 S0 z# p- Q% j7 Dif (i && (i !== imagewidth)) x = i + randomrange(-dx * offset, dx * offset);
# a$ Q6 l( t# F( [* ]0 T7 pelse x = i; 2 e5 C% `, S D! {
if (j && (j !== imageheight)) y = j + randomrange(-dy * offset, dy * offset);
7 Q4 e5 S5 J/ A& H+ yelse y = j; ; B: a- s% b7 t; V0 n! ~
vertices.push([x, y]); + H) m: G/ K! `) i9 Q- g
}
5 o" H8 e+ ]4 m, h& ~% D} # \4 O2 ^& j9 c
indices = delaunay.triangulate(vertices);
9 I2 y/ q& t9 J6 o3 S4 b} ' D T$ { d! o
function makeboxes() {
7 H- @6 h3 Q: i$ @& ^( }var p0, p1, p2,
) Q5 |' `+ {( U" C0 S! Q7 o- G+ Bxmin, xmax,
7 R* K/ q) M% q: U4 Yymin, ymax; 1 ~% I& e* R V
for (var i = 0; i < indices.length; i += 3) {
$ m) k5 x' X! f7 A5 r3 g% p2 ~p0 = vertices[indices[i + 0]];
2 P+ s0 f) N. B7 K) F( f. jp1 = vertices[indices[i + 1]];
- \7 u) D. e0 m& Up2 = vertices[indices[i + 2]]; $ c* }4 l1 A8 r" L/ f4 a' [
xmin = math.min(p0[0], p1[0], p2[0]);
, b' f; s+ R1 m/ ^. F# Y1 `7 Uxmax = math.max(p0[0], p1[0], p2[0]); . |- L5 g# C; C
ymin = math.min(p0[1], p1[1], p2[1]);
, K3 y# O" @! j% M a/ Q! Cymax = math.max(p0[1], p1[1], p2[1]); 2 `& l# d6 B7 e, {; e6 i
boxes.push({ , u, D% u4 X% D8 Z
x: xmin,
" V2 g! d; P- h9 H' ?y: ymin, 2 B2 Z* ~0 _' R. |1 E! d# z
w: xmax - xmin,
( `+ F! S$ n5 A m, yh: ymax - ymin / T8 U4 R: p C1 h* W
});
. f7 @' I/ H- {9 C3 y}
. ~4 D) V4 z3 B# A} ' e0 m0 B6 l9 [! g) E7 K4 k
function makefragments() {
& M. }4 P+ s- E. W' [+ E A( q* q9 L. ?4 Mvar p0, p1, p2, & j* F5 T& B e# ]: Q! Q- A+ y5 s
box,
& ]7 D6 l4 D$ q% `* A8 \fragment;
+ W2 N! d" @+ T, E9 Htweenmax.set(container, { perspective: 500 }); 7 Q4 K9 Z! N0 N% D) O8 w, r+ J! K$ d
var tl0 = new timelinemax({ repeat: -1 });
$ g; m0 Z/ `0 y- ~2 M7 p! ?for (var i = 0; i < indices.length; i += 3) { 9 X# T4 d5 k: {7 p7 u. S, v$ y7 i1 b
p0 = vertices[indices[i + 0]]; 6 N! z: i( c0 i7 q1 t) `8 k3 ~
p1 = vertices[indices[i + 1]]; % T, p w7 W) p% x4 ~
p2 = vertices[indices[i + 2]];
& { c' |8 C% hbox = boxes[i / 3];
3 o, ?1 k2 Y+ j! z* dfragment = new fragment(p0, p1, p2, box); 9 [! T& I) e) I/ k! s
var rx = randomrange(30, 60) * ((i % 2) ? 1 : -1); 4 T2 m$ b1 }: i: I( l; j! P. ^
var ry = randomrange(30, 60) * ((i % 2) ? -1 : 1);
+ B4 I: p9 s: u, u5 Mvar tl1 = new timelinemax();
' `% ?7 T+ H( }tweenmax.set(fragment.canvas, { 3 c# P) T/ _# u8 K$ V
y: box.y - 1000 ( u0 t+ ^' t; c: D, T
});
' n; h( ~. M$ F! n2 P! ^6 T9 Jtl1.to(fragment.canvas, randomrange(0.9, 1.1), { ( \7 ^# n4 c& W+ R: j
y: box.y, ; F, o% {1 B- ]6 R
ease: back.easeout 2 R2 \, p, n. d" N' \
});
) t* T K6 Y: F5 Ftl1.to(fragment.canvas, 0.5, { + R+ A' T* M6 P4 c5 o1 v1 B( s
z: -100, # E' d" T& h$ `
ease: cubic.easein, % k; L1 e8 C# }3 v/ R# W
delay: 0.4
- S5 f+ r/ W' ]+ |7 @: ?) W});
3 D2 X# {7 A+ V# X' Vtl1.to(fragment.canvas, randomrange(1, 1.2), {
$ ]/ k- Y& ^/ L/ `' ?# _) Drotationx: rx,
) L0 ^7 I4 s* R5 K7 X- r" y# N3 wrotationy: ry, $ O9 @3 `3 |, A# N: d3 b. n
z: 250, 0 v: w. S. R" ^0 h+ s) E* J
alpha: 0,
8 [3 Z+ `4 u* d1 Oease: cubic.easeout
0 q5 n2 H! h# ^% A+ @4 n* J});
# ?3 \6 j4 K" p7 `" j# Y3 A0 Atl0.insert(tl1);
6 B" `0 ?( f2 v; M i0 z s+ bfragments.push(fragment);
2 M; I, b- J3 Jcontainer.appendchild(fragment.canvas); ! j4 Y. S' W$ A3 E& f0 _
}
0 b' j3 @* `' a4 K7 i6 R}
& \5 e) c3 k# p( M7 ffunction randomrange(min, max) { + s- X2 l8 T. a& Z. m% v4 g6 F% E
return min + (max - min) * math.random(); ' w3 F3 p9 I" g. l2 I& U& h5 n
}
+ d# _ z2 f0 O, o$ ~9 o( Sfragment = function (v0, v1, v2, box) { 0 u$ {9 s ?/ j {8 \/ J1 m* s
this.v0 = v0;
0 X' L4 y3 \# h; ]this.v1 = v1;
8 ? Y' {: W$ j3 B- u7 H! gthis.v2 = v2; 8 o/ p- v9 i: o, X9 r& C
this.box = box;
9 v. a5 f9 K( D3 Q) r$ Wthis.canvas = document.createelement('canvas');
2 C, y; E1 D4 i2 w- o: @this.canvas.width = this.box.w;
! P! F' Z8 u5 Y) m5 |this.canvas.height = this.box.h; . N1 U& Q/ N. I" G( ]5 O
this.canvas.style.width = this.box.w + 'px'; + }& ]3 \# H1 g3 t2 H( @; L6 {
this.canvas.style.height = this.box.h + 'px';
# r9 b+ S+ o; L e" v2 kthis.ctx = this.canvas.getcontext('2d');
0 Y w8 N C7 s: Q+ {( O# O: ~tweenmax.set(this.canvas, { 7 t1 q, `9 r! j6 m" a V
x: this.box.x,
9 k9 a& L0 g+ f- c3 t6 xy: this.box.y
# L0 [, H8 |- R; s; ^# c! H});
6 N/ O4 z. ^; Q+ y: v3 X) Xthis.ctx.translate(-this.box.x, -this.box.y); 8 D; C( Z% h4 i* s( M
this.ctx.beginpath();
0 V$ `! i& G( @6 Nthis.ctx.moveto(this.v0[0], this.v0[1]); " {: b, G; i! D9 R; i" s# K
this.ctx.lineto(this.v1[0], this.v1[1]);
" U0 z: H F% z( V0 V! G n. N5 }/ {" Nthis.ctx.lineto(this.v2[0], this.v2[1]); $ {) i4 w u I4 V: a9 F- } ~
this.ctx.closepath();
$ ^/ v" X9 o" F- wthis.ctx.clip(); : x8 y4 B- i+ I/ ~7 \5 ?9 ^$ i
this.ctx.drawimage(image, 0, 0); 8 i! p3 C m) H; y7 @
}; //@ sourceurl=pen.js
Z' i' M8 D7 o2 z' }& y! w. @- P* V
1 I9 j5 ]5 ?2 V' q3 H! L W& `更多网页制作信息请查看: 网页制作 |
|