返回列表 发新帖

html5版canvas自由拼图实例

382.6k 8
swmozowtfl 发表于 2015-7-10 22:26:00|湖北 | 查看全部 阅读模式
本文实例讲述了html5版canvas自由拼图的实现方法。分享给大家供大家参考。具体方法如下:( b# z+ l8 e7 J0 @" s/ a
代码运行效果如下图所示:
( t8 |9 G. q0 R

; ~' Y! j4 Q8 a" ]                               
登录/注册后可看大图

' o' D4 l+ U, q7 Bcanvaselement.js代码如下:) R: T) m* J2 G2 O0 ^
复制代码代码如下:define('canvaselement', [ '../multi_upload/core' ], function(s) {
: C3 H4 l2 p  _" Rvar canvas = window.canvas || {};
0 b5 u4 i* h' y5 u9 {0 s( O& e8 [(function () {% {( C) C; M# r8 V
canvas.element = function() {}; 0 a/ e% E" G3 r, a4 |6 P
canvas.element.prototype.fillbackground = true;
, u, V0 n- O. n! p3 t4 @2 _9 ]0 xcanvas.element.prototype.showcorners = false;( y* Y  `# n" z# d  x5 Z: Z5 S
canvas.element.prototype.photoborder = true;9 q* u. E1 e& p& p, ~
canvas.element.prototype.polaroid = false;
2 f, J: C. ~# r" f3 r: K- ocanvas.element.prototype._backgroundimg = null;
' i: x, `+ n& m* ycanvas.element.prototype._groupselector = null;$ \" @  O- k* x' d  A+ e8 S0 F
canvas.element.prototype._aimages = null;
/ `9 [1 ~) ~: E- s' P" V( e$ dcanvas.element.prototype._ocontext = null;! H# @1 Z0 u, ~( D- j) a# j4 W
canvas.element.prototype._oelement = null;
' Q0 l8 @5 W) N6 Y" xcanvas.element.prototype._oconfig = null;
6 T0 E" `$ Y' F4 \  O5 Pcanvas.element.prototype._currenttransform = null;. `0 w& e% D5 A" V
canvas.element.prototype._prevtransform = null;
) Q- L8 h' D% gcanvas.element.prototype.curangle = null;
3 m7 Y9 Q. T( f- `. I  d8 @canvas.element.prototype.init = function(el, oconfig) {
7 o1 D& p, _& z$ V" f9 vif (el == '') {
* l: `; t+ y6 @8 p7 X/ t9 {return;
: N8 M. r  U/ b}$ U  z7 Z5 N7 D' T
this._initelement(el);  f: {! n+ |) I9 Y( G9 }
this._initconfig(oconfig);. @; G6 E! Z& G$ F7 R1 Y" f
this._createcanvasbackground();/ U; }' l4 ]! r0 A. ?
this._createcontainer();
# g5 u0 ]: f5 }4 e1 B, h" B* t) Qthis._initevents();
: r, d; l6 J* a& U9 z2 W& {! ethis._initcustomevents();
# s/ Q, {* H. c' ?( K, K( C3 r3 e};
' `9 m' Z& p8 |$ X' l5 @, l0 Icanvas.element.prototype._initelement = function(el) {
6 Q- s& ?- J. s3 P. rthis._oelement = document.getelementbyid(el);# B4 Y4 a+ |) g) y  ^/ H! u
this._ocontexttop = this._oelement.getcontext('2d');+ O3 V% E/ u4 T: @  E
};) V/ f1 ?+ V" x# S
canvas.element.prototype._initcustomevents = function() {: C! U6 @2 m$ _- g' ?( ^$ l
this.onrotatestart = new canvas.customevent('onrotatestart');
' y5 R2 U* r  h7 }3 othis.onrotatemove = new canvas.customevent('onrotatemove'); 9 v  A+ K; d2 p. A3 M
this.onrotatecomplete = new canvas.customevent('onrotatecomplete');, d* C+ [( L- @9 I- I7 ^
this.ondragstart = new canvas.customevent('ondragstart');
- C+ B/ w, C% f& C# |, jthis.ondragmove = new canvas.customevent('ondragmove');
. J* j" U/ a" t; g$ H8 Vthis.ondragcomplete = new canvas.customevent('ondragcomplete');
7 q4 X0 ]/ M* b% R};
/ k: P0 A0 s5 n/ Fcanvas.element.prototype._initconfig = function(oconfig) {* b: J6 A4 h8 a1 s! q) X' V8 E, M
this._oconfig = oconfig;6 A+ [  L, ]( R) L: W5 ~
this._oelement.width = this._oconfig.width;
1 X6 c/ \) o9 D! ythis._oelement.height = this._oconfig.height;
0 E6 Q$ U* n0 y$ Xthis._oelement.style.width = this._oconfig.width + 'px';" V3 J5 T$ O4 w
this._oelement.style.height = this._oconfig.height + 'px';
; m7 S: v9 a! i; c/ K6 U0 x5 z};, a0 M2 d- v4 D8 L6 e  w
canvas.element.prototype._initevents = function() {9 l6 n, D9 y7 {9 r
var _this=this;' f% B9 t& _# i; K+ M0 b1 c7 o" |
s(this._oelement).on('mousedown',function(e){
' V' L) [! u) B$ L) }1 L7 ]_this.onmousedown(e);; T; o6 D) n* F5 C$ N
});
! u/ j1 |4 u' F/ p: D* \- c0 vs(this._oelement).on( 'mouseup', function(e){* N( e: k8 X4 ^- Q' F
_this.onmouseup(e);- k9 o% U( A" }. e: d6 U
});4 g2 W/ F* ^) s6 b9 E9 I: b
s(this._oelement).on('mousemove', function(e){
1 s# |6 J3 F: e4 F: \) y$ G4 v_this.onmousemove(e);8 A8 U. S6 R4 Y* F
});' b6 L( ]6 T2 t" ^- p- \# Y
};
4 M- x. S5 ~+ F0 Z% |  T: M; hcanvas.element.prototype._createcontainer = function() {
+ r* X7 k" P6 B8 F& W/ Rvar canvasel = document.createelement('canvas');, v* L) n5 c( E+ x; i: Q
canvasel.id = this._oelement.id + '-canvas-container';! x; ^  U$ x3 e1 s% a. n
var ocontainer = this._oelement.parentnode.insertbefore(canvasel, this._oelement);/ C; e, v" I. S3 H5 a
ocontainer.width = this._oconfig.width;  o; k  \5 o' B4 Q' h
ocontainer.height = this._oconfig.height;
$ c( y$ ^. n8 c0 U8 S9 yocontainer.style.width = this._oconfig.width + 'px';
  M7 b4 |3 S) s7 J# i. r% Locontainer.style.height = this._oconfig.height + 'px';3 j7 U% e. ^% t2 m  w  a
this._ocontextcontainer = ocontainer.getcontext('2d'); . J  A- Z8 M  U# n
};
  Y& m1 H- `3 K. dcanvas.element.prototype._createcanvasbackground = function() {7 ?, y. L7 C5 Y" H; J
var canvasel = document.createelement('canvas');' q! ^- ^2 z$ A0 a& ^- t; _
canvasel.id = this._oelement.id + '-canvas-background';1 L  `, }( d+ ~" v; f$ F3 w- h
var obackground = this._oelement.parentnode.insertbefore(canvasel, this._oelement);% H* E4 x' R  W- r( O3 \
obackground.width = this._oconfig.width;
, s) ?4 ?7 S7 sobackground.height = this._oconfig.height;+ h: J, n" l. n
obackground.style.width = this._oconfig.width + 'px';) Q4 v& N2 k& k. [
obackground.style.height = this._oconfig.height + 'px';4 t  [" V8 h$ X9 u
this._ocontextbackground = obackground.getcontext('2d'); , R) Y& N) D& q2 c
};+ J. R. `/ r. x
canvas.element.prototype.setcanvasbackground = function(oimg) {2 Q2 l  ~4 X7 U6 i
this._backgroundimg = oimg;2 z5 x1 y  C+ p* k. a4 x
var originalimgsize = oimg.getoriginalsize();
) u. X  k" r9 ~# p2 W8 \this._ocontextbackground.drawimage(oimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);6 G# f; K5 ]" X% n2 d$ i# j( N6 w3 u
};
& L4 d1 C0 P' e, R$ g$ acanvas.element.prototype.onmouseup = function(e) {
- @% p9 v: v1 N# Fif (this._aimages == null) {
# f* ~' I8 j& y. W; f8 N$ Hreturn;
6 J+ R" ?. N7 S6 l. \8 Z}' y' n# P+ {0 G0 v( D% x6 K
if (this._currenttransform) {
% t" \% }: p. p1 |- N1 Dthis._currenttransform.target.setimagecoords();0 p$ o7 `, Y- l" ?
}) W* I6 Q/ H: O6 p
if (this._currenttransform != null && this._currenttransform.action == rotate) {
5 T& J: _) U% z+ p- B4 Nthis.onrotatecomplete.fire(e);. p) d( p! q0 ^# _
} else if (this._currenttransform != null && this._currenttransform.action == drag) {4 v$ v% s: c3 q7 ?, [
this.ondragcomplete.fire(e);# X2 c1 y3 o& D5 J
}$ K, j6 J$ `0 c8 M3 {) z9 `6 j; B
this._currenttransform = null;' _8 d$ n1 @+ y) Z4 u2 {0 N8 [9 k, o
this._groupselector = null;3 q+ Z* w, o4 {/ r
this.rendertop();
' }$ Z& z/ w& N% t3 r$ L};# ~1 Q# g$ L! k1 X) B
canvas.element.prototype.onmousedown = function(e) {
  |! }4 r. @4 x* \var mp = this.findmouseposition(e);/ W& Q/ g& M! E7 g4 D9 q) w
if (this._currenttransform != null || this._aimages == null) {: ^2 e# f" Q, h+ ~
return;9 K$ ?1 f3 B1 L2 [* ]) m- `3 z9 F
}
. f& Q6 \" `  ~$ ?+ `* jvar oimg = this.findtargetimage(mp, false);1 h' g: ]: D2 X6 V0 e. b+ F
if (!oimg) {7 V, ?/ m" E* G+ W! G6 J+ ?5 u+ [
this._groupselector = { ex: mp.ex, ey: mp.ey,' g: N1 B3 l$ Z1 O0 ~" N* l
top: 0, left: 0 };
1 P$ S2 k' r& g/ R2 k}: T2 @) N' _( e- P
else { 9 \# @$ N# `' G; o; A# V
var action = (!this.findtargetcorner(mp, oimg)) ? 'drag' : 'rotate';
8 z5 S2 L; a1 Qif (action == rotate) {
6 F( b3 [  e3 a4 dthis.onrotatemove.fire(e);
1 F; U0 u9 k" ]} else if (action == drag) {
8 M! r8 s9 G1 W7 d3 Gthis.ondragmove.fire(e);# r+ R: \  k8 b. {; D7 Y; Y* z
}2 {' j( D# o3 I+ q$ @
this._prevtransform=this._currenttransform = {
1 D6 R1 r! o- j' N# d* ztarget: oimg,6 T% ?* V, z( z& V2 x4 ^
action: action,+ m4 @4 k  _! h& m
scalex: oimg.scalex,
4 s- `3 h$ q' X& V, N6 K  U7 N. ]offsetx: mp.ex - oimg.left,
& J9 ]! m. `. v2 y$ q( ?offsety: mp.ey - oimg.top,
3 X" Q/ B$ I, c# ?# e$ r, jex: mp.ex, ey: mp.ey,' s$ K( F, u/ n/ y) J7 K
left: oimg.left, top: oimg.top,
) a7 n8 `# _8 W- h5 I4 P4 [theta: oimg.theta
( m0 }0 j/ R7 S" u$ B' r. m0 v* ^};
* Y( y8 y2 }) W) v+ X6 ~. p$('canvas_menu').style.transform='rotate('+oimg.theta*180/3.14+'deg)';9 Z: I2 P6 `2 T$ c9 F
$('canvas_menu').style.left=oimg.left+px;
( O+ g# q/ |  `# v8 A, h- J) N. T$('canvas_menu').style.top=oimg.top+px;
0 H/ [; X; m! {4 n4 \$('canvas_menu').style.display=block;, \$ w, W# x7 k: p/ H" I
this.renderall(false,false);
8 x  u6 _+ j8 K+ o9 G}: L$ M! U& c* _6 v4 f  o
};
0 c* f# G: k. s0 B9 p2 lcanvas.element.prototype.onmousemove = function(e) {7 b9 ~4 t  P$ m. A
var mp = this.findmouseposition(e);
/ M* G2 J' k) m; [" i7 ?, Bif (this._aimages == null) {9 W5 A, P8 P/ u6 v( k
return;* ~- k# `! M% r
}
7 q0 E: Z, l0 s0 U) R( b1 fif (this._groupselector != null) {
6 a8 {  \4 R% Y# Ethis._groupselector.left = mp.ex - this._groupselector.ex;
$ ~% T& W9 L& c1 U; ethis._groupselector.top = mp.ey - this._groupselector.ey;
1 q1 Z3 M1 z$ ~4 M" e% Ethis.rendertop();9 J+ Y; n2 {  h5 C: i6 Q8 l
}  P: P3 a6 X: d! r7 Q2 r- K# Q
else if (this._currenttransform == null) {
8 R! i! k0 k1 h. x" U& k& kvar targetimg = this.findtargetimage(mp, true);
% C0 Y- m0 O0 B- ^: I8 T) Bthis.setcursor(mp, targetimg);6 c' G( `! x1 w; r- m  P+ e+ T
}
& c. m+ B( P( v% {else {0 I. V2 @5 j4 X+ O6 ^
if (this._currenttransform.action == 'rotate') {5 f+ K% P+ C" W8 f
this.rotateimage(mp);
( ~# X9 @! q7 n# ?this.scaleimage(mp);
' e9 c0 ]4 |3 U% B! S- lthis.onrotatemove.fire(e);
2 y  d) Z4 G; x2 P, x4 U% U; r2 J} ( S* H: {# u  |6 V, d
else {
2 G. @& W0 F* f( F& b. ^this.translateimage(mp);
! K( G7 R; L; w4 Q8 l1 `  z1 Ethis.ondragmove.fire(e);+ q0 \% Q' k, I; b
}: W$ l; G' [; ^8 }5 m
this.rendertop();% L/ s' Y& {0 q/ @6 j% n, |
} ' f% A. m+ @6 T4 L- G& |% Z- g
};
# q5 W: Y  ^: b+ mcanvas.element.prototype.translateimage = function(mp) {; x# R( @7 e: O/ ~
this._currenttransform.target.left = mp.ex - this._currenttransform.offsetx;
( @" O  B; c' ^7 }0 i2 M2 ]. ithis._currenttransform.target.top = mp.ey - this._currenttransform.offsety;% z/ _2 |9 N7 U. e" L. U; j, ~" ~
$('canvas_menu').style.left=this._currenttransform.target.left+px;9 J1 m( R" {) g5 Q. f' n! `6 s
$('canvas_menu').style.top=this._currenttransform.target.top +px;( Z5 |  F$ v* G  V% v6 x* R
};
( a9 P& m. }5 H4 ]7 pcanvas.element.prototype.scaleimage = function(mp) {7 x- s8 J" m8 B
var lastlen = ' C* Z4 T& T, o% F1 G
math.sqrt(math.pow(this._currenttransform.ey - this._currenttransform.top, 2) +$ P# _+ N7 H3 s6 O
math.pow(this._currenttransform.ex - this._currenttransform.left, 2));
% t6 l: P. H5 P# Rvar curlen =
) S1 [! K1 M" Y7 A( Emath.sqrt(math.pow(mp.ey - this._currenttransform.top, 2) +% X! K8 F9 V3 q/ M/ W( Z/ g- z! }9 g
math.pow(mp.ex - this._currenttransform.left, 2));$ y$ _- c' N5 C  {
var curscalex= this._currenttransform.scalex * (curlen / lastlen);- Y5 r6 A4 S- x5 n  P
var curscaley=this._currenttransform.target.scalex;; G% N, e3 z: ?
if(curscalex>0.7&&curscaley>0.7){- \5 S  Q" ~- [! O
this._currenttransform.target.scalex =curscalex;
# u( C' [3 [7 y( j6 Hthis._currenttransform.target.scaley = curscaley;
, B- S0 r4 [! ^4 C( K6 s}" [7 E" ]! e! ^3 o& e
};
3 i) q- F- o1 a1 V' acanvas.element.prototype.rotateimage = function(mp) {
9 h* ^+ _9 |% G& t5 Avar lastangle = math.atan2(
1 R) ]% m. _/ X7 f3 J  h6 D3 Vthis._currenttransform.ey - this._currenttransform.top,& B1 f; s3 h4 @" I" u! I
this._currenttransform.ex - this._currenttransform.left
- i: i) w/ |: U$ u+ E);! A9 `0 m0 D' L9 c" W
var curangle = math.atan2(7 S9 b: A' x3 z# B: n- Z3 K* E/ W
mp.ey - this._currenttransform.top,+ S8 p; L) F, ?# s  X% z
mp.ex - this._currenttransform.left
9 \; z% M6 o: {- D2 {, V);
' E& h% X9 _+ m2 B7 [this._currenttransform.target.theta = (curangle - lastangle) + this._currenttransform.theta;
! `& Y  g1 O' L1 G2 B: q& rthis.curangle=this._currenttransform.target.theta*180/3.14;
+ ^: P. u- `. W! p0 I3 Q7 E$('canvas_menu').style.transform='rotate('+this.curangle+'deg)';3 V" ]- o8 n8 y0 T) w
};
) E: C5 Z$ S) t) g6 Xcanvas.element.prototype.setcursor = function(mp, targetimg) {
( a8 _6 ?0 j% N# [  fif (!targetimg) {
: c+ E; Z6 [* j& @" S0 jthis._oelement.style.cursor = 'default';
6 T) V, w& e. c+ Q, P' \# E}. G6 H- A6 L8 J$ c# H9 p
else {
, V) R- _( o# g! Rvar corner = this.findtargetcorner(mp, targetimg);% X5 }. v7 l2 t9 Y
if (!corner) 2 g% D, s; ^/ C7 z; o. T5 R" A
{
% }5 Y2 L  P$ `$ @  Othis._oelement.style.cursor = 'default';
2 D/ K' m) Z) U4 X3 v" Y. v, I}
% d0 r9 O. W# b7 U9 ?' R: kelse
) ?4 ]/ v  @) D% u4 y9 V{  ~8 V: m& m' f
if(corner == 'tr') {8 u6 e7 a. j& ]# {' x0 Y/ d
this._oelement.style.cursor = 'ne-resize';
+ s4 Z( R* v' U9 v% R' q}6 `) `/ P3 q( i
else if(corner == 'br') {0 a4 u4 F' ]. A4 y* z# n
this._oelement.style.cursor = 'se-resize';
. S; @+ D3 Z! r# T: D}& t0 N$ \! m. a' ?
else if(corner == 'bl') {
* X2 L6 {1 h; p% \9 M. Nthis._oelement.style.cursor = 'sw-resize';
6 s3 ^) Z( M; ?) }3 l}1 b5 y1 D: F1 q, T( o
else if(corner == 'tl') {
& U) r2 n; u) d7 Kthis._oelement.style.cursor = 'nw-resize';# e9 K; ~5 ~/ R: R! X
} $ t3 @8 x' u1 Y# U) q
else {
7 x: N/ u7 e8 Q% Bthis._oelement.style.cursor = 'default';" {- a$ N2 ~( d+ `% E
}
) |/ S4 _6 i8 l/ W}
- _  U( H3 g  k" n$ @}% k% s5 E' p5 B, u) w3 f
};. ]6 U0 o; ^6 B' \7 }: v
canvas.element.prototype.addimage = function(oimg) {
0 E; j$ z* p  S3 q2 Qif(s.isemptyobject(this._aimages)) {6 x1 \& `) _" m8 F' x3 n- A
this._aimages = [];3 J0 f  M( I, U
}
6 i3 f$ B- _7 Zthis._aimages.push(oimg);+ J( J+ D( c# m1 n* f
this.renderall(false,true);</p> <p> };
$ B5 O1 t& G3 }! ?; m2 \canvas.element.prototype.renderall = function(allontop,allowcorners) {
7 ~9 T0 [& A8 b* pvar containercanvas = (allontop) ? this._ocontexttop : this._ocontextcontainer;( F1 r6 C, m; |6 I
this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
9 a4 G7 E1 L- ]/ `* K& ncontainercanvas.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
0 N) u+ S, w2 W% ^if (allontop) {9 V/ h( Y- |1 n( W/ w' t
var originalimgsize = this._backgroundimg.getoriginalsize();
8 j6 B3 t; k) e, kthis._ocontexttop.drawimage(this._backgroundimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);
5 T$ H1 h0 r1 ?' F. I9 _( P}
  V& R3 a- a5 b' F& gfor (var i = 0, l = this._aimages.length-1; i < l; i += 1) {  c$ {* a* c& `3 V$ M0 ]' \8 U
this.drawimageelement(containercanvas, this._aimages,allowcorners);
* G' a6 M# D. ~5 V! O! v}# v" e0 X1 |# ?7 r
this.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],allowcorners);' m! T; s" Y" h  W9 g
};
" I. i- T0 I% Q% u( C# P& K0 tcanvas.element.prototype.rendertop = function() {' ?1 i4 [7 M' x* ]
this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));( m1 t+ p2 R# v7 u1 K
this.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],true);
% T. n8 R! c8 aif (this._groupselector != null) {; w3 \7 z# y+ ^% W) T# W' ~
this._ocontexttop.fillstyle = rgba(0, 0, 200, 0.5);
# _; ^* V) L8 }: h, h' C2 xthis._ocontexttop.fillrect(6 W! Q. Z# P2 {( [
this._groupselector.ex - ((this._groupselector.left > 0) ?
1 _2 Y* J+ r* L0 : - this._groupselector.left),
$ r) q4 R- a% r: D8 N" t, fthis._groupselector.ey - ((this._groupselector.top > 0) ? % j- L$ ~7 M$ V6 R
0 : - this._groupselector.top), $ y# w- W0 q4 i3 ?
math.abs(this._groupselector.left), 3 D6 c6 S2 a( [0 u: z6 v$ s
math.abs(this._groupselector.top)/ i" X/ G; c8 \* T1 @- W
);9 T- M: z; Z+ M1 r7 g8 Q" L2 Y3 F
this._ocontexttop.strokerect(
* n0 \: T4 J* z7 ~8 }1 t& ]4 ^this._groupselector.ex - ((this._groupselector.left > 0) ?
7 W' [- K( X8 c. Q0 : math.abs(this._groupselector.left)), / D8 t; x% N) L" `2 Q
this._groupselector.ey - ((this._groupselector.top > 0) ? 3 u9 E& U9 o5 ?! X4 F' z
0 : math.abs(this._groupselector.top)),
/ X) Z8 H' V/ E' h/ Z% I9 J5 a2 A1 wmath.abs(this._groupselector.left),
# x5 C! }7 Y7 k( x% k* e8 cmath.abs(this._groupselector.top)) y8 D: W& C5 |
);
6 m2 u4 }: [/ [  R; W}
4 O- ^+ D, _/ q! t( Y$ K* e};2 m3 [! _6 x, q  s) v3 Y8 ]
canvas.element.prototype.drawimageelement = function(context, oimg,allowcorners) {4 ]& T+ e& s. X1 r: ^
oimg.cornervisibility=allowcorners;) U1 O+ y8 _! T! j, i* i# ?
var offsety = oimg.height / 2;
  T6 L0 i7 P5 \6 l7 Mvar offsetx = oimg.width / 2;
7 B( O: u& h6 \7 p1 N1 L/ ?context.save();# G; {' ]9 C* K7 C  S( y2 d
context.translate(oimg.left, oimg.top);
! K2 R1 r! X+ B. zcontext.rotate(oimg.theta);
+ r; o/ Q3 Q) W, ucontext.scale(oimg.scalex, oimg.scaley);) A3 T% ?! r" I2 Z" ]
this.drawborder(context, oimg, offsetx, offsety);
3 |0 _. b- V) C+ h0 dvar originalimgsize = oimg.getoriginalsize();! b2 L7 V3 r. b2 g/ q* b$ _+ G
var polaroidheight = ((oimg.height - originalimgsize.height) - (oimg.width - originalimgsize.width))/2;
: y7 R+ ?: ]  X+ `: c6 Wcontext.drawimage(( j% p" n4 h( v
oimg._oelement, 9 y5 C' H; C" s! @" i
- originalimgsize.width/2, ) t) F8 a# s# Y4 N) S/ B. a. {
((- originalimgsize.height)/2 - polaroidheight),
0 t: C1 b4 ^; b" coriginalimgsize.width, : T8 W7 N- C  ~9 ?+ t* [- i  h
originalimgsize.height
) d" S2 F1 k* q& p. Y);
0 w8 n+ b% D; Eif (oimg.cornervisibility) {8 W% w6 E/ b9 I' Z: f
this.drawcorners(context, oimg, offsetx, offsety);
' N( X) f1 J4 ]3 M; N0 @+ E) q0 H}
. I& C/ N$ y2 p- R1 i- Mcontext.restore();3 Y! c/ q: G2 i
};6 j% _3 W* |# L0 K- c" d/ R9 F
canvas.element.prototype._getimagelines = function(ocoords) {6 Y$ Q2 Y/ I& @& I1 j
return {
2 I0 D( D) e+ dtopline: { - g! G% @2 F/ y6 W9 o
o: ocoords.tl,/ N& r* B* L# X
d: ocoords.tr . x* G% ~" @/ z
},
2 A" n2 c4 ~; L# zrightline: {
6 }. J: [$ X5 u, A/ mo: ocoords.tr,2 e9 e' J0 b% R8 V8 F% H$ B4 I
d: ocoords.br
# K4 p6 d0 ^% f},
/ E+ _% ~4 q! xbottomline: {
- x9 P, h8 W6 k3 R# Co: ocoords.br,
' x2 v2 g% I: _/ c' Zd: ocoords.bl * c8 q% ~, x; Z1 e; ^6 Q0 [# ~: q
},
: V2 c: t1 ~( b6 B  Y0 h! lleftline: { ! z  `. q* t$ _; K0 u! @
o: ocoords.bl,
8 H6 H- {! Z8 s6 y0 Kd: ocoords.tl $ |2 n. V5 M; }- B, E
}; ~" R- i) f7 l# W; ]
};; ?- M7 S3 d( j& t' E* |+ O% A( E
};! e$ J  Q+ x! ?3 o. X2 K3 F
canvas.element.prototype.findtargetimage = function(mp, hovering) {" A- q" y5 f- n* ~8 {+ _
for (var i = this._aimages.length-1; i >= 0; i -= 1) {: [: }: @9 I! ^( c
var ilines = this._getimagelines(this._aimages.ocoords);
) S  k  f) p) o6 U, P) a9 [4 E" yvar xpoints = this._findcrosspoints(mp, ilines);
# O* {+ n& E1 l0 ^if (xpoints % 2 == 1 && xpoints != 0) {
3 h7 q2 L$ l0 y% yvar target = this._aimages;
  D) |$ K* Q$ H! V& `if (!hovering) {) G, |% J" O% C* q* Q
this._aimages.splice(i, 1);9 v+ @/ A% F2 y& n; `/ T
this._aimages.push(target);
" D% h6 n$ L, k% C  }+ t( }+ J}& Y" @* R) C/ c" W
return target;
6 }1 ^3 q9 y4 q/ Q}
. ^# `. F' N* H* T! A2 x! r# v, F" C}
% R; p7 O. W# P9 Greturn false;/ E/ K9 @3 n; O7 H7 N. M2 N) m
};
0 N0 u, _+ t2 X! k" tcanvas.element.prototype._findcrosspoints = function(mp, ocoords) {
3 O- n$ d" `' A2 Z0 Jvar b1, b2, a1, a2, xi, yi;* Z) Y; t/ W( l, G* w( ?
var xcount = 0;
" F/ k6 k" @# e5 |- C: u; b9 U  E/ Pvar iline = null;
; F) z. c! m6 D  ^# xfor (linekey in ocoords) {0 j, q6 a! l& ?0 v) ^% j2 U
iline = ocoords[linekey];
3 r$ G( |8 ~: R& I6 E, {9 x5 R: vif ((iline.o.y < mp.ey) && (iline.d.y < mp.ey)) {
) a0 T$ l5 w  Q) i( |continue;
7 U0 m# R- p% {2 }, {* o8 \}
+ `* z! {/ G! T" R- G/ {! L4 ?if ((iline.o.y >= mp.ey) && (iline.d.y >= mp.ey)) {
4 o. [7 v% \( R+ E! X  {continue;
1 q% c$ y8 q7 t7 @' V1 B0 f! j/ f}7 F2 s  o7 \- k) m+ I) l9 L  W1 A* _
if ((iline.o.x == iline.d.x) && (iline.o.x >= mp.ex)) { / S- i) j2 k! t$ ]5 a9 f, s4 E
xi = iline.o.x;
/ D' I, m. }% \, |3 ]0 G4 kyi = mp.ey;
8 n& r$ B" @. K, p8 q}
9 \! \  B; R; }else {
! q5 A4 h! ^  i" O* a$ w# ib1 = 0; + f4 b2 c7 l" Z' |4 c0 `
b2 = (iline.d.y-iline.o.y)/(iline.d.x-iline.o.x); " A% R1 X; D2 m' Z
a1 = mp.ey-b1*mp.ex;- t5 P8 g+ o: G: W
a2 = iline.o.y-b2*iline.o.x;3 M+ M) t6 f6 q5 i
xi = - (a1-a2)/(b1-b2);
- W; c- Y5 B9 Q7 |yi = a1+b1*xi; , l+ f! {) ?9 g* R7 b
}
' I# D3 C4 l# s7 o+ Lif (xi >= mp.ex) { 7 B/ S) ?5 j4 M6 H: q
xcount += 1;
  x4 b: m2 q& n  j' G6 X4 ~}
8 d' I% h. L% G# h5 }if (xcount == 2) {5 v# S) e% K: E) p* X/ P
break;
- b; V! f. H2 d) f}
' }3 g, ^( y* t8 h  O5 ?. X9 Y4 ]8 q}5 z* Y, a. E  q3 ?* C1 ^
return xcount;
2 l  P& |9 }; m$ }7 D' q3 c! h. ^};
; a8 {2 P5 W0 m  c6 I: Vcanvas.element.prototype.findtargetcorner = function(mp, oimg) {
. L- i2 n& U$ w" v/ |var xpoints = null;  }4 ~- m1 t# u  I0 |4 Q  m. w
var corners = ['tl','tr','br','bl'];8 d8 C2 r( ~" q! Q& d/ E/ m" H
for (var i in oimg.ocoords) {
9 N! F0 W( |1 Q0 j5 b% Kxpoints = this._findcrosspoints(mp, this._getimagelines(oimg.ocoords.corner));$ N% v3 `+ x) N, j
if (xpoints % 2 == 1 && xpoints != 0) {! J) \0 q2 t( A* |5 ~( }
return i;; ~4 z5 e9 @% U* K, K
} . P9 e4 {0 I0 H7 {# Q1 W
}4 \, u4 t8 t- S
return false;
7 v$ @0 J8 s6 M4 J+ g: `};# i2 H( ?, [$ H3 b
canvas.element.prototype.findmouseposition = function(e) {" y7 g( g3 r/ S, @6 F, c
var parentnode = (e.srcelement) ? e.srcelement.parentnode : e.target.parentnode;9 F8 ]0 k* T& @9 t, w
var issafari2 = !s.support.ie&&!s.support.firefox;. A* z4 {2 a) Y9 J5 v
var scrollleft = document.documentelement.scrollleft || document.body.scrollleft;' S/ q1 ?) Z3 Y; u; l2 l
var scrolltop = document.documentelement.scrolltop || document.body.scrolltop;0 P- c% D4 t; B6 v8 H) ]' l
var safarioffsetleft = (issafari2) ? e.target.ownerdocument.body.offsetleft + scrollleft : 0;! W% N! ]& i- u* w
var safarioffsettop = (issafari2) ? e.target.ownerdocument.body.offsettop + scrolltop : 0;
6 b6 \+ B8 s: M3 a" n; [return {
  j; r- b6 p7 G) iex: e.clientx + scrollleft - parentnode.offsetleft - safarioffsetleft,; q& |. A3 a8 y4 h  d! u
ey: e.clienty + scrolltop - parentnode.offsettop - safarioffsettop,  p2 v  M- |' s; t. e
screenx: e.screenx,- c1 J+ M. y4 W! {& c' E% |
screeny: e.screeny, a8 `3 B- }; h6 ]
};( k& r5 n0 n1 v8 X; _/ V- i& A
};/ g" H' i5 L, z/ z) Q
canvas.element.prototype.drawborder = function(context, oimg, offsetx, offsety) {+ ^% P" T: Y5 M' s- x
var outlinewidth = 2;
' i! `0 I9 d, ^9 zcontext.fillstyle = 'rgba(0, 0, 0, .3)';" k- E; |  ~9 W  G3 s! X: A% o0 d% y
context.fillrect(-2 - offsetx, -2 - offsety, oimg.width + (2 * outlinewidth), oimg.height + (2 * outlinewidth));- u5 W% D" u9 J4 `) t; C) @
context.fillstyle = '#fff';
4 c% h7 v$ g1 y3 {% @5 c9 Y( }context.fillrect(-offsetx, -offsety, oimg.width, oimg.height);$ `: y% x9 D0 M6 l) g4 A6 ^" G0 x; A
};
8 u; R# k0 A2 ^3 A! kcanvas.element.prototype.drawcorners = function(context, oimg, offsetx, offsety) {  ]+ p1 {! \; m) p  d
context.fillstyle = rgba(0, 200, 50, 0.5);
# P* a( b" T+ r$ k. gcontext.fillrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);9 P  V" [* B" w. n
context.fillrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);
- ]" E8 G3 a  ocontext.fillrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
4 w% C1 r8 x% j  ~context.fillrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);" l. l( g1 }$ ^4 _- C% m% r
};
' K0 o$ g) p+ E/ y. e5 A+ @. ecanvas.element.prototype.clearcorners = function(context, oimg, offsetx, offsety) {
6 ~5 h& F2 B3 |* e3 ncontext.clearrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
* u/ D5 G! o% ^context.clearrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);
! D: H; W+ Z, [* Dcontext.clearrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);; v6 K% [% {) V: `, g
context.clearrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);7 l3 E7 h$ j+ l" v- t& v
context.restore();& b2 X- X5 I3 S, Z+ O3 Q$ _; e4 @
};4 S2 Z1 w* l5 ~' G$ K0 J& J, L
canvas.element.prototype.canvasto = function(format) {; T3 E) b, ?) W: C7 T; h# \4 j
this.renderall(true,false);5 j3 k, X! J% v0 d! p5 \
var containercanvas =this._ocontexttop;  `: ]1 G8 E8 g; e' P  L2 G
for (var i = 0, l = this._aimages.length; i < l; i += 1) {0 a% I2 x6 I' C& l; v" r
var offsety = this._aimages.height / 2;1 E( s) R, i5 A8 `3 q1 S% B( F
var offsetx = this._aimages.width / 2;! y* _- N- V8 |% f: G' z' P
this.clearcorners(containercanvas, this._aimages, offsetx, offsety);
5 q, i3 O0 k0 [% [& o}. I5 B( [( L! m' `; F' P$ `
if (format == 'jpeg' || format == 'png') {7 t3 c# r. Q3 ]" R: H0 _9 G
return this._oelement.todataurl('image/'+format);# i1 g3 q6 m- ~2 u! l. V1 q/ x
}
+ o3 D% U# X" {! R8 b7 b- B}; * r2 E  a1 B. z; L9 c
canvas.customevent = function(type) {" X$ c( B1 I6 M1 f+ p
this.type = type;9 E4 |( W6 M  m3 k
this.scope = null;+ {7 D7 A3 Z7 S; @
this.handler = null;
6 O4 i  w3 M' G# Xvar self = this;) [. i5 z& Z0 B3 K0 O
this.fire = function(e) {
, ]  J- G/ g! {* E0 C# _if(this.handler != null) {
% e. S! u& n% t2 |  G; Cself.handler.call(self.scope, e);
! t% M: R5 x3 i( d6 X2 q$ w}
+ }1 z, h7 U+ t0 u# J};
  Z8 |  I. S7 |6 A! ^7 `};
6 |  S* v! Z4 ], i  z3 e8 c}());
- @1 P/ F9 @! sreturn canvas;/ A) v8 y1 T  x
});- y  Z5 v: `% }! |
canvasimg.js代码如下:$ a$ T; ~, W4 l, o
复制代码代码如下:define('canvasimg', [ '../multi_upload/core' ], function(s) {
- p/ T, B$ |2 B" X! `4 |" Rvar canvas = window.canvas || {};+ V& Z# z) P4 E+ O$ B! c
(function () {2 C$ q3 ?& n* v+ Z8 e
canvas.img = function(el, oconfig) {
* a0 {1 T  t6 e- T; c& x* u5 X/ uthis._initelement(el);
9 C) @" _: d. Qthis._initconfig(oconfig);8 T4 L" Z/ g" m
this.setimagecoords();
& h" |- ?8 ]" ]/ p9 H9 H3 b};
' M* y* L( a6 |& [/ Jcanvas.img.css_canvas = canvas-img;
' c4 ?$ c3 f0 Evar default_config = { + J( S& O0 A2 b) V
top: {
  g6 O6 |# U9 ^* L& ?$ ^5 ]4 hkey: top,
( @) Q9 n+ v- q* p3 {+ X0 Evalue: 10
9 G8 ~" ?  H7 S% e  B& A},
- F2 {+ E) R! N9 @  e+ gleft: { % n$ c2 I6 h: D& F) ^0 J) l
key: left,
$ V/ o' Z8 c" V1 z" s0 jvalue: 10
1 a1 ?2 \7 V& n7 I9 \( s5 m}, 2 L, @1 V. p; A% _! a
angle: {
0 k2 M$ U0 O& X9 E9 q6 J# O0 Skey: angle,
6 H4 r+ i8 Z0 A7 z! Vvalue: 0
: T% J) ~. I4 V7 v, }},+ Y2 o+ f- P4 N
theta: { ; Z1 m  z- n* W/ I( G) b4 ^6 T* v
key: theta, " z5 U# w- t6 u. d- Z: i# ?: y9 T  H
value: 0
( P# j; u! U6 Z( `6 C},
8 G* L6 _2 k3 e7 Y. o) N+ \% kscale-x: { ( i& {2 U8 A7 C3 ~/ j" u- M( k' i: f
key: scalex, . ?- e7 Q( l4 z0 Y
value: 17 }! Z4 P" Y5 a$ C- q
},* B% \/ p& a( B: O& J# e$ U( I& Y  H
scale-y: {
0 A2 d8 i9 ?0 tkey: scaley, 2 G" A* e4 y2 k8 T* w9 _1 T
value: 1
  z8 A2 d( r& d# |% X" |3 k' U+ C},* X( x) y* v  t0 o, _8 D
cornersize: { 3 A/ E; d: |, Y
key: cornersize,
! {  c, f3 n- y! q# W; `+ e+ M" Jvalue:10
" R+ a. g+ Q9 o. o7 e5 T},; L6 n. F7 T0 X& l2 ]+ o
borderwidth: { 2 i. i( T3 d* O- h% `
key: borderwidth, # X, @3 u7 j; M2 F8 Q0 d1 j- p2 j" j
value: 10
6 e6 J# M6 y7 ~2 o' ]},
$ z% c9 k/ S: r# k. D5 E2 B' Z6 Vpolaroidheight: {# h2 V) u* n4 S) s' I/ c
key: polaroidheight,
( p! j( y, [9 q) k) @# Vvalue: 40
) C/ ~( W# L6 \. _1 S2 K3 k) c},( m- i: u2 T$ P- F! k' A
randomposition: {
3 o/ [% ]3 z% t% dkey: randomposition,
' H' B  f" L  m- y0 |: I# hvalue: true. O' ?* e7 v) x
}
# |% k( C: J3 z};: N5 t" h0 ?. T9 y' s* P  ~; F
canvas.img.prototype._oelement = null;8 ^- V( L/ ]# r6 h( ^. Y  w
canvas.img.prototype.top = null;: G* O! S7 @% @3 N% N
canvas.img.prototype.left = null;0 z9 _+ U. I& l+ I1 d: y2 o! b
canvas.img.prototype.maxwidth = null;
8 U# A7 N8 H2 B) ^1 \6 Scanvas.img.prototype.maxheight = null;
4 K* R/ a. r  u( wcanvas.img.prototype.ocoords = null;
  L% w( j, z- Q  r+ H0 Lcanvas.img.prototype.angle = null;  P# ~2 X! C: ^; C1 X. G
canvas.img.prototype.theta = null;( h/ g6 x2 ~3 y$ H* ?' m% J0 a
canvas.img.prototype.scalex = null;. t" G* s8 \0 Y6 c6 W. N
canvas.img.prototype.scaley = null;  M( B' U7 R) S4 [
canvas.img.prototype.cornersize = null;
: \& [% e- V  [canvas.img.prototype.polaroidheight = null;
4 q2 M! T' o9 g) S. Y+ v" ycanvas.img.prototype.randomposition = null;
9 a9 c* @  ^3 ]0 u" Ucanvas.img.prototype.selected = false;
2 P! e& L4 N  r2 q* t3 \canvas.img.prototype.bordervisibility = false;
' V7 z* w, o% ncanvas.img.prototype.cornervisibility = false;
. Z. c1 ^( V/ {; n7 b, s5 Z, {canvas.img.prototype._initelement = function(el) {
" a5 W! a  i8 J9 m0 e1 H( j" ^this._oelement = el;
; A* [6 n+ C' k};
: @8 s& S7 M: X# W; R8 E4 J  Kcanvas.img.prototype._initconfig = function(oconfig) {
7 M* l+ v& w5 I% V" Avar skey;
1 C# ~. F/ X! ?# _- a/ `- tfor (skey in default_config) {) B9 B& h2 V6 C  |; w7 O6 I
var defaultkey = default_config[skey].key;
  c9 v. Z$ N. e" eif (!oconfig.hasownproperty(defaultkey)) { // = !(defaultkey in oconfig)
6 i7 E: k# J2 d. B  T% Sthis[defaultkey] = default_config[skey].value;, L3 P! [1 G5 Z2 L- }) @& C( L
}
) }. d- r5 ]9 belse {4 O; n2 Y8 N% Y. T" o3 s7 {. ?- u' E
this[defaultkey] = oconfig[defaultkey];
* B' G: H6 g* W0 p" Y8 U  E}4 u4 [& U6 a; p: C# g4 C
}
0 X, e) P" j" Z3 ^7 S/ Dif (this.bordervisibility) {
& Z9 V+ i1 R4 r8 b5 T4 Mthis.currentborder = this.borderwidth;
% Q2 y& q0 r# ]$ d9 `2 g}
* C4 e  L7 W/ Eelse {
+ X5 y% B: {# [! hthis.currentborder = 0;
! h7 G3 y$ U8 u( W' X. e" K}
) ^& D; x: B( H( X5 Pvar normalizedsize = this.getnormalizedsize(this._oelement, parseint(oconfig.maxwidth), parseint(oconfig.maxheight));; y/ v/ k# \: k# A- _0 @
this._oelement.width = normalizedsize.width;
* x. N) \1 k' D/ Fthis._oelement.height = normalizedsize.height;
$ G# e( q' v: p$ m3 A* hthis.width = normalizedsize.width + (2 * this.currentborder);
1 F, L0 @+ V  d' u2 z, athis.height = normalizedsize.height + (2 * this.currentborder);/ F6 Z* I, R# ]) C6 O
if (this.randomposition) {5 v+ h5 F7 a/ k" a
this._setrandomproperties(oconfig);
1 ~# K9 n; [, H}
. n, `$ y: e, N2 I. Kthis.theta = this.angle * (math.pi/180);
! H6 [7 ?1 V# ?3 F& @: r1 N2 i};4 D/ E/ J! u; }, e1 c* M
canvas.img.prototype.getnormalizedsize = function(oimg, maxwidth, maxheight) {
, [- n, Q' v6 B- Pif (maxheight && maxwidth && (oimg.width > oimg.height && (oimg.width / oimg.height) < (maxwidth / maxheight))) {
, L1 u2 W5 P; |. _normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);
4 k& o# Y: `( G( }$ ~. z# G, Nnormalizedheight = maxheight;6 w( ]  A7 V, a: Q" ?/ W: C
}
% o1 @. Y9 Z* H7 b/ Xelse if (maxheight && ((oimg.height == oimg.width) || (oimg.height > oimg.width) || (oimg.height > maxheight))) {' {& L5 N" s! P/ u
normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);8 l, C, M* S: J- P0 {
normalizedheight = maxheight;
( t- H+ N0 l" z0 b( n9 W5 b# _}
  k& @/ \$ m0 zelse if (maxwidth && (maxwidth < oimg.width)){
: ~+ @) U! F" Z( n. R4 p+ Hnormalizedheight = math.floor((oimg.height * maxwidth) / oimg.width);: v  S  Z! o5 V1 D4 h3 z
normalizedwidth = maxwidth;
3 `3 ^) N; `, Y0 z1 i}  c* [% O% n& n( F. M: E! G
else {5 \$ \+ H4 ]- U9 G3 j
normalizedwidth = oimg.width;$ U' ~4 J  P5 D9 e
normalizedheight = oimg.height; 1 p% C; `- y  K6 p
}
) O& \0 t/ Y2 j" |. b5 Z3 Breturn { width: normalizedwidth, height: normalizedheight }
( ^0 d3 J1 ~( |( V* g},) T5 }+ ]4 g9 _9 Z! g4 |. h' W
canvas.img.prototype.getoriginalsize = function() {
) n4 Z4 g! f. J& |9 f: T; Ereturn { width: this._oelement.width, height: this._oelement.height }) p/ ]: o" e" h2 X+ o5 `
};
/ v% K; J" B/ d# K9 }canvas.img.prototype._setrandomproperties = function(oconfig) {
% n+ R: w2 `2 rif (oconfig.angle == null) {5 i" z) c4 z1 O! T: e6 p4 `
this.angle = (math.random() * 90);
# o% H$ x! E, f4 A% a! o}
9 K1 u" Z0 a7 N3 i5 m; f1 s9 Aif (oconfig.top == null) {
$ z3 z/ J0 @2 P) y! @$ Tthis.top = this.height / 2 + math.random() * 450;& G4 B! V2 g, ^, l
}
- z* N$ b& q# ~2 S! f  D$ B. u( _$ _if (oconfig.left == null) {
  e* Y) ?) \% n: k9 `this.left = this.width / 2 + math.random() * 600;
1 t; e) p& |! x8 |; {5 J, v}+ `7 r$ {$ t* g8 ?
};0 `& Y  U2 C2 x. Z. |
canvas.img.prototype.setcornersvisibility = function(visible) {& v: D6 B+ L4 `, o$ b# y& h
this.cornervisibility = visible;3 `# K2 y' O! R2 C3 q
};3 l6 q2 ~  x& K6 Q/ D0 v
canvas.img.prototype.setimagecoords = function() {
, p! u$ V' d: ^: Z) e  Dthis.left = parseint(this.left);- T- u% V8 [4 M
this.top = parseint(this.top);
/ D0 X" D, v6 |1 I/ J/ `& |' ethis.currentwidth = parseint(this.width) * this.scalex;
- F$ h; r! r7 c# R. K7 R8 x4 Ethis.currentheight = parseint(this.height) * this.scalex;
$ h$ ^: x) D4 @this._hypotenuse = math.sqrt(math.pow(this.currentwidth / 2, 2) + math.pow(this.currentheight / 2, 2));
+ ~7 J. Q# g" A6 gthis._angle = math.atan(this.currentheight / this.currentwidth);
$ \2 ]$ r. t/ Vvar offsetx = math.cos(this._angle + this.theta) * this._hypotenuse;  q5 }+ B& ?$ d( M
var offsety = math.sin(this._angle + this.theta) * this._hypotenuse;( p3 Y* ~+ }' O" G+ d/ M. g
var theta = this.theta;
+ I" g% z# S" j9 q0 G- B0 dvar sinth = math.sin(theta);, k* L" e( }) ]- l& r) w  D, Q
var costh = math.cos(theta);
/ E" F" C) J1 F! C+ |( K! ^var tl = {
9 y) x+ B1 S& Ox: this.left - offsetx,! S8 d3 J0 h: U. K1 t. Y
y: this.top - offsety1 W3 e. d' h% j& Z' i  A0 e1 c/ U
};3 V+ s2 f  }( G5 W7 ^
var tr = {
& I% k) j0 S9 @1 ^0 @6 Wx: tl.x + (this.currentwidth * costh),
$ N$ j& s+ V+ s8 o& N1 Yy: tl.y + (this.currentwidth * sinth)+ `. {6 ^$ e  J% o
};
4 Z2 T: }% ^" l1 y; F7 F# g1 t$ ^var br = {2 s2 w/ e) \" O# a, f' l2 x
x: tr.x - (this.currentheight * sinth),
: N' B: n) P, M7 A3 S. Zy: tr.y + (this.currentheight * costh)
) X7 E. m/ b- d  j2 f: H  D  e8 P! m& O2 j};
. m+ Q  l# `9 G/ ~$ Tvar bl = {/ {* w1 O: {  j5 V; W5 S; C
x: tl.x - (this.currentheight * sinth),  Y' O7 Z: G. p7 x! M# p' M$ |
y: tl.y + (this.currentheight * costh)
$ N6 @. i$ ?7 [};
: |: W0 Z$ U$ _0 K! X6 gthis.ocoords = { tl: tl, tr: tr, br: br, bl: bl };
6 ^+ j8 `" ?) Bthis.setcornercoords(); / I9 _8 f1 T4 o* m: R: l
};
; `# h/ Z1 w3 F- Acanvas.img.prototype.setcornercoords = function() {, W+ f% s! D; x" x2 W
var coords = this.ocoords;
7 s8 E% ?2 @* @, W+ k" }5 X* K  lvar theta = this.theta;8 I5 d+ F0 I# W
var cosoffset = this.cornersize * this.scalex * math.cos(theta);
# o1 W. F6 q) M( f" r, A3 H2 u% ]var sinoffset = this.cornersize * this.scalex * math.sin(theta);& c( n$ {9 L; L! A/ f& d+ d6 X
coords.tl.corner = {" _: C) n1 T! X1 x8 g3 w
tl: {5 d( G! R" ]! F6 {
x: coords.tl.x,
* ]$ f) _# K2 ?. z+ Y: c+ J! T7 Xy: coords.tl.y
3 y, [+ Z& ]! }0 |: Y},
  Y2 e# m; Z7 Ftr: {
9 Y$ H, v) S, q' m: Hx: coords.tl.x + cosoffset,
; R& ^0 Q* n, O2 q" Fy: coords.tl.y + sinoffset
6 P0 A) Y  ]# E# t},
' P1 P2 i  x6 t) }bl: {2 ^& q3 M$ N% _6 e6 _* o& c4 K: s
x: coords.tl.x - sinoffset,
6 H2 ~6 Z2 [/ N3 {$ yy: coords.tl.y + cosoffset- V# n- B# L# X; q3 m/ w5 g
}
; T) E* P; S- |: e: }2 Q2 W9 ]* [};! c5 S' q" D7 |3 ?+ G
coords.tl.corner.br = {# h  m/ v% V1 p+ z* _9 O/ R
x: coords.tl.corner.tr.x - sinoffset,( o. V2 m& s% v  \2 n8 J9 s  E
y: coords.tl.corner.tr.y + cosoffset7 n! S! z+ @- Q% u1 V
};) {8 x( r0 \: t1 d
coords.tr.corner = {
  I& {$ |& v" ?4 t: |: Mtl: {. P0 k% N( N' z" E. u+ G. h3 U
x: coords.tr.x - cosoffset,6 Z5 @7 [* h* i! k& `, J
y: coords.tr.y - sinoffset" L  y+ l( R# C& ^7 s
},
+ [; U! L' B$ ^& r' @. h* Otr: {
( _$ k4 z4 s+ V* |x: coords.tr.x,
! W( Q: S' ?: m  M! i9 G0 B/ m+ J1 Gy: coords.tr.y
% ]' D" X0 S4 z},5 _6 k7 W* {( q0 H0 d2 E
br: {5 _) s8 t" C8 p/ G. j
x: coords.tr.x - sinoffset,
. Z8 C) x/ [9 hy: coords.tr.y + cosoffset# m' F0 s7 d2 c. X& E
}0 p7 \1 O* h6 Q* R4 q0 i
};
. v0 s5 g: F/ Q) Z" R# scoords.tr.corner.bl = {4 p% x8 w, v' H8 v  O7 B$ ?. N
x: coords.tr.corner.tl.x - sinoffset,
7 c- |* I) Y; F% @/ [+ {# [) w2 jy: coords.tr.corner.tl.y + cosoffset; M# G; b# W( m2 y
};
$ p- h3 u% M3 y* ~2 W- A$ icoords.bl.corner = {
* a5 N( V1 Q6 x' a& G% }* \tl: {
- f& M% B- t3 H6 _( _x: coords.bl.x + sinoffset,
' q, j* E8 j1 `9 s$ b+ j6 X+ ~! Ey: coords.bl.y - cosoffset6 A9 k7 ?! g6 ~& a8 d; ]
},
  ~6 }9 _- o# ]0 cbl: {) {* |% @5 P/ Z
x: coords.bl.x,
/ C2 G0 F  o; Q* ky: coords.bl.y. X- _) h+ V! o
},' |; h9 n" V5 i3 _* p+ h$ a8 Q1 U
br: {
. g, v6 I% L9 M6 m  n: W* Px: coords.bl.x + cosoffset,
+ {. G5 ?. {  ^5 Vy: coords.bl.y + sinoffset
4 M" ^; w- I3 B* o$ t7 `9 b6 i}; `, ], P7 d/ F0 c
};1 J9 L/ n; F# ~8 u6 `* t6 X
coords.bl.corner.tr = {
+ d) M5 a- C* T$ i: ]) o4 [x: coords.bl.corner.br.x + sinoffset,
! b2 E2 ?( F0 _' ?& Wy: coords.bl.corner.br.y - cosoffset
& H2 y/ ]" B$ q' [6 c1 B* G};4 G* W$ H) L2 |+ w
coords.br.corner = {
/ ?! _' s1 |  _; Ztr: {, d' N# k$ q8 [: d6 G9 f) m) B& l
x: coords.br.x + sinoffset,- z: l. P7 F- Z$ @9 ?2 ]- m
y: coords.br.y - cosoffset
1 Q: i, P0 T# {9 [: C/ O) f" r},
: y+ o; Q( b9 y) ]) wbl: {/ O3 ]6 Q; [6 H% p
x: coords.br.x - cosoffset,1 ]# m" E% t/ l5 h5 W
y: coords.br.y - sinoffset
1 C$ i" T) T  Q+ L},
  S' i; k3 h0 H' h0 Tbr: {, b8 P7 U5 D4 D( A9 j
x: coords.br.x,6 L4 o# B) m" ?* B; n- n
y: coords.br.y: N$ }8 v! \. f7 R+ S
}6 i& u! T+ v% g
};
$ f7 O5 v6 T8 {& t: B* M0 B7 pcoords.br.corner.tl = {
/ ^) U% [, p) w1 q2 k2 n( B8 [' {x: coords.br.corner.bl.x + sinoffset,
% o4 o* _9 R7 L, py: coords.br.corner.bl.y - cosoffset
% Q6 b" o' w: b  N' M5 [};6 V) @# P3 r- |2 u
};  W( X7 V+ R" u- P+ V
}());
* F( T* @* A4 `& T4 m4 r8 r+ \return canvas;& ?) L( a! p2 `
});# G, |; V  L5 w3 T( T6 U
puzzle.html代码如下:" A, ]5 x  U' T- s
复制代码代码如下:<!doctype html>
1 D1 K8 O0 {/ X: K1 L2 k<html>
4 _) \( o  `: K& ?  m2 c9 X( {, J<head>
* l2 l4 g, O, a: {( t<meta http-equiv=content-type content=text/html; charset=utf-8>
$ V0 X7 ^; }+ w; F<title>insert title here</title>/ Z5 x1 n3 o* ^9 M+ ]; j
<link type=text/css href=html5_puzzle.css rel=stylesheet />, T/ n& b0 A$ D! Z
<script type=text/javascript src=../multi_upload/seed.js></script>
9 Z0 u% Z" I' K" k<script type=text/javascript src='html5_puzzle.js'></script>
. g+ C) E6 o1 Q( l) l</head>
6 [4 J0 C  x9 o" _<body>& O* f) w7 I: ^5 B/ E( l: F
<div id='html5_puzzle'>$ d! z  j! b: B4 N) d9 R* D
<div id='puzzle_left'>
, p! F5 I+ T+ _3 |% w3 }) _  u<div class='puzzle_column'># t6 |2 g8 e3 C& e. n) a/ \
<ul>
. U4 y# S) j5 ~0 L<li><img src='small_img/1.jpg' data-index='1' /></li>* e8 }7 S* G& [3 @' x! k; g0 Y% L; ^1 J
<li><img src='small_img/2.jpg' data-index='2' /></li>
. K4 H. Q( m; f9 f# c4 O<li><img src='small_img/3.jpg' data-index='3' /></li>
$ J/ k2 v- U0 w  g<li><img src='small_img/4.jpg' data-index='4' /></li>
  }! I9 H8 g. J  ~, q<li><img src='small_img/5.jpg' data-index='5' /></li>
) A( _; i% P# z</ul>
. C2 y2 [: v3 @3 j</div>& H1 m3 I3 k9 v4 Q, B& K
<div class='puzzle_column'>* g: O; t9 I+ V
<ul># V4 T: f7 _- W% A, J% U" u  S
<li><img src='small_img/6.jpg' data-index='6' /></li>
; O$ {9 M, F3 \7 ~& E<li><img src='small_img/7.jpg' data-index='7' /></li>
8 z% r3 x# X* x) O; Q' X<li><img src='small_img/8.jpg' data-index='8' /></li>
9 H) E  P2 H; D9 i" T7 @<li><img src='small_img/9.jpg' data-index='9' /></li>- R8 W: n9 Z8 c# s! ]
<li><img src='small_img/10.jpg' data-index='10' /></li>
; ~2 d, v  ^1 s2 ]+ ~# F* t: W</ul>
% {/ u( |0 m* O, ]9 k+ \' j3 e9 W</div>
5 R" a' h) E- X</div>3 H) E  L2 c5 t5 R1 B
<div id='puzzle_right'>& J  L2 W6 f& f  O, h7 e
<div id='puzzle_canvas'>
. W) s! u: P5 n6 |<canvas id=canvid1></canvas>
: s+ s. b6 G( v# U& J7 h<div id='canvas_menu'>) W4 K0 N" A* \$ p
<a href='javascript:void(0)' id='photo_delete'>删除</a> <a! X& f% I% J5 _
href='javascript:void(0)' id='photo_update'>更改图片</a>
/ }( w. e' D# N$ f</div>; b' Q, X6 H; B7 o
</div>
) s  Q$ h5 t1 v8 _8 F3 Y$ x8 a<img id=bg src=big_img/1.jpg width='600' height='450' />
' P* [3 v% ^7 W2 g+ [</div>
5 B" K8 @7 J$ J9 G: O' h  U<div id='puzzle_bottom'>: z/ W6 A! g% C3 @4 o1 Q2 m* Q; Z4 Z
<a href='javascript:void(0)' id='add_img'><span>添加图片</span><input$ s' K. S6 {# l' k+ {+ f3 e
type=file multiple= id='fileimage'> </a> <a
1 C: `' `6 W2 C; r: ehref='javascript:void(0)' id='upload_btn'>上传</a> <a>点击图片可以旋转,拖拽,
$ m6 {* \" H" D! a$ G+ R8 T8 c) m缩放哦!</a>. S& Y' {& c8 h( g
</div>! q# L) Y1 O/ [
</div>9 Q% {0 ~9 a& X' O& n: I, U
<input type=file id='test'>! W: Y: l# ^; v- p- p
<canvas id='test_canvas'></canvas>
- a7 y  `% p! X6 u</body>
2 ~/ `% ~% H# s& E0 i8 I</html>: t5 `2 m1 k% |9 X! W
html5_puzzle.css代码如下:
, v8 [$ n6 a4 v$ i  U# l% ^; ]& _+ {复制代码代码如下:@charset utf-8;</p> <p>#html5_puzzle {1 h( J# W4 L$ n  t
font-size: 0;6 }1 c% ~! L# K8 G3 ~) ?
}</p> <p>canvas {, ~: j8 B' i& K
background-color: transparent;
3 ~: V# q) g% [0 D7 ^7 oleft: 0;5 }* P3 z7 O* N9 F
position: absolute;
. z  q" t+ u  R5 S  k: F; ]top: 0;
) m4 a3 V8 X, Z% q8 e1 m2 u; x}</p> <p>.puzzle_column,#puzzle_left,#puzzle_right,#add_img {4 M$ w8 f- ]" C7 J' x- l
display: inline-block;
' q, h& q7 k8 x( K6 l' `. j5 N}</p> <p>.puzzle_column li {( r0 e8 w$ d$ r/ }
display: block;
8 I9 X) U! Y# T2 f8 p! K  @  K6 hmargin: 5px;
/ O& t% }0 e' B2 e/ {border: 1px solid #ffffff;- T  X' K, V" Y2 q
}</p> <p>.puzzle_column li:hover {$ h) ]1 g# Z2 K# G# V7 O
border: 1px solid #3b5998;% D" x/ `& ~, f8 a. o& L
cursor: pointer;; o# i' O$ l; J' P  e" p+ P
}</p> <p>.puzzle_column {
" B9 d5 s/ L$ g! mfont-size: 0;
% H; e5 |$ |1 w% M" `2 p}</p> <p>#puzzle_left,#puzzle_right {
& x0 w7 g% V, w: eborder: 1px solid #3b5998;% ~# O* v8 q6 U/ |% u0 @3 s9 }. \2 g
}</p> <p>#puzzle_right,#puzzle_bottom a {6 k7 a; z" R- F# N, }, M  ]. `
font-size: 14px;
$ K2 K- n5 W# ^! ~margin: 10px 0 0 10px;
" B4 W, a$ \' O# H3 X/ [% k}</p> <p>#puzzle_bottom {
( _& g: ~/ D: g/ E, Tmargin: 5px 0;
" a1 R* H, b+ K; n/ n3 L}</p> <p>#puzzle_canvas img {; ]; j% ?1 g* `2 P( t( t$ X6 e
}</p> <p>#puzzle_canvas {% c& C+ [2 t. W! }  k; N% L
overflow: hidden;
: g9 f  P% q* }! b0 z$ Xwidth: 600px;; ~- [/ u/ e- I% C5 o* z
height: 450px;! ]# q7 _1 ]% |: H; Z
position: relative;7 Y9 P7 O- W  x
}</p> <p>#add_img input {$ \4 _" s9 `! t$ x4 v3 N
position: absolute;4 \/ n: M( r/ E, C3 d
font-size: 100px;4 l' y9 f& g, m4 I# W* X
right: 0;
. k  s7 ^7 N) {( q2 a- I. L  Btop: 0;
- e* T# c: J$ B! }+ V4 ^opacity: 0;; B3 A4 B% A# z" z! k) v* ~2 ^
}</p> <p>#add_img {
7 S# P" {! k/ q/ o0 Sposition: relative;3 N3 c* s1 I. x: {7 P$ ~
display: inline-block;) j$ \) F) v6 u4 B7 F3 S8 y! G: w
background: #3b5998;
2 q& z1 B0 j7 W( Y4 `& n6 B; Y9 fborder-radius: 4px;! z2 g/ t2 k+ l0 d0 l' A! H
padding: 4px 12px;
7 N* W8 |2 ^( U, |7 `" F2 {7 {" g* b  Noverflow: hidden;- c0 `+ {5 ^+ {* w- ?6 @" t- \
color: #ffffff;
. h9 j/ m2 E  a$ u}</p> <p>#bg,#show_list {
7 u: n7 d- L& i+ z7 f. ldisplay: none;
/ u! A( p, o$ [9 Y2 \}</p> <p>#canvas_menu {
: S- I1 x% w  wborder: 1px solid red;
, z+ b7 M: ]! ^position: absolute;2 ^2 d" {; t- Q
z-index: 5;. l& V3 W) x; o
top: 0;' n* T/ q0 ?$ w% }
left: 0;6 N8 Z) F# A: @1 M7 M  U
display: none;
* D, L: J! I5 c( P}</p> <p>#canvas_menu a {5 l  H1 L% u! u& J
display: inline-block;
& [# q+ I' T1 E% B' I3 o# [1 c1 R}</p> <p>#test_canvas {+ a2 D) l2 |! r8 N' T# O! B" \  P
top: 700px;
6 x0 v7 a0 I- X+ o}/ @: w5 R# _) c3 l% u" E+ E
html5_puzzle.js代码如下:" o. H0 ?' \; O
复制代码代码如下:require([ 'img_upload', '../puzzle/canvasimg', '../puzzle/canvaselement' ], function(
3 N& P1 p9 ^4 O6 L, d0 Is, canvasimg, canvaselement) {
8 H0 \% }3 {1 h& g" h4 yvar img=[];, ^% T# e+ \  ~1 s
var canvas = new canvaselement.element();
% r/ w$ u5 b* i' y1 `# u* a: b; N% Ucanvas.init('canvid1', {4 r# w. W4 _: R: A9 m
width : 600,: y0 d6 G0 g; ?7 ~5 d
height : 450
! t) p. z  ^6 E4 C, }});! T2 a+ ?$ ~6 O3 ]3 q5 E9 z- N: x
s('.puzzle_column img').on('click',function(e){
2 z1 _! @4 ]: H& l* d% s& jvar index=this.getattribute('data-index');3 Q6 e9 M' `7 W1 K; b" ]/ T- h
$('bg').onload = function() {
0 ?: X+ h" a3 `3 W! a; ^var ctx=$('canvid1-canvas-background').getcontext('2d');
' t& S0 F2 H5 v+ n4 Octx.clearrect(0, 0,600,450);
6 u$ R1 v! g% Pimg[0]=new canvasimg.img($('bg'), {});
/ s, U% d8 [) W: w3 q4 s! Icanvas.setcanvasbackground(img[0]);
" R' E- b* x: Y8 a% A};
- |1 i( x1 V/ T) w4 O! B1 ~5 U6 [$('bg').setattribute('src','medium_img/'+index+'.jpg');
8 G3 O9 I9 M4 p) Ie.stoppropagation();
$ ]2 Z6 u' {4 f- y: J. G! K, h});/ `9 p$ o- H  v% B& w8 x6 q
var canvasdemo = function() {- Z! l% P: k4 n: q# z( j5 O
return {
! J  F3 u: V# U- w+ ]5 ]init : function() {
% z) D0 S+ d3 C5 {var img_list=dom.query('#puzzle_canvas img');
, m0 C- e1 \7 G1 p) pimg[0]=new canvasimg.img($('bg'), {});, S) N) Z- e) V& e7 E2 [
s.each(img_list,function(i,el){
( o5 z0 Z3 G  l2 J* I5 Fel.setattribute('data-index',i);
, U, \+ L# P- B! I5 U+ ximg.push(new canvasimg.img(el, {}));
- W' c% y# ?4 Y' a7 V: k* Z8 {canvas.addimage(img[i+1]);8 Q- Y, N4 b* D) T, j
});
* ?" q- w+ J* p0 [; ]* b9 h# U* \canvas.setcanvasbackground(img[0]);+ I8 }7 q' @0 ?# |. b# F" ]
this.cornersvisible = (this.cornersvisible) ? false : true;
5 m+ r" h0 d9 [# |3 p- p2 Nthis.modifyimages(function(image) {1 P2 D4 g9 m3 g9 R+ @" F0 ?' ~( A
image.setcornersvisibility(this.cornersvisible);4 s% Z( Z7 J- B7 D5 A
});( |" i( ^. |0 O8 y6 F& y
},* @5 k, s& Y$ G2 |3 M! w
modifyimages : function(fn) {% @* B6 p; V* u% _% d
for ( var i =0, l = canvas._aimages.length; i < l; i += 1) {
/ u& z7 B/ e; j7 Ifn.call(this, canvas._aimages);
2 L) k& h+ e" r- W}
2 y, |. O; q' a# Z$ ]- Ycanvas.renderall(false,false);8 j5 c! z$ O. `
s('#puzzle_canvas img').remove();
0 o4 _9 f/ w4 U7 r8 \  X" W3 i) Eimg = [];2 a( n  C. \5 k% X$ _- m# k
}
) S9 u4 t/ k. C5 {- H/ i};$ i& o/ F2 V3 U! }0 m: b7 H
}();
; g6 R/ \2 w6 |8 n& dfunction getcurimg(){3 ?& M, S2 r; l" o
var oimg=canvas._prevtransform.target;% @  ]' }2 f, m2 r+ e! ^
for(var i=0;i<canvas._aimages.length;i++){# c2 B7 p6 Y! V/ T
if(canvas._aimages._oelement.src==oimg._oelement.src){' u0 p5 _. g0 Y% V& B1 @# Z% ^
return i;
! M8 T+ n3 h% c+ v. x}
6 z1 N+ [6 f. H! j, s2 T  S$ m}
" i* d0 S5 F" ~% ^) v6 |- M}
: k: y8 A- t7 Z+ \' T# w' @5 }s('#photo_delete').on('click',function(e){
) Z% O: d& V; \) i6 _# m; M+ Bvar i=getcurimg();
( q8 r* Z3 k0 ~* Bcanvas._aimages.splice(i,1);0 F8 _9 G( N$ {. \: J
canvas.renderall(true,true);& N' P" O" R* C+ B3 ^! k) h8 k8 g
$('canvas_menu').style.display=none;
6 N- C5 l8 p5 j. \$ t' }: m});
9 @, w2 e  t( z- N" Es('#photo_update').on('click',function(e){. i! q* s1 F) x* O- l: O+ v
$('test').click();8 p/ g' V! b5 ?+ t" K9 O+ W
});6 U; |) }$ X% }2 z, n
s('#test').on('change',function(e){3 L6 {6 a& Q! Y7 c, o" b& o6 a- t$ n
var files = e.target.files || e.datatransfer.files;8 t6 `6 c( f$ }0 Q1 a5 v4 P) D
var reader = new filereader();
" q# ^  q1 G. E! z. ?reader.onload = (function() {
* b9 w! ?& `0 E4 [) Freturn function(e) {
% \+ a: T- e; T, g- ]var dataurl = e.target.result, canvas1 = document.queryselector('#test_canvas'), ctx = canvas1.getcontext('2d'), img = new image();7 T, G" S. N. Z& ^0 }; |2 ?
img.onload = function(e) {
0 m- P( g. m* x3 O9 Nif(img.width>200||img.height>200){7 V+ ^) \2 v( e2 c- K( A0 F" G
var prop=math.min(200/img.width,200/img.height);: F, H9 ]+ e( J7 J# u% }9 J  o0 P6 }
img.width=img.width*prop;* [; b* |, |' f0 e
img.height=img.height*prop;
  S* R9 f$ X3 ?, u}3 ^) a. e, X" r! e$ g+ {
canvas1.width=img.width;
% v  S4 [2 C, m% t- Ecanvas1.height=img.height;
8 R2 L" p+ z! X+ rctx.drawimage(img, 0, 0, img.width, img.height);) T) o# A' [# K8 g
s('#canvid1').html(s('#canvid1').html()+<img src='+canvas1.todataurl(image/jpeg)+'/>);8 i* e5 d# ?* M4 u/ Q
var t = window.settimeout(function() {" c" J/ ]7 p, L: O5 C- X0 e! p
var i=getcurimg(),target=canvas._prevtransform.target;
$ Q# F9 G3 G7 U9 jconsole.log(target);
% Q( @* I, g& z5 [6 hcanvas._aimages=new canvasimg.img(dom.query('#canvid1 img')[0], {
5 F) w8 @! {. _9 Z9 ftop:target.top,  H; S4 p! M6 \/ W, P* Q
left:target.left,
8 u+ W% ^' c8 t; v) v+ ]% mscalex:target.scalex,% s' o& Q- ~$ i3 q8 t' S
scaley:target.scaley,
6 r1 R. R4 n9 p  o0 f1 Z+ k7 {! |1 p1 [angle:canvas.curangle7 P7 F# N6 T, V7 _
});9 J) c" X6 [  A0 Q
canvas.rendertop();
1 a! M+ W- K% a5 t8 ~cleartimeout(t);. B5 h* l3 i- f* T3 Q
s('#canvid1 img').remove();
( V& A4 |* ?  C  x! v$ m* c! i, t},1000);
" D6 G+ t6 _* o  |$ i1 A/ Z4 C' B};( [; ~! u' C4 j/ ^. b
img.src = dataurl;
, m/ ]1 T3 I6 E- \};
" x, w6 u% W/ t2 F9 z* R! M})();& K5 q! e4 }3 B! m3 p
reader.readasdataurl(files[0]);' {8 n  h: h$ j
});& Z1 [6 e0 `( n* N8 b, j
s('#upload_btn').on('click',function(){- V0 F! T) k) \) A9 I
var imgdata = canvas.canvasto('jpeg');1 X7 |7 y2 ?3 h+ }: u3 U: G1 q
var imgvalue = imgdata.substr(22);9 Z4 H% k, e* [  V9 ]- q
s.ajax({
+ K& v9 @3 V" U8 h1 Q4 p! x& {url : 'http://localhost/html5/upload1.php',
. @1 U9 L, ]' V3 l3 b; mtype : 'post',/ ^% j. g' ^! l/ S! ^) R" w
data : {
" }9 ]1 P: @& M2 C7 l5 himgdata : imgvalue,3 _0 |, e! ~8 d# f; c
file_name :'mix_img.jpeg'2 @' [5 Q3 n. t+ T* ^, O. L, j
},
( @, z, y, O% ]. q8 Edatatype : 'text',
& v& H1 J$ s$ `  wsuccess : function(data) {+ h9 O9 p" j2 X  P+ a. ]
alert(s);1 Y& {; O2 O$ O4 C0 G
}) V1 W* o6 f- p0 Z) h) H
});) L0 y0 `& o" \0 b- G8 [
});
6 i: `6 C* k+ g4 H+ \});8 V) T6 S4 d' b* x' _
至于用html5 input读取图片,这很简单就不贴出来了。. T. x3 @1 ^) y8 p/ [/ b, [, C
! C2 v9 d, H9 v5 l8 I- q
更多网页制作信息请查看: 网页制作

回复|共 8 个

effoggikeftor 发表于 2015-10-26 14:23:35|美国 | 查看全部
想骂你.却不知道从何骂起.
Acropozelan 发表于 2015-10-26 14:23:39|湖北 | 查看全部
怎么就没人拜我为偶像那??
mwxny 发表于 2015-10-26 14:24:32|美国 | 查看全部
兄弟....表给偶丢丢哦....
tohme 发表于 2015-10-26 14:24:38|美国 | 查看全部
看,刚说你眼神不好,你还就来劲了不是.
gevaemaidovef 发表于 2016-2-8 12:39:35|INNA | 查看全部
疯了.........
wwdu926a 发表于 2016-2-8 12:39:36|INNA | 查看全部
这篇帖子构思新颖,题材独具匠心,段落清晰,情节诡异,跌宕起伏,主线分明,引人入胜,平淡中显示出不凡的文学功底,可谓是字字珠玑,句句经典,是我辈应当学习之典范(不好意思回错帖了
mwxny 发表于 2016-2-8 12:39:55|湖北 | 查看全部
一骂人就激动....
Mqokjdvq 发表于 2016-2-8 12:40:20|美国 | 查看全部
对于这种刚发的帖子,我总是毫不犹豫的顶了。如果火了就是个前排,可以混个脸熟,说不定谁好心就给粉了…稳赚不赔;如果沉了就感觉是我弄沉的,很有成就感,还能捞经验。

回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

得知互动是一个融创意、设计、开发、营销、生活、互联网于一体的专业交流分享平台。
Copyright © 2026 得知互动-专注Ai与站长技术交流社区-Ai交流平台 版权所有 All Rights Reserved. Powered by Discuz! X5.0 鄂ICP备15006301号-5|鄂公网安备 42018502006730号
关灯 在本版发帖 扫一扫添加QQ客服 返回顶部
快速回复 返回顶部 返回列表