本文实例讲述了html5版canvas自由拼图的实现方法。分享给大家供大家参考。具体方法如下:
. X5 k9 v( e7 q6 w代码运行效果如下图所示:
" d x( Q2 t% V- d ~
4 c" E2 t% S1 S: [' [canvaselement.js代码如下:
P& Z( \8 K) r$ m- _9 }( g复制代码代码如下:define('canvaselement', [ '../multi_upload/core' ], function(s) {3 J7 i; U/ r9 G
var canvas = window.canvas || {};
2 \1 W, X7 ]6 A2 W& X: [# {(function () {
, t9 y x9 O j4 z+ H- jcanvas.element = function() {}; . H; S7 V* v% a& A9 a0 [8 v
canvas.element.prototype.fillbackground = true;* |7 ^( e: t" t' q, w+ C0 ^4 n' ^
canvas.element.prototype.showcorners = false;# D/ Q( W5 e6 C- O! R7 w" A
canvas.element.prototype.photoborder = true;
" H) A3 u' E$ f" j, h2 N: tcanvas.element.prototype.polaroid = false;! N! E3 N u ~) U0 z5 }1 Y" m
canvas.element.prototype._backgroundimg = null;( l% I* n8 ]& G @: ^
canvas.element.prototype._groupselector = null;
2 ~( @1 B; b( p+ |: V" R: K! Hcanvas.element.prototype._aimages = null;+ o1 v8 j6 @0 b
canvas.element.prototype._ocontext = null;
' d3 D( z( T2 i3 V# o! {canvas.element.prototype._oelement = null;
7 {: J2 L9 E" Y1 D- }canvas.element.prototype._oconfig = null;
5 W# ^/ n& P" x; t% y" Ucanvas.element.prototype._currenttransform = null;
% k3 E6 A" F+ tcanvas.element.prototype._prevtransform = null;. g; [7 O3 t+ D5 p# x
canvas.element.prototype.curangle = null;
1 D5 O0 x' o1 U7 Z3 B% H. |canvas.element.prototype.init = function(el, oconfig) {: j k( m: H2 @1 d: S3 N
if (el == '') {
& P, T. p1 \7 O' Nreturn;
5 S1 U r0 H G% j}3 R0 C' l3 B0 ~& n/ l* `0 A: ?. a
this._initelement(el);
4 V1 {- L: C* Jthis._initconfig(oconfig);* \. R- g5 j0 t% E5 d2 z5 [( S
this._createcanvasbackground();! O7 Q- x7 o3 @0 l# b
this._createcontainer();
2 c, u- P W$ o% v0 Kthis._initevents();
/ Q$ ?. \/ u# f6 i$ {1 Lthis._initcustomevents();
3 S' C9 ^! U5 b u/ D( s U8 H/ c$ ?};% P+ |; A7 Q4 Z1 y( K3 [) V
canvas.element.prototype._initelement = function(el) {+ b9 X; ~* }. Z% q6 A3 }$ q" P
this._oelement = document.getelementbyid(el);- E% x y% w; d# h" R5 g
this._ocontexttop = this._oelement.getcontext('2d');
) X; M6 n: l9 c# c5 e9 Q8 j% t% K};
! h) U0 U% ]& t+ o" e1 Y5 ]2 Qcanvas.element.prototype._initcustomevents = function() {: Z, _/ d0 p$ o
this.onrotatestart = new canvas.customevent('onrotatestart');
+ H6 g5 R& e f& `4 r, {this.onrotatemove = new canvas.customevent('onrotatemove'); 0 z% R6 r7 M) F, x+ U6 _5 O
this.onrotatecomplete = new canvas.customevent('onrotatecomplete');
! Q6 J2 Y, S3 H. ]4 K% H3 j4 ithis.ondragstart = new canvas.customevent('ondragstart');
% ]! M" a) p2 T* G+ h, Qthis.ondragmove = new canvas.customevent('ondragmove');
4 x, M% Y/ h# m. nthis.ondragcomplete = new canvas.customevent('ondragcomplete');; E$ j2 t# h ]0 s8 V! I3 U( W
};
4 A; ]+ j4 R! s/ gcanvas.element.prototype._initconfig = function(oconfig) {" V, m. s1 \% j, O; t
this._oconfig = oconfig;
6 j( X3 s/ G* `3 I0 L Pthis._oelement.width = this._oconfig.width;
8 N1 K" y3 t* o/ V, x/ n: }8 y( A, y$ Dthis._oelement.height = this._oconfig.height;7 N7 [" Y* }9 ^9 K
this._oelement.style.width = this._oconfig.width + 'px';
+ d5 k: x3 e4 g% O! Xthis._oelement.style.height = this._oconfig.height + 'px';0 T, x: n- a' g3 q
};0 b+ c! m. S4 J
canvas.element.prototype._initevents = function() {
7 y* i& l# w/ {' L6 {) Wvar _this=this;
/ B; K, X3 B1 G; m, }2 A7 As(this._oelement).on('mousedown',function(e){
' ^3 d2 u2 ^* }, Q' b: z7 y$ L_this.onmousedown(e);/ p+ d4 h, b6 o6 Z
});
, a; n0 H6 ]+ i! Zs(this._oelement).on( 'mouseup', function(e){( S0 F2 S( c+ o& x' a5 `
_this.onmouseup(e);
; D2 w/ Q" V! I" z4 e0 }});
: P3 X3 n$ R( D- s# @) i# |2 p6 Xs(this._oelement).on('mousemove', function(e){
5 F9 P5 ?% i4 h }1 s& i_this.onmousemove(e);
1 W, s1 P0 |& O});* M: {* F+ A3 z; S6 R
};0 k; k1 D w u) K, t2 G
canvas.element.prototype._createcontainer = function() {1 M! J8 ^2 G, t! Z
var canvasel = document.createelement('canvas');
; F+ ?& l+ n1 U! o) s' Ecanvasel.id = this._oelement.id + '-canvas-container';1 n, C o2 B6 L" Y4 _+ A( [
var ocontainer = this._oelement.parentnode.insertbefore(canvasel, this._oelement);
8 h _& f3 W6 {7 Z( vocontainer.width = this._oconfig.width;
/ ?2 B, s1 H0 I1 F2 eocontainer.height = this._oconfig.height;: A) D6 [6 g# K4 ]
ocontainer.style.width = this._oconfig.width + 'px';+ v5 q" s! @2 f
ocontainer.style.height = this._oconfig.height + 'px';8 ]" Y+ E. H2 Q# f) f" j" P
this._ocontextcontainer = ocontainer.getcontext('2d'); . c" x8 O" L0 {( n9 [
};" _8 |# M5 F; s7 ?& v w
canvas.element.prototype._createcanvasbackground = function() {3 Q$ s1 F! k- @& o/ r
var canvasel = document.createelement('canvas');& t7 D4 S$ w% a+ N$ H3 a' f! k! j
canvasel.id = this._oelement.id + '-canvas-background';
: t, E' w: a; M2 \, K3 g/ `var obackground = this._oelement.parentnode.insertbefore(canvasel, this._oelement);
: v1 |# C/ r Y5 H7 G! Uobackground.width = this._oconfig.width;
) { T. y- H+ Y# I! Qobackground.height = this._oconfig.height;; G* _) ~2 i* L
obackground.style.width = this._oconfig.width + 'px';
, s7 B q+ \5 hobackground.style.height = this._oconfig.height + 'px';
3 x/ M" ^1 f/ o( F bthis._ocontextbackground = obackground.getcontext('2d');
" Z J, r$ i3 z1 E% ^( E( M2 E9 T};
% }% E T7 U( d3 U% Y) q4 t( L/ e4 zcanvas.element.prototype.setcanvasbackground = function(oimg) {
9 l8 N8 z$ D1 {1 R+ O) w! j6 F8 T9 w7 Ethis._backgroundimg = oimg;- v* {1 B' K3 ]/ x- f% j
var originalimgsize = oimg.getoriginalsize();
: w. V Y( U2 I- B8 I+ y; w5 l: Ethis._ocontextbackground.drawimage(oimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);+ d5 \" r, J% h5 I" O3 F i
};
! P8 R! [$ i/ }6 Y0 D& b9 Kcanvas.element.prototype.onmouseup = function(e) {! ]3 y- t4 y& j; m; T* r' @/ o
if (this._aimages == null) {, [% `1 c( [( Q9 J
return;
# w1 u4 Q1 _1 y9 Y/ E% r$ u}
1 p4 l! ^, f" M: w z6 gif (this._currenttransform) {
( t. m7 j. h6 Sthis._currenttransform.target.setimagecoords();
G/ U7 y1 }8 t0 O! q}+ O F& u( c7 W' f
if (this._currenttransform != null && this._currenttransform.action == rotate) {
+ M7 m# t A* o2 z: b& H5 l. qthis.onrotatecomplete.fire(e);
9 D+ x5 p( U7 h; j6 t! V} else if (this._currenttransform != null && this._currenttransform.action == drag) {
4 |% `6 B$ W( N |# othis.ondragcomplete.fire(e);2 j5 f1 |: ?2 j+ C* g8 ^+ t _) q+ y
}2 a# W1 T" ?/ D2 D! S8 n
this._currenttransform = null;) ^& t; ]( Y. s; t" c% Y9 r! ^+ S
this._groupselector = null;
3 g# d1 E$ x3 M6 s! |% wthis.rendertop();6 m3 D4 u4 |$ d
};
. M& ?. j' }: v6 c8 acanvas.element.prototype.onmousedown = function(e) {
/ B: j4 ]: M# n7 D8 qvar mp = this.findmouseposition(e);/ v8 o9 H9 q2 |
if (this._currenttransform != null || this._aimages == null) {
. d" g, b6 A; Sreturn;
$ b- k9 h& v3 V' |}
* j3 M- r' J5 o/ f9 M8 x! h$ Pvar oimg = this.findtargetimage(mp, false);
& e. Z* t6 N3 e/ r7 m1 R! M: aif (!oimg) {$ P0 v1 i* {6 y9 `% ?# i
this._groupselector = { ex: mp.ex, ey: mp.ey,/ M1 `: @# F& a/ [ ]' J
top: 0, left: 0 };
5 f2 a& D2 p: {& k5 N}$ z' q1 p( n& g2 G6 ^& {% F4 B2 X! R& g; O
else {
, q- i3 n2 w4 a3 ]- O' X: Yvar action = (!this.findtargetcorner(mp, oimg)) ? 'drag' : 'rotate';
, @/ A, J. ]; y _0 zif (action == rotate) {
# K5 H$ L4 u7 W( `, kthis.onrotatemove.fire(e);7 l/ ]3 V: }* j. J8 T; r- P
} else if (action == drag) {" C+ U8 @3 E" r E" N4 b6 A! }" J- D" E
this.ondragmove.fire(e);3 _3 u0 |( j* n* K0 E3 I
}2 a6 T8 z- n/ J, v6 {
this._prevtransform=this._currenttransform = { ' d9 F8 Q' S; O
target: oimg,
% @, _6 E; v2 G b2 T- E" ]action: action,
: o, C6 ^- `( F* j5 Oscalex: oimg.scalex,6 e! a4 e6 G4 Z/ ?
offsetx: mp.ex - oimg.left,
8 P; h8 |7 } B' ^offsety: mp.ey - oimg.top,
, H& N* I' k1 L& ?$ Aex: mp.ex, ey: mp.ey,7 D j* F5 _" U+ p$ ^1 Z
left: oimg.left, top: oimg.top,5 E' K, ]5 a; P
theta: oimg.theta
5 S3 q/ M7 e' ?, r: p6 F& i3 Q: Y};
) a, n8 s4 o* Y& z4 F$('canvas_menu').style.transform='rotate('+oimg.theta*180/3.14+'deg)';
* Y& v7 A8 u# l* w' n3 T7 o0 H4 ]$('canvas_menu').style.left=oimg.left+px;
$ G& g O- p/ r2 S5 ~$('canvas_menu').style.top=oimg.top+px;
& d( i( F5 D8 J$('canvas_menu').style.display=block;8 Q. E+ r4 N. p$ D M
this.renderall(false,false);
- ]1 L* f- L7 B0 {8 G5 U}
`& B0 f# D# ^9 o2 |};
# }3 | @$ ?7 A) w1 Q9 j8 G ~canvas.element.prototype.onmousemove = function(e) {7 t2 N# G$ |2 m G& f. D0 k
var mp = this.findmouseposition(e);7 e; o; m- |# D' }
if (this._aimages == null) {
! R5 b% K, G( greturn;/ s5 }( r2 a" y, B t
}' \) E8 y- ]7 j4 _8 _, l9 f0 {
if (this._groupselector != null) {
4 I; ^/ ^+ \1 z4 R! nthis._groupselector.left = mp.ex - this._groupselector.ex;: |, ]) |2 \- D7 _) h4 c
this._groupselector.top = mp.ey - this._groupselector.ey;
6 Y- \6 w; _! K+ Xthis.rendertop();6 L) {4 Y4 I: H
}
j- r, f9 q& R# h6 ~! Velse if (this._currenttransform == null) {
; |5 I6 i: O8 F H/ |var targetimg = this.findtargetimage(mp, true);
. U n6 L! i; C/ G+ Z( o) Dthis.setcursor(mp, targetimg);
6 G: `( }7 F! |: G3 v, F5 `}
- C' |7 b. _8 k9 ielse {
8 q/ f: D* s- u) i9 W! U+ E8 _if (this._currenttransform.action == 'rotate') {. W; X; k' K( {5 p, b
this.rotateimage(mp);
% ]4 e0 Y! U5 b( Bthis.scaleimage(mp);: l- @, z, P1 E8 R5 i" L4 k F2 Q
this.onrotatemove.fire(e);8 X8 z, `* M+ g! T! ]8 r! X/ H2 J
} |& j; p' Y7 `: P9 `
else {
$ x& d. M. K5 h# ^* F, jthis.translateimage(mp);
/ M" ^6 A$ c7 W/ H; Y8 |4 dthis.ondragmove.fire(e);
5 J1 e7 `% F; C2 J5 d}* K$ j% o, o. I1 E- y; E" K9 D
this.rendertop();
- t0 W9 j7 T# T+ J! Z" U}
7 `* o! `. {2 K8 e! B};# H0 o' U. {4 W% X! w. V
canvas.element.prototype.translateimage = function(mp) {
/ e" |6 Q6 K: F% \: Zthis._currenttransform.target.left = mp.ex - this._currenttransform.offsetx;: r, V% f, W* v
this._currenttransform.target.top = mp.ey - this._currenttransform.offsety;" S$ E/ x3 K, I6 W' x
$('canvas_menu').style.left=this._currenttransform.target.left+px;/ \" m0 ?- F4 C; s/ M
$('canvas_menu').style.top=this._currenttransform.target.top +px;- p7 }/ P0 x; d C
};- t7 X/ P: i" b
canvas.element.prototype.scaleimage = function(mp) {9 \7 Q$ E4 u7 e' e0 h9 Y( J7 R0 |
var lastlen =
0 S4 Z9 k4 m) l$ t# g: Imath.sqrt(math.pow(this._currenttransform.ey - this._currenttransform.top, 2) +9 v [/ {) d6 l, g1 S0 m7 N) R
math.pow(this._currenttransform.ex - this._currenttransform.left, 2));
& l4 ~# X$ U( z! a1 D1 |" [, C8 kvar curlen =
0 N- m& X" q/ hmath.sqrt(math.pow(mp.ey - this._currenttransform.top, 2) +2 @0 s5 _. Z e0 ~0 V; ~
math.pow(mp.ex - this._currenttransform.left, 2));8 y# Y% E9 u* W( y
var curscalex= this._currenttransform.scalex * (curlen / lastlen);& Z- _6 A/ @4 y e0 P7 w) D+ F
var curscaley=this._currenttransform.target.scalex;. `4 N1 D4 ?7 k7 v! E/ h% I
if(curscalex>0.7&&curscaley>0.7){. C9 _3 k7 j2 ^: x( Y% W% y) M! c
this._currenttransform.target.scalex =curscalex;
" [! R! a: W5 T% }/ ^" E# u4 d5 Othis._currenttransform.target.scaley = curscaley;7 u ^# R8 K$ ], X
}7 b O5 J8 R6 m! m. x( p. H
};0 ~/ D1 Z5 ~- u5 d0 s/ R
canvas.element.prototype.rotateimage = function(mp) {% r! M7 g: G2 M8 z6 a3 C, @3 Z
var lastangle = math.atan2(
6 a& }, A4 I1 T9 O) L( `* Uthis._currenttransform.ey - this._currenttransform.top,
8 K0 u! f! e6 Q dthis._currenttransform.ex - this._currenttransform.left5 m% Z' y0 _4 x5 J0 J3 {4 G7 P
);- h$ t" }. z' s$ D2 i
var curangle = math.atan2(
% K% x8 U1 I; {# n* Tmp.ey - this._currenttransform.top,
" f; k' Q2 Y; g$ `- S' f( n' F- x2 Qmp.ex - this._currenttransform.left( T! }% F' Z9 b1 B3 E! D% D
);1 Q3 s) k" ~& ]% ~1 n8 G+ H, M
this._currenttransform.target.theta = (curangle - lastangle) + this._currenttransform.theta;7 ?6 P: h" G" I
this.curangle=this._currenttransform.target.theta*180/3.14;
: O; T: o1 x7 P$('canvas_menu').style.transform='rotate('+this.curangle+'deg)';/ Z4 I l' I, g- S6 U: L7 n1 u
};/ {4 h H, {# m- L7 k# A6 T0 D
canvas.element.prototype.setcursor = function(mp, targetimg) {$ R% `+ t2 b" Q& v' r1 Z9 U
if (!targetimg) {- i; k, U- Q; \ w
this._oelement.style.cursor = 'default';
- U3 [' C2 l8 z1 }% U}
6 ^! V; X1 \2 y# k$ W! J& Gelse { 1 J* l" X# z0 M! |' K1 E& W
var corner = this.findtargetcorner(mp, targetimg);
/ k7 l8 [; _6 C2 Y+ A! J! u3 D; J; @) sif (!corner)
5 f# a+ I" [3 Q. _' f ?{
2 h: e0 T# {9 K% y% z8 @ X g+ s* _: Dthis._oelement.style.cursor = 'default'; @( q8 m. j/ c. P4 Y1 D5 M/ }1 M
}+ q8 d( P. C' L4 q$ [
else
0 ]$ x8 w+ C6 G# o2 t/ t{. s8 J! v# M0 K8 F* C
if(corner == 'tr') {
$ x) z9 M( N9 g+ Dthis._oelement.style.cursor = 'ne-resize';+ A. _2 l( X- ` E0 S
}3 q- a! ? z; W0 @" V& M
else if(corner == 'br') {# L# \7 D" {: f3 U& t9 I
this._oelement.style.cursor = 'se-resize';
* J# a/ p5 |# m( ?! i/ s" r}
! P( Q+ X$ ?5 ^, T, s& ielse if(corner == 'bl') {
7 i; ]2 H8 q i4 Q4 E- ^2 ^8 y$ Qthis._oelement.style.cursor = 'sw-resize';
7 W. A+ y: P5 }}3 \+ }; A' |+ U: k7 W4 v
else if(corner == 'tl') {
5 E- G* Z5 t! J( [this._oelement.style.cursor = 'nw-resize';
% r7 d+ b+ v6 y7 j: h}
: b( |/ E8 j; Selse {% p. _' T. h+ T. Q' Y/ _7 \8 [
this._oelement.style.cursor = 'default';+ y5 J. D# u; W/ E X1 y( m
}
( E6 h0 E" F3 m( O8 [2 h}# b# b) P. Y4 Y R; x, o) v- k
}
! F l& M- b( g( @/ H4 j};
: Q5 S4 n* L- \6 Gcanvas.element.prototype.addimage = function(oimg) {
: W% b2 e8 u# P8 L& i1 Eif(s.isemptyobject(this._aimages)) {
8 |0 s# ?+ y7 l! b- j1 [$ lthis._aimages = [];8 e1 z; P2 q6 s+ r: |2 U
}, V1 \' a, ?, E: n
this._aimages.push(oimg);
3 H* D# p$ j) g8 H r& tthis.renderall(false,true);</p> <p> };
d$ G. R$ B: rcanvas.element.prototype.renderall = function(allontop,allowcorners) {+ ^" M* H3 |0 Q+ E: D% M
var containercanvas = (allontop) ? this._ocontexttop : this._ocontextcontainer; n! J( n, @$ o: b4 ~9 E6 j: c# r
this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
. p1 I7 L/ |- y0 @# Z6 w# C& _9 fcontainercanvas.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
, U9 @ f1 D ~+ I, N1 E! `8 Bif (allontop) {
5 V, w2 c+ n/ q3 ?9 Avar originalimgsize = this._backgroundimg.getoriginalsize(); A6 N" i( q6 \9 s: C0 T* Z7 \
this._ocontexttop.drawimage(this._backgroundimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);* N" b$ \) \2 s! A
}; ^" U1 L: T5 m- E
for (var i = 0, l = this._aimages.length-1; i < l; i += 1) {5 R/ Y: q5 R9 S9 ^ p5 Z
this.drawimageelement(containercanvas, this._aimages,allowcorners); 5 r" g/ u, {1 v" F/ n9 g3 h4 q; f
}
/ f4 H3 K, v' M0 U G8 u+ Rthis.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],allowcorners);
; ^2 z' j, ?& _5 L" U% X7 ]+ g};- H! o8 A8 {+ e- g
canvas.element.prototype.rendertop = function() {$ H# x$ f z% V3 H4 M) g/ \% a2 ]
this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
. R/ P% U( k7 ?" O9 q$ bthis.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],true);
! [2 M- b: y# x2 c3 ]0 A+ w3 k! |if (this._groupselector != null) {$ T$ M1 ^1 Z. D3 X0 ~4 k) P
this._ocontexttop.fillstyle = rgba(0, 0, 200, 0.5);
; J9 T* w |9 G5 L; s& t }this._ocontexttop.fillrect(
6 L- P3 U; C! B) |* b2 {+ Ethis._groupselector.ex - ((this._groupselector.left > 0) ?2 x, \" h" d) f
0 : - this._groupselector.left),
+ p" l: G& }! J- A. Dthis._groupselector.ey - ((this._groupselector.top > 0) ?
3 @% w3 U. `& c; ]" w1 g! o0 : - this._groupselector.top),
) B/ x/ o& c4 l3 D; f) vmath.abs(this._groupselector.left), 0 `/ ]0 t( V7 B# r& t' ~
math.abs(this._groupselector.top)
3 E- `0 G1 k* C0 L9 ]) p" M);
% [; N4 B4 }3 w% L: zthis._ocontexttop.strokerect(
9 p* R' q& z4 M, s7 b# cthis._groupselector.ex - ((this._groupselector.left > 0) ? X- T) S- V- ~
0 : math.abs(this._groupselector.left)),
7 c0 x1 U6 |0 q; ]# L1 X8 |this._groupselector.ey - ((this._groupselector.top > 0) ? 1 H9 k: l$ B( @" G" s! A" g9 Y7 S
0 : math.abs(this._groupselector.top)), 5 n f/ g7 X6 H& N3 S
math.abs(this._groupselector.left),
+ L) L$ o0 J! U8 n5 s2 L0 d5 cmath.abs(this._groupselector.top)6 T) Z$ o1 {3 P- `* H
);
1 d n4 J* p/ ~4 `( Z' q S}/ d) X' \9 O( T7 g* S' c5 S
};, y* l0 b- P7 m- t2 K
canvas.element.prototype.drawimageelement = function(context, oimg,allowcorners) {
8 Q0 r: A0 ?% {& Q. t* N$ |+ Joimg.cornervisibility=allowcorners;* a1 \# Y: M3 `1 v) a4 Z. s& n
var offsety = oimg.height / 2;
- `+ Z0 w7 f6 hvar offsetx = oimg.width / 2;; M h, c6 d% u9 }
context.save();
1 A) p5 D. @. K/ D% {context.translate(oimg.left, oimg.top);
7 n8 i$ E6 @. ^& {# r0 scontext.rotate(oimg.theta);; }, |# ?3 Z7 _% d) i
context.scale(oimg.scalex, oimg.scaley);
6 z" R8 l1 d' B+ s3 V/ ~- Rthis.drawborder(context, oimg, offsetx, offsety);3 P5 P0 }" z" v+ K/ J' @
var originalimgsize = oimg.getoriginalsize();% [7 k7 S) ~ a" t! a% e
var polaroidheight = ((oimg.height - originalimgsize.height) - (oimg.width - originalimgsize.width))/2;! h5 j* U' Y5 F
context.drawimage(
5 E4 Z$ Z: \, ^2 Y9 \oimg._oelement,
/ P- {( l S2 K4 C9 _- originalimgsize.width/2,
, l! A; j+ H; I((- originalimgsize.height)/2 - polaroidheight),
1 @8 W- t- @, _" `- b) koriginalimgsize.width, 5 n- x) q, z9 U7 ~2 O6 y
originalimgsize.height
/ h, f9 p7 t$ ?. ~$ b);
: D2 i6 k: c! j5 T# P @if (oimg.cornervisibility) {
) K' w0 o0 T n# xthis.drawcorners(context, oimg, offsetx, offsety);( S. [" l4 Q v1 I: d- C! T
}
6 |) J( x% M; Q9 {context.restore();
( y) b4 |8 o. T0 F: T8 j+ m7 [/ L. _};2 J4 _" v7 Q/ f, k2 E" l
canvas.element.prototype._getimagelines = function(ocoords) {9 Z# G4 b4 K2 U1 x
return {, U; ^6 |' |6 y5 m" }! d
topline: {
$ b% `: ^ ^. xo: ocoords.tl,2 O G; x" S+ g+ e( t! Y/ u* d
d: ocoords.tr
) F9 S, k/ _5 X9 c8 S& Y3 I},
0 ~! a3 y" z, E% p- J$ Q2 frightline: {
! n$ H! c4 E3 qo: ocoords.tr,
7 Y) E5 ^0 o5 y+ Xd: ocoords.br
% U2 ?* c, `: o/ q$ K& w},
- o. r3 v+ v+ Q5 h8 I% N3 Fbottomline: { % U9 ~9 _! `- r6 l: B4 B9 E [
o: ocoords.br,
$ p9 j* v! q& L9 Cd: ocoords.bl ! ^9 n2 \8 \5 u0 I* ^% K5 }
},' w F! D8 j4 K/ y( V* ~
leftline: { 5 A4 ^# O* N0 U! }9 N% r* `
o: ocoords.bl," m+ d" w5 v& f" d2 ?: M
d: ocoords.tl
1 m, k6 r! P0 ~% Z4 `}
' k, l$ E# U( v! `. E/ R4 g8 H1 C: M};
# [* h1 d8 W5 t; U: ^) } `};& P6 r" n- k }% Y3 `* ^' D
canvas.element.prototype.findtargetimage = function(mp, hovering) {' N3 T2 @$ G0 L; u9 ]& w
for (var i = this._aimages.length-1; i >= 0; i -= 1) {6 t: K/ [1 s* P" T$ p [1 P
var ilines = this._getimagelines(this._aimages.ocoords);, g$ s$ M7 l+ A9 G) w, y C2 x: ?
var xpoints = this._findcrosspoints(mp, ilines);
8 @. L3 s1 W) N3 ^% v) Zif (xpoints % 2 == 1 && xpoints != 0) {
$ F6 v' c$ ~5 bvar target = this._aimages;, l, D% D, N/ n+ l8 n% F6 e- P0 L3 e
if (!hovering) {
& A* R5 ?2 H) r% L3 p' i u! p) Rthis._aimages.splice(i, 1);
2 J) y7 K2 Y7 i2 [this._aimages.push(target);
. j7 ~1 w l& a5 n0 d}
% J+ l9 t/ r0 f& O( ]return target;- Y) j; A! L8 ~* U, ~) R
}
4 ?6 ^+ u3 v* C5 |( b! x}; M3 {* p8 @6 d7 m1 w# D9 K
return false;
1 [/ x6 i; {; @- s9 K3 b. e}; - I/ M' ]' T Z0 ` r7 y
canvas.element.prototype._findcrosspoints = function(mp, ocoords) {
( h \3 S F, c6 t& p2 Q7 }5 J6 Jvar b1, b2, a1, a2, xi, yi;
! B- z$ D4 d- A/ G, Avar xcount = 0;; h( R8 |, Y! ]# s
var iline = null;" h4 K9 z( e! i" I
for (linekey in ocoords) {0 s- Z" m L2 l
iline = ocoords[linekey];
- h0 a" F! J( pif ((iline.o.y < mp.ey) && (iline.d.y < mp.ey)) {
; z. x1 S0 a4 a4 r, Zcontinue;
) g$ W9 [! d! e; y$ C; {7 y+ v+ g}. i2 {5 K5 {; @
if ((iline.o.y >= mp.ey) && (iline.d.y >= mp.ey)) {
6 s7 g0 V0 @# ?1 Dcontinue;+ C- j7 U/ ~) u2 C" h" r* o
}
, A% S, n0 q- N2 d+ Cif ((iline.o.x == iline.d.x) && (iline.o.x >= mp.ex)) {
2 }- N/ {7 r& J" S3 Fxi = iline.o.x;
) `& f( [* }* N# ~+ \- t% Oyi = mp.ey;* ?: G5 W' a5 X3 I( p7 ` X
}
# ^8 H7 i- {( U1 Melse {
! W- X3 @* y) Yb1 = 0; & j( I! L, v* n* N) C
b2 = (iline.d.y-iline.o.y)/(iline.d.x-iline.o.x);
* }" y8 d$ @% K# @# f$ l& y1 Ba1 = mp.ey-b1*mp.ex;
: f& Y" E! B" Z+ U5 F- Q) }a2 = iline.o.y-b2*iline.o.x;3 d( N X/ p+ E/ V$ q/ E
xi = - (a1-a2)/(b1-b2);
' V( H6 u$ j' P6 Y/ u$ l$ t9 [9 Iyi = a1+b1*xi; d+ f5 d7 T. d: [: ]
}
) `1 u6 q( E2 h' ?% g7 yif (xi >= mp.ex) { 7 {* q1 [' n7 m
xcount += 1;/ V" J* Z! t! \: o+ M6 w. L
}
1 s% H; i' p; G1 Jif (xcount == 2) {
; C5 q3 n' \# S' u9 Y6 B! t; Abreak;
7 b, @1 e. \1 R+ K" l/ P1 s}1 X9 [& y7 ]1 X% E
}
4 T) [! Z& N0 A: }6 n6 a- }1 x: r5 Creturn xcount;
6 E( z* S+ T) }: x5 U7 N};* e$ W/ A1 e. ^. H; b8 T8 n F
canvas.element.prototype.findtargetcorner = function(mp, oimg) {
7 F' \" c! o9 @: K ]3 V- V4 ^var xpoints = null;
. V c. ? C& T8 Dvar corners = ['tl','tr','br','bl'];
8 }2 f, b1 \2 {- n5 o6 Y6 w' ?for (var i in oimg.ocoords) {
( `& L# J* x& a9 ?2 lxpoints = this._findcrosspoints(mp, this._getimagelines(oimg.ocoords.corner));
0 a7 \2 V) c, N; @6 z# Kif (xpoints % 2 == 1 && xpoints != 0) {" y6 |( i i8 C7 }
return i;
' d8 ?* F% @' H' m2 P- J}
( i) A2 _0 w# ~5 T- R. p# ~5 L}. f. ]) B/ F! T# K7 @7 \5 n2 u
return false;
% F% l3 h$ n( g0 V- C};
. x. S) E, c: K3 W6 }# r5 d$ z, _canvas.element.prototype.findmouseposition = function(e) {
8 {* }- ]' l" @var parentnode = (e.srcelement) ? e.srcelement.parentnode : e.target.parentnode;
8 F. p* S l) f6 J% [3 i6 Pvar issafari2 = !s.support.ie&&!s.support.firefox;1 W. k9 `! [: y" S( k# }
var scrollleft = document.documentelement.scrollleft || document.body.scrollleft;/ A! D7 \6 P' e
var scrolltop = document.documentelement.scrolltop || document.body.scrolltop;! |: Y0 S3 d; g$ \% g: @/ ~8 O
var safarioffsetleft = (issafari2) ? e.target.ownerdocument.body.offsetleft + scrollleft : 0;4 v& u1 B0 p! r
var safarioffsettop = (issafari2) ? e.target.ownerdocument.body.offsettop + scrolltop : 0;6 l, t2 I# [9 g. z
return { U9 l$ F: z- g& c
ex: e.clientx + scrollleft - parentnode.offsetleft - safarioffsetleft,8 `4 g9 ?5 `" S& g# |
ey: e.clienty + scrolltop - parentnode.offsettop - safarioffsettop,3 | q% s1 R6 a
screenx: e.screenx,0 F! k( B( k3 e2 ]; N2 M0 [
screeny: e.screeny
; b+ p1 z1 r: C3 F. Q};. T0 C& E# ^9 q W- K+ T
};
: i- R( B6 b, Tcanvas.element.prototype.drawborder = function(context, oimg, offsetx, offsety) {% B6 E# E$ K5 ]8 G' t$ S- h q
var outlinewidth = 2;4 `/ X6 X* z' P* Z/ [3 b0 \# D, S
context.fillstyle = 'rgba(0, 0, 0, .3)';
' h' y/ a+ O% @/ n0 B3 bcontext.fillrect(-2 - offsetx, -2 - offsety, oimg.width + (2 * outlinewidth), oimg.height + (2 * outlinewidth));8 y8 D* b; [% B, Q. _ N
context.fillstyle = '#fff';
! K+ ]' u% ]& z& Q) ocontext.fillrect(-offsetx, -offsety, oimg.width, oimg.height);
/ g* c4 G5 l4 W* Q1 t* J};
) s& n% b( |0 T5 t, ~canvas.element.prototype.drawcorners = function(context, oimg, offsetx, offsety) {/ H7 ~3 Q* \& b3 \
context.fillstyle = rgba(0, 200, 50, 0.5);6 x/ ?7 v9 d8 p
context.fillrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
) b/ B" t( C4 r, v0 k( X! V5 Lcontext.fillrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);$ G" Z% T1 R) e3 }2 n. x
context.fillrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);& W7 \, m3 R' K- ]- k3 {; I
context.fillrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);% @% [% M! u- v' W5 p
};! w, K1 A# y6 j9 A
canvas.element.prototype.clearcorners = function(context, oimg, offsetx, offsety) {7 C+ Q% P0 X4 V6 M) D* a/ G; Y) m
context.clearrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
: h n! D9 d7 h% l5 z. Scontext.clearrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);1 Y" ?$ _' o/ F; ]: d
context.clearrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
9 Z$ [* q6 c, r; q4 Bcontext.clearrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);6 F6 V) \$ \. r4 D. \; l. W4 _7 r
context.restore();
7 v( X, o5 D' u% [% k/ L; x};
7 Y! p+ T. ]' n$ Ccanvas.element.prototype.canvasto = function(format) {
; c" m: B: H/ l. W( ythis.renderall(true,false);9 M; L' H/ H9 L6 d9 t e# k. g5 z" W
var containercanvas =this._ocontexttop;
: D8 g, y5 J4 G* e$ Xfor (var i = 0, l = this._aimages.length; i < l; i += 1) {
4 k0 q' ^7 E/ O {var offsety = this._aimages.height / 2;" O, _( |/ {( a/ S
var offsetx = this._aimages.width / 2;3 l* I9 o' X) ^* l! b
this.clearcorners(containercanvas, this._aimages, offsetx, offsety);5 j2 Z# m% p1 H4 E2 t2 L( H5 i3 b9 W% A
}
3 |- n+ |( j6 N5 }! z1 t2 D' I y1 iif (format == 'jpeg' || format == 'png') {, e3 j) W8 A" w, B8 j2 _# w, @# R
return this._oelement.todataurl('image/'+format);1 t0 T3 p4 @! l1 K
}8 `; j3 v5 P& j K/ v+ E9 l/ `4 g
};
f' Z) L/ J* w+ Hcanvas.customevent = function(type) {
1 P6 Y( F1 k$ J, f( Gthis.type = type;. v9 F7 e$ }5 J/ ^4 d, m
this.scope = null;# Y* g! {2 r: B3 l& P
this.handler = null;5 [# `% e3 m2 K. S( ~! H# \6 I
var self = this;
" n! O0 N! m2 u% I9 kthis.fire = function(e) {5 m s i: K) T# s/ J& c& K4 K
if(this.handler != null) {. g0 i( f4 u# m
self.handler.call(self.scope, e);: Q; l3 P& L3 W
}
' s( ?! @1 L, S! U* j1 p; a};
+ |4 w: |$ `0 h8 |+ ]; S}; 9 C& z# E8 k4 {* m9 H3 L
}());
* h1 H) j8 x+ n& ~return canvas;
4 L& O/ g) s4 r0 t, G});) Y( h5 V: G& [1 s# g
canvasimg.js代码如下:$ J8 g: O" {" B& D7 e- S: }' s( W
复制代码代码如下:define('canvasimg', [ '../multi_upload/core' ], function(s) {
9 }1 v" D2 C! s4 _& Z) Q; {, \3 l% Pvar canvas = window.canvas || {};5 v( h0 I9 L7 y( i. X$ w
(function () {8 p( r) V3 k: v- b: v3 w
canvas.img = function(el, oconfig) {3 P r7 p) l9 O. c. Q
this._initelement(el);
( `' p# B6 T9 E- m( @this._initconfig(oconfig);
+ V$ }! u0 F! @- i! A1 |( e! jthis.setimagecoords();/ [. L( m3 e" `* L1 S" V
};
' O4 X! q- i' A8 Dcanvas.img.css_canvas = canvas-img;; ?6 j& C+ v; X
var default_config = { ; A* B3 A7 S# H! _* g* S0 `: i
top: {
5 T; l% d( v) S9 n' D$ @4 ekey: top,
/ {, R1 \' C& i$ ivalue: 10 ) v, l# m6 n% ?: Y
},' h& S4 t8 k& {% [
left: { ! i2 G" R3 k1 `
key: left,
+ I9 s3 i2 i' y E, Y1 jvalue: 10
* q& w7 p) E h! s- i& s1 v},
6 [+ X! Y+ E' }- e( Q2 b! B# dangle: { / [/ ?8 r) E- a L
key: angle,
2 {3 `# I& `( K% L0 _4 ], Z4 evalue: 0 4 n; E% [/ z& b6 [
},! Q4 l$ u& c" u& D+ \0 a
theta: { , B; `4 |/ o m7 W" d. Q
key: theta, 0 F* @" A/ O! U
value: 0
& \! ~ C& B/ r8 p4 V},: Z; `+ f# u% T8 ` T
scale-x: {
. G% U1 x, o7 A/ Q9 Ykey: scalex,
# b1 C, T. E( N3 fvalue: 1
/ c7 K G( C1 f, o8 R8 `},
3 z% C& U, b4 A nscale-y: { 5 F( N* s4 K! B* M
key: scaley, , i! N) m" S: y2 d+ i' a% ~2 j
value: 1
( K Z8 i1 B- Y z. V}," J/ I. b$ j- `/ X3 w- Z
cornersize: { 0 \0 W* p* j% w: h& g+ J- M. E
key: cornersize, 1 m5 C. T4 R. e" r, q
value:104 _, R2 l4 f8 H X2 p3 M% {" @
},+ x1 n$ o" E+ R
borderwidth: { $ m9 R( k) t1 U8 M( C @
key: borderwidth,
( w0 }2 f1 ]* uvalue: 10
6 H: J: k9 y+ H9 h( ?& d},9 T+ O7 [$ t1 u9 t# M: r
polaroidheight: {, j( [ F" {5 t
key: polaroidheight,
% V; {& W( Q, Xvalue: 40+ S& d' S$ G0 ^& _) x
},
1 p: p, U9 K7 D( Q, _9 ]: d9 Drandomposition: {5 U7 g2 j" W2 h0 b" }
key: randomposition,2 x2 m* L8 x7 a
value: true" {! x1 l, p0 S2 Z) ^
}
2 Y6 j. f4 e! I% Q/ y: }};8 U5 E, A, z& v$ n
canvas.img.prototype._oelement = null;
/ b6 B8 d7 }' g% ?( f2 n' Lcanvas.img.prototype.top = null;
3 N: Z& c* `8 K' j+ z2 ^canvas.img.prototype.left = null;
" u/ w1 V) x, a& D! L; V" i1 ]canvas.img.prototype.maxwidth = null;9 H: x9 b2 t; L
canvas.img.prototype.maxheight = null;
, X& M) @7 l" e, m5 Ucanvas.img.prototype.ocoords = null;
O) t7 p0 p! D a: B- v% C& O4 ecanvas.img.prototype.angle = null;2 `9 Y: {9 J3 B) y1 i
canvas.img.prototype.theta = null;
4 h- t2 Y$ P; z+ {2 tcanvas.img.prototype.scalex = null;
0 f6 [6 N2 V- x; V. c3 Z" Qcanvas.img.prototype.scaley = null;
+ g# x5 G* H& o+ q1 Hcanvas.img.prototype.cornersize = null;3 E: B0 r" _$ D# h/ t! }/ m
canvas.img.prototype.polaroidheight = null; g3 y* O0 {2 F' O. i. ^) W; A
canvas.img.prototype.randomposition = null;- {3 `. }2 h6 W5 Z% a$ \+ i. _
canvas.img.prototype.selected = false;% ]' e; J/ D+ `# Y5 P# A U) [
canvas.img.prototype.bordervisibility = false;
0 C( [4 Y. G" `3 d& H) ecanvas.img.prototype.cornervisibility = false;* w/ [6 H3 t8 u/ ]1 B! F
canvas.img.prototype._initelement = function(el) {
6 p( j6 T. d9 O; mthis._oelement = el;
8 V2 J5 W, I0 [1 ~" t8 m};! U6 K4 A9 t5 g2 Y8 N- k
canvas.img.prototype._initconfig = function(oconfig) {
* j8 D/ T9 O" l8 E4 qvar skey;8 n" S. N# x8 L* S4 m' y+ r
for (skey in default_config) {
( L3 D( ]# O: N# q! svar defaultkey = default_config[skey].key;& u$ s% t: W5 y
if (!oconfig.hasownproperty(defaultkey)) { // = !(defaultkey in oconfig)$ A. g! W( k0 ^5 ?' a, v6 g
this[defaultkey] = default_config[skey].value;) K$ c- W+ d+ l7 q( O" p
}& e; ^, z f* I( L$ z+ V8 w9 t% B
else {: p) a9 z8 w. j& K; {
this[defaultkey] = oconfig[defaultkey];9 N$ O+ t& O2 r" G" ]" P) ^
}
/ l4 g( E0 b# ~+ L}
, M6 f" j7 K, F, q. C2 `% Gif (this.bordervisibility) {
: s, i) C4 ^7 l+ M6 othis.currentborder = this.borderwidth;
/ I# |) c: N' j}: G' C" g+ ^' j. r! A& t
else {5 P% d* w4 O `" M$ e3 F. p
this.currentborder = 0;
, S3 v+ Q' M- ~}
6 A0 F$ m" _, H7 n; vvar normalizedsize = this.getnormalizedsize(this._oelement, parseint(oconfig.maxwidth), parseint(oconfig.maxheight));' V4 f. r- V: P* p3 q
this._oelement.width = normalizedsize.width;
! j* ^2 O& G! V( c& kthis._oelement.height = normalizedsize.height;/ P3 ~& E1 k) r. n" K
this.width = normalizedsize.width + (2 * this.currentborder);
7 b- A) a7 M4 U0 m7 c- C3 U/ C4 Bthis.height = normalizedsize.height + (2 * this.currentborder);9 H* {# g. Q2 u" j! l# \; J3 U
if (this.randomposition) {
7 M3 |# o# [% D, gthis._setrandomproperties(oconfig);+ o1 j3 ~. B" ~1 {' }& X
}. O( Y7 e; U+ F6 ?9 y
this.theta = this.angle * (math.pi/180);
- u! ?1 I$ Q L) @};
7 b1 j- U% s$ q' L; Scanvas.img.prototype.getnormalizedsize = function(oimg, maxwidth, maxheight) {- o' L- J1 }4 [
if (maxheight && maxwidth && (oimg.width > oimg.height && (oimg.width / oimg.height) < (maxwidth / maxheight))) {1 T- a6 |9 c. R- R
normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);
0 |9 }# R! {. c6 p, H& V* l3 Fnormalizedheight = maxheight;
* k+ ]2 f9 |* S$ V}9 r0 \- @9 A1 J
else if (maxheight && ((oimg.height == oimg.width) || (oimg.height > oimg.width) || (oimg.height > maxheight))) {
' a: _2 U, M1 }normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);: j H! x# u$ I5 y. C% o
normalizedheight = maxheight;% _. ~. h4 O' A3 Y0 a
}3 B* ]0 `1 |: G
else if (maxwidth && (maxwidth < oimg.width)){ 7 Z3 c. _- N. }& W" r! X7 V
normalizedheight = math.floor((oimg.height * maxwidth) / oimg.width);, g4 J4 ^# x% p) l9 q- I/ @
normalizedwidth = maxwidth;" K3 @3 e. z1 M: B; I7 m
}
8 ]5 g7 {2 e# Belse {
8 r1 \' \5 v- X' R! enormalizedwidth = oimg.width;
: ^* S! i8 m1 ^: n# A' Snormalizedheight = oimg.height;
7 h: H4 x3 X2 n3 j' n; }& ]}6 W6 b4 I8 ^, l* J
return { width: normalizedwidth, height: normalizedheight }! ?+ ]. A% G( k( c* D. _
},; k% i& o9 c2 }: T* v5 b0 t
canvas.img.prototype.getoriginalsize = function() {
0 v1 w, `+ k2 v* x+ breturn { width: this._oelement.width, height: this._oelement.height }! K3 b$ J9 ^! |0 i
};4 p* w& I" p `- p
canvas.img.prototype._setrandomproperties = function(oconfig) {. @! U2 i& b; |5 O, r
if (oconfig.angle == null) {
) j Z9 X: m1 }7 [/ cthis.angle = (math.random() * 90);5 k8 \7 ]1 z; ?7 S4 \7 S
}
6 a# s4 J- y1 S/ @- @' s& jif (oconfig.top == null) {
" U7 D' q$ m" H, o! othis.top = this.height / 2 + math.random() * 450;" k. s1 g! P& l0 z8 {: G! S
}
" w' J- ~ C( I6 s. }) y" z5 pif (oconfig.left == null) {* u% \+ q) I: j! o; ^
this.left = this.width / 2 + math.random() * 600;% Y4 v- s2 q9 ^. f
}
1 t$ T0 _7 r. \};
# e2 M* U) ~7 i# Q! w! Jcanvas.img.prototype.setcornersvisibility = function(visible) {) Y% T8 p% N6 W7 I! Y! C
this.cornervisibility = visible;& a- `: D/ L, V, I
};
9 v& }! S+ j0 h) S7 N9 t: D. Rcanvas.img.prototype.setimagecoords = function() {7 M6 G5 e2 H. ?5 C3 q [' u. d
this.left = parseint(this.left);; ]4 w2 w& q$ g; z6 l
this.top = parseint(this.top);. P2 `0 k4 ^7 ]$ T4 f
this.currentwidth = parseint(this.width) * this.scalex;+ ^: n2 V5 V: G: o; b* w" B
this.currentheight = parseint(this.height) * this.scalex;4 o& V! J: {! h9 x# H
this._hypotenuse = math.sqrt(math.pow(this.currentwidth / 2, 2) + math.pow(this.currentheight / 2, 2));+ a$ Z( L# N( p" a: V) x, J
this._angle = math.atan(this.currentheight / this.currentwidth);
$ b( h/ A' S- I2 cvar offsetx = math.cos(this._angle + this.theta) * this._hypotenuse;
8 N" h/ a/ i; lvar offsety = math.sin(this._angle + this.theta) * this._hypotenuse;) _8 U7 n4 ~% S8 b
var theta = this.theta;
' G) N' m6 S, e5 q4 l1 gvar sinth = math.sin(theta);* ^1 W" I E$ r; l$ }2 D7 w
var costh = math.cos(theta);
, A7 x& m+ T& S3 ~) bvar tl = {
c% v. f3 x4 {- y" o: lx: this.left - offsetx,) [# s1 Y- p' E1 G# k! k* k' o6 p$ x
y: this.top - offsety
" I# @& p5 d6 k8 ?* I5 O5 U+ Q};& d# l( R; K% ^4 j2 |0 e
var tr = {4 ~4 P) T& F# l. W" L: x
x: tl.x + (this.currentwidth * costh),
! ~7 W; U5 Z Q; ry: tl.y + (this.currentwidth * sinth)- x0 Y' v" H- o& _6 X7 ?
};$ G! F) Y1 W2 ^( X$ T6 W7 Q
var br = {. K& ?) Q. b: g
x: tr.x - (this.currentheight * sinth),
) D, h3 F* w3 ty: tr.y + (this.currentheight * costh)# Q- M& v$ Y3 c" S6 g/ P3 L2 [) x
};
- M+ T$ t# `: K4 b5 ]! Yvar bl = {
( ?0 s6 N |% I, ~2 \3 cx: tl.x - (this.currentheight * sinth),/ [$ C* A9 N3 T- S
y: tl.y + (this.currentheight * costh)" x! F+ u/ L. K% V, V. k* D
};) A1 e4 }' D2 o+ C6 Y! }
this.ocoords = { tl: tl, tr: tr, br: br, bl: bl };9 n# F4 e$ y# t% e. k" Z4 S+ L
this.setcornercoords();
, {* s9 r2 ?/ b5 t7 C# F};: g. |1 a& Y, x3 A; K p: Q$ b
canvas.img.prototype.setcornercoords = function() {
, W! p+ b+ s* H7 mvar coords = this.ocoords;1 r" Z! e2 w ?
var theta = this.theta; P( U7 n2 u4 k/ ^6 F: D5 m
var cosoffset = this.cornersize * this.scalex * math.cos(theta);
( t" Y) a2 k2 dvar sinoffset = this.cornersize * this.scalex * math.sin(theta);: u2 `/ X3 D( _2 b4 d& t- E: J2 n
coords.tl.corner = {% `- b- k" n3 @+ y0 m8 e! s
tl: {
) D3 o0 _/ s! Y2 o; Q, ~! ~x: coords.tl.x,. l n/ O$ W& O/ S7 h
y: coords.tl.y
; M$ i8 X+ Z7 T},0 I! E/ G* q- D' }" j- m5 k
tr: {
) d1 |! N+ C; a. ?x: coords.tl.x + cosoffset,( ?' [) u/ O% o7 r6 q/ V( O
y: coords.tl.y + sinoffset4 y9 A& W3 G4 r- f# C1 R6 K
},
2 ?( u7 Y! s7 r" q$ cbl: {
`) u2 D; D- f8 }9 jx: coords.tl.x - sinoffset,' P" c6 j# K* R0 X" H; v6 v! |6 O, j! q
y: coords.tl.y + cosoffset# l7 R! y8 R7 C; N
}
0 A6 B* z) r6 B, k# V6 I, w) B2 p};
- o0 Z/ A1 r3 m- y8 ecoords.tl.corner.br = {4 p2 a1 M9 @! E
x: coords.tl.corner.tr.x - sinoffset,7 }$ o6 r) _& [
y: coords.tl.corner.tr.y + cosoffset7 r* \4 O* N5 p. {: a& [
};# F' K* s1 L7 V5 c/ m+ P" v) C7 R
coords.tr.corner = {
, ^' U2 t- A$ W; ktl: {
) U3 I. t5 E1 f9 |x: coords.tr.x - cosoffset,- ]( j- { W) F; m/ y- d3 b
y: coords.tr.y - sinoffset4 @7 w7 ^7 x: X/ @( i" P l8 `
},
: Z; ?4 l/ z b: t7 b5 X# btr: {" H" r6 ]( c' c9 X; ]4 }
x: coords.tr.x,
" P) q! E0 X9 h5 R% G& ?y: coords.tr.y
. U; B' z' n# V/ _2 i7 h},
% p7 Y% T( y- M) A6 O9 b% Q- f$ y/ \3 ibr: {' k; e/ M( U# j# r
x: coords.tr.x - sinoffset,
3 @+ S1 s" T/ d$ dy: coords.tr.y + cosoffset5 E4 r7 ?/ R6 |! q! ^$ H
}; S/ z* T/ I/ x, M/ I: e7 C4 M
};# \: ^3 n2 ^2 Z1 ^8 A
coords.tr.corner.bl = {, t: D1 m+ |& r2 d! c }* e
x: coords.tr.corner.tl.x - sinoffset,
$ h) l4 x! U s2 v2 r: my: coords.tr.corner.tl.y + cosoffset
1 `0 B& A4 r9 c! C8 ~9 S& C% W+ }};
0 w( e0 N" m9 h) }. Icoords.bl.corner = {) n' E) X: m. i8 S
tl: {+ ]; v$ Y' h+ n( `# a% ^ t( B
x: coords.bl.x + sinoffset,; [$ w1 R7 F* |2 _ Z0 B
y: coords.bl.y - cosoffset! M/ k; }' T+ p& R
},
' `5 t0 H% L& Sbl: {
$ `" w" j r% R" J* S# m: Gx: coords.bl.x,
+ Q) m$ t2 M7 v7 P- V" ry: coords.bl.y6 C7 p; v+ P+ ~2 Y* O1 |5 z
},
; e8 z- x* \2 O$ ]br: {
5 g' i/ h$ }. {8 C% ?% Lx: coords.bl.x + cosoffset,, T6 a4 x% ]2 M. i9 x" b4 e
y: coords.bl.y + sinoffset
/ G& K; l" c% ^}
3 f0 T8 D# c7 r0 N# w% i9 n};) \ m# k5 S, w& H( A3 B
coords.bl.corner.tr = {
6 B6 n( E# N2 z9 R7 n- Fx: coords.bl.corner.br.x + sinoffset,1 l q) b% s+ V9 R2 a: _
y: coords.bl.corner.br.y - cosoffset
" t0 S# z/ n g& O% G8 X};0 S& @' J% f; J
coords.br.corner = {' @% I$ H! C1 `9 n( U1 K, b
tr: {' [$ g" U$ c8 K& q/ A- q
x: coords.br.x + sinoffset,
$ z3 H! ~! U3 Cy: coords.br.y - cosoffset
; @0 U: }! p3 m1 u3 P},8 R$ E' n& |3 m3 o' R8 O8 h% o
bl: {- C8 y* p8 X' H7 |$ N) c& ^ {
x: coords.br.x - cosoffset,' G0 R3 ~ }9 ~) M
y: coords.br.y - sinoffset" y( k. u( b' k
},! t$ h, Q& }1 k0 E. ]
br: {
6 H+ k% W& g9 _x: coords.br.x,% u% n+ c. Y7 v: e1 |: @
y: coords.br.y
# X) f9 ]! j, Q' f} ?0 T$ ]% M% o( a6 p# P1 n
}; m J' o! E2 {* z$ ?0 v7 K
coords.br.corner.tl = {
7 B* |, u: [9 ix: coords.br.corner.bl.x + sinoffset,7 w: y5 a* z$ a" h' \/ [8 x$ y
y: coords.br.corner.bl.y - cosoffset
4 m4 v) A9 v4 Y& C* ?, C};0 i1 q. O9 B( l& F e: R, E/ \+ D
}; [* c! {% {/ [$ S$ s/ I
}());: B9 o) v9 u( |4 n8 Q3 J% ]
return canvas;( Z: q2 f/ D1 E9 H
});
) a+ _% L$ s/ k8 R1 ]( s1 n5 apuzzle.html代码如下:
- h" x( n$ f$ v d复制代码代码如下:<!doctype html>: u# q4 w8 D7 g' n# \' p% D
<html>4 T0 B5 v2 G! ^: N4 T
<head>
1 y# U! Z8 `' G) m) w) ~, D<meta http-equiv=content-type content=text/html; charset=utf-8>
1 E+ e* d% d" m' V% k# f<title>insert title here</title>+ |6 w4 j# _+ d# r3 q! ^0 [
<link type=text/css href=html5_puzzle.css rel=stylesheet />" A; |3 I" [& z$ G6 d7 v' l+ F- _
<script type=text/javascript src=../multi_upload/seed.js></script>
# R) q& H7 b+ N% q' O+ W9 g/ ~$ b3 C<script type=text/javascript src='html5_puzzle.js'></script>
+ e7 ~0 I: _4 r) ^2 o& _2 q</head>
) O/ l* f0 Y; p/ q/ m9 n5 p7 H<body>4 N0 o4 R; h6 T. c D# ~8 w
<div id='html5_puzzle'>
( t( N7 A2 D, b J4 g" X* |<div id='puzzle_left'>
6 e: ?- G8 g0 A/ K<div class='puzzle_column'>4 r1 h5 }. n, e4 s4 b
<ul>3 }0 ?! d5 ?4 Z' U* F' j
<li><img src='small_img/1.jpg' data-index='1' /></li>. `% u6 I, C7 t
<li><img src='small_img/2.jpg' data-index='2' /></li>% i6 P$ Q! a% g1 V: T, X. {
<li><img src='small_img/3.jpg' data-index='3' /></li>
4 I! W8 A t6 S<li><img src='small_img/4.jpg' data-index='4' /></li>$ f2 O0 i+ H4 L" p* c8 W' `* |
<li><img src='small_img/5.jpg' data-index='5' /></li>
. ]+ J; u, I0 t% _8 [0 K& V6 R</ul>0 X7 R3 j5 F- T
</div>
' ]1 |! U% k0 T7 C<div class='puzzle_column'>
, Q P2 v% S) N& n8 T. w<ul>4 a6 M/ a& e: [& i( m
<li><img src='small_img/6.jpg' data-index='6' /></li>
/ p! t0 K! I- \' o<li><img src='small_img/7.jpg' data-index='7' /></li>3 ^/ ~8 E! F9 R
<li><img src='small_img/8.jpg' data-index='8' /></li>
) X' m& Z9 H; F3 ~<li><img src='small_img/9.jpg' data-index='9' /></li>' ]4 I4 @: u, q
<li><img src='small_img/10.jpg' data-index='10' /></li>
" X7 a/ A& k& B9 L$ W) j</ul>
- \$ W3 ^* U, B( ?' ~- m</div>8 v& b% ?" M- z- N1 e
</div>
" \2 B3 P9 t* e! I9 p. W( z* t<div id='puzzle_right'>
( c- i& u( r( p4 R( {% z<div id='puzzle_canvas'>3 \0 t. Z! `- ^$ c
<canvas id=canvid1></canvas>! o, L" F0 t& l
<div id='canvas_menu'>9 y) I1 f [% |' \7 D9 ]
<a href='javascript:void(0)' id='photo_delete'>删除</a> <a
4 ?+ j% y% P" Ihref='javascript:void(0)' id='photo_update'>更改图片</a>0 U% K/ a( q5 z6 X. L1 S
</div>
. e0 s8 G- Y6 I8 M- p% Y% k</div>
8 ?, r: ~- J/ F<img id=bg src=big_img/1.jpg width='600' height='450' />5 ?& H; e1 a# H- |9 k/ Z
</div>
7 s: r# w ~4 O! c* x<div id='puzzle_bottom'>
7 T4 \; o1 p7 L* J& [$ o+ S<a href='javascript:void(0)' id='add_img'><span>添加图片</span><input! L) N/ k) y2 M; k! w u6 R, K1 Z7 |0 |
type=file multiple= id='fileimage'> </a> <a
# U* \/ {: S/ _( Shref='javascript:void(0)' id='upload_btn'>上传</a> <a>点击图片可以旋转,拖拽,* {. D6 `( o6 R4 F+ W. ~
缩放哦!</a>
" W3 {8 }- Z! ]3 @3 {</div>
4 Q) o+ A8 ~! D9 H</div>: H7 N* h! d' F& u# M% a
<input type=file id='test'>
5 `8 I- _$ L Q( A% M" H3 s<canvas id='test_canvas'></canvas>
$ q# C6 E$ D; e6 Q& v</body>' y e# e0 ]) {' B2 I8 N z
</html>
+ X$ |# _. {7 L/ f+ n9 Bhtml5_puzzle.css代码如下:9 u1 n5 p, b H+ s& r3 I3 T9 H/ D
复制代码代码如下:@charset utf-8;</p> <p>#html5_puzzle {
0 I0 ~% @$ ?, M$ d: Dfont-size: 0;( C/ F2 {0 D) ^# ?/ U. [/ I# \4 l
}</p> <p>canvas {
% B( f% ]3 i- ]background-color: transparent;
[9 y' I% A6 O* A7 f& }! y4 vleft: 0;$ O5 p4 ^. c. J4 @- g
position: absolute;
& A! i( V2 k3 btop: 0;
1 d1 ~& X3 P6 E5 s& }. q}</p> <p>.puzzle_column,#puzzle_left,#puzzle_right,#add_img {" Q/ ^9 W2 g) w: b) D* a a9 d
display: inline-block;. x( {5 y) W" C) [
}</p> <p>.puzzle_column li {& q2 W r9 w: a3 d j4 {) {6 ~
display: block;" f# M. e' |( F [- U; K
margin: 5px;3 c1 l( ^: I/ Z
border: 1px solid #ffffff;: b/ n4 w9 ?8 R" s/ i
}</p> <p>.puzzle_column li:hover {" C: x( Q5 a/ i) I+ r" h. |5 G
border: 1px solid #3b5998;, r+ m1 z0 O; n; V
cursor: pointer;
- T4 x P" [- h# C3 B I2 O# ?' N}</p> <p>.puzzle_column {" E% S+ W; ]* R
font-size: 0;. @8 U& j0 y4 U: A$ M3 j+ M# K/ C
}</p> <p>#puzzle_left,#puzzle_right {9 `6 v5 J: G; M0 o0 v
border: 1px solid #3b5998;
; l/ E! _+ w9 n& X. S `}</p> <p>#puzzle_right,#puzzle_bottom a {
+ n. g3 m) w/ A) J' Xfont-size: 14px;
8 y u2 t5 \9 S- ^margin: 10px 0 0 10px;
' X) Q6 T5 D8 w( E}</p> <p>#puzzle_bottom {
+ f+ [- z9 N/ C% v) @margin: 5px 0;' v9 Z' {& P7 k1 C: m1 A% t! _
}</p> <p>#puzzle_canvas img {
* R: I4 y' m/ }( H6 d}</p> <p>#puzzle_canvas {. N1 n4 S4 A. M" A: Z9 d+ ~8 J
overflow: hidden;
9 ?$ Y+ b# k! Fwidth: 600px;
; ~ U+ |8 v5 a: ]* Pheight: 450px;6 h& Q8 n6 {/ e0 \ ^
position: relative;- U& H# u4 z; g! l8 b; _
}</p> <p>#add_img input {
1 f& W% C/ R9 R' C: ]% Cposition: absolute;
1 a; e- |3 n7 \8 F% c% Jfont-size: 100px;
; t% i6 F6 E" V/ Zright: 0;/ B1 q/ R9 U0 {1 f! ]
top: 0;9 e7 Z/ z' J# t3 {0 G6 I
opacity: 0;
6 \6 s6 k. a; x2 D- x( a/ k# z}</p> <p>#add_img {8 L" Q8 ^3 n' K# m
position: relative;
& K j2 v4 V) {( v3 z: q K Z5 Qdisplay: inline-block;. J1 F# P' I9 C8 \0 p& h- d- l
background: #3b5998;
6 ]% D! P. e; y$ R' C* [( xborder-radius: 4px;0 U2 C% m% [4 B* C6 `. ^2 ?
padding: 4px 12px;! ~/ c+ P% b# ]2 W- k
overflow: hidden;. N8 R$ t! j2 N' j! i5 k" ]9 Z
color: #ffffff;
6 G+ H# N: X( @" w, V}</p> <p>#bg,#show_list {0 `6 Z' ?" ]" R. ~! i& g' l: I
display: none;! f/ `8 Y; ^8 U. p+ l
}</p> <p>#canvas_menu {
. {: |2 b& K# E: _8 Z2 I* z; Iborder: 1px solid red;0 x9 e) x0 U& ^0 Z
position: absolute;
+ j2 }3 P% G1 ?# j% b( ]8 B% a6 {# {4 Zz-index: 5;9 v: P/ y0 x, Z# @, Y
top: 0;
* n; K4 T+ u" m" h* Wleft: 0;3 m1 K$ ] Y8 O) y& I, Y
display: none;' Y3 z5 s- N/ m
}</p> <p>#canvas_menu a {% b8 M! S Y k# z
display: inline-block;: {2 }. S9 m. j! Q" u! s
}</p> <p>#test_canvas {+ U: g) Q9 i" D" o; X
top: 700px;8 ~) S& T5 z" f) w( Q
}" y- D; J+ K3 H+ o" z) |) m
html5_puzzle.js代码如下:3 n9 E# E! E6 ?, b
复制代码代码如下:require([ 'img_upload', '../puzzle/canvasimg', '../puzzle/canvaselement' ], function(
& W; x. o. y& O5 Ss, canvasimg, canvaselement) {
3 y4 E1 V' D& a# b y9 w, zvar img=[];/ G" n% h5 f4 b4 r0 f$ M9 }
var canvas = new canvaselement.element();1 s5 Z- V- M* \5 e4 W( D; T" M
canvas.init('canvid1', {
7 ^) B" ], Y5 P8 b+ e! }width : 600,
9 g; L! Z/ c( a3 qheight : 450 i' s2 |2 _3 n- n
});
* ^' X; r! ]- [0 c( Qs('.puzzle_column img').on('click',function(e){, g6 T# m3 J3 n+ Z4 Q; A
var index=this.getattribute('data-index');
* R' k$ v: p* T0 l# G$('bg').onload = function() {
/ @6 o2 t: x5 V3 o, Wvar ctx=$('canvid1-canvas-background').getcontext('2d');
5 {, o/ j; W5 S5 U: _) gctx.clearrect(0, 0,600,450);4 W' L8 |, W% j9 E! E' f
img[0]=new canvasimg.img($('bg'), {});
; r: L7 v+ v5 B6 K. c+ Q K& acanvas.setcanvasbackground(img[0]);( S% s: w/ Y7 D+ a, {) c% {
};* x4 D) D4 I3 m6 t5 c
$('bg').setattribute('src','medium_img/'+index+'.jpg');
B) v0 T+ e+ R: f! Y We.stoppropagation();
- F; X* ?2 O* |- s& ]+ G});: ~9 g3 ]) l, V4 O2 h2 n
var canvasdemo = function() {
9 A1 r/ P! p5 W1 m5 }: jreturn {0 c- _8 O4 }, Y: k) i
init : function() {
0 k5 p5 W2 w6 Gvar img_list=dom.query('#puzzle_canvas img');
$ w: l4 ]$ z7 Z4 `( l, ~- V5 E' Timg[0]=new canvasimg.img($('bg'), {});
, m ~/ Q8 a4 A" j5 \s.each(img_list,function(i,el){' ~- B; i' a( [: z) o
el.setattribute('data-index',i);
# J+ Z9 s; {3 u, U5 W6 L" \8 Kimg.push(new canvasimg.img(el, {}));3 b c% g" E- K1 ?3 o
canvas.addimage(img[i+1]);
0 G3 a' j! X; @) z3 r2 e6 v});
+ v/ l* j$ ?1 ycanvas.setcanvasbackground(img[0]);6 N3 _4 ]' ~5 b1 z! K' h2 m% T
this.cornersvisible = (this.cornersvisible) ? false : true;
7 G' k, R7 @# _, c% h9 u% f Z2 Dthis.modifyimages(function(image) {% p [, {' G3 u4 S1 w9 E* U
image.setcornersvisibility(this.cornersvisible);
6 R" N$ x/ E, O" O* |});
) \" |8 u3 E/ v' i7 R* S},2 ~; l+ W; \& A) |! {. U
modifyimages : function(fn) {
0 G3 d1 t U D( qfor ( var i =0, l = canvas._aimages.length; i < l; i += 1) {/ S4 l8 l7 k; b4 B
fn.call(this, canvas._aimages);0 ]2 H |# d1 Y2 s+ C/ }7 ~
}' J: Q L9 c; y7 H
canvas.renderall(false,false);
1 |+ D. W1 Z1 Fs('#puzzle_canvas img').remove();
. k' G; j; H _! t* G- I/ }img = [];9 M6 n# ^- j: ~; h' X* |, J- j5 u! _9 t
}
@ S, ~/ {& v, s( E7 Z* v};7 x; K' g0 i" e7 k9 U/ g0 }8 Y
}();3 p+ |" V6 Y9 \; f `" O
function getcurimg(){/ X- `5 O$ I3 L8 D" r
var oimg=canvas._prevtransform.target;( V9 `! z( E3 K9 I+ M1 }
for(var i=0;i<canvas._aimages.length;i++){8 {. }! A6 p5 D: L, X8 |! ~
if(canvas._aimages._oelement.src==oimg._oelement.src){8 O" V: l7 Q) w, T' r
return i;
6 }! a1 c8 C' l" z/ A}
' b c; s1 Y0 ?7 ]$ X}( k$ W2 V4 H# N
}
+ W& j% z9 ~" C) Ps('#photo_delete').on('click',function(e){$ I! t) G: L, X2 F
var i=getcurimg();
! L% s+ l; D4 l1 U& b2 ?: j% ~canvas._aimages.splice(i,1);
$ Y; P( ?: D, C* \1 s$ r7 [canvas.renderall(true,true);; L# {) H+ q0 ~( {; a
$('canvas_menu').style.display=none;
, g: W. O3 |# ^2 K- ]});
0 T$ }7 x( T7 F/ Y# i8 ^s('#photo_update').on('click',function(e){ o1 F9 \) m9 _
$('test').click();
3 q0 ~; A3 ~; A/ m0 {; Z( k});- a+ t- y K) g4 v
s('#test').on('change',function(e){
* @* I9 Z2 a0 E+ z0 w7 `: svar files = e.target.files || e.datatransfer.files;
( C4 D! D o3 O# z' Z& }" p8 x; qvar reader = new filereader();
$ z: F/ Q7 O- Q1 G! Xreader.onload = (function() {4 I, x: Q: p& ^/ Z, H
return function(e) {
2 `1 _" E! N) J. H6 ~' @var dataurl = e.target.result, canvas1 = document.queryselector('#test_canvas'), ctx = canvas1.getcontext('2d'), img = new image();1 f% s% s: R7 g5 q L q% _7 b
img.onload = function(e) {& k: _8 z1 N5 e
if(img.width>200||img.height>200){+ ?) ~6 O% p; H4 b5 H
var prop=math.min(200/img.width,200/img.height);: q. |# H, H6 r& u: _% v3 i2 T
img.width=img.width*prop;
* y; o9 S( }% y+ A" p3 limg.height=img.height*prop;
& S8 |6 L: R9 W}
% D' u0 D( v/ K" h( j; B. h( t# Jcanvas1.width=img.width;' _* i; l' u) R8 M0 Q; C( |
canvas1.height=img.height;/ ?# ^/ r; G+ p$ Q# q
ctx.drawimage(img, 0, 0, img.width, img.height);+ d j5 n* W# [: I- M: {1 Z! x, h/ b8 W
s('#canvid1').html(s('#canvid1').html()+<img src='+canvas1.todataurl(image/jpeg)+'/>);' a+ x% t) ^7 e& `* u
var t = window.settimeout(function() {( F4 k' K, B( F& o- P
var i=getcurimg(),target=canvas._prevtransform.target;
. n j, C, K8 L$ Dconsole.log(target);- L" X: z: L! z" G
canvas._aimages=new canvasimg.img(dom.query('#canvid1 img')[0], {
) s2 g1 l* Q Q) N& ~& c# Itop:target.top,
! n2 A; Q% o: P& xleft:target.left,
7 x, ~$ t5 @3 x9 uscalex:target.scalex,! R% c1 X, x+ F
scaley:target.scaley,
( d" y7 T: _( i/ j" z" y5 xangle:canvas.curangle
( n. w% D, X6 {5 I6 k});$ x L% G- |: m( q% I- j6 u
canvas.rendertop();
$ c0 A* \+ m* E; C# mcleartimeout(t);
$ n1 l( E/ s0 ~+ Zs('#canvid1 img').remove();
) ]1 x+ f- @4 c, d/ S},1000);6 n6 S1 P* m0 }
};, `% L$ G0 r6 n' c
img.src = dataurl;+ U* F* }8 J. B: G
};8 D0 I* y, g5 L
})();
6 L4 P! s1 w5 mreader.readasdataurl(files[0]);. ~/ _9 [: J, X1 m& Q2 v6 ]
});
; S2 [5 T& P* h5 n/ s8 q) ]s('#upload_btn').on('click',function(){
7 c r* T6 i/ `& q1 O. H avar imgdata = canvas.canvasto('jpeg');
0 R& t: d% ]9 z% X' ^& hvar imgvalue = imgdata.substr(22);
Y& L4 y5 a& @' f. Ms.ajax({
) N0 U# S3 `6 a curl : 'http://localhost/html5/upload1.php',
3 e) l6 a# r8 _- ]% Xtype : 'post',
( |: E1 _' _. t) T6 v1 Jdata : {
0 G' }7 b8 N2 A6 himgdata : imgvalue,
! i( i# j$ @, `2 mfile_name :'mix_img.jpeg'
T. M4 U; q9 d; g},0 [/ N0 ]6 z. ]: L- y6 u4 ?* @
datatype : 'text',
, F& ^8 l$ y" Q: E" U' _0 ysuccess : function(data) {
! P+ V a+ n8 m( e. p2 w0 D* walert(s); n. T" @3 X9 ?6 Q# D+ h/ p
}1 V/ v+ \1 H" r2 _
});$ C( p& |$ e" W$ ^5 i0 a! n, }
});
! Q. ]4 _: E4 c% Z6 a});& T2 O2 s3 U8 O E
至于用html5 input读取图片,这很简单就不贴出来了。
: U* g1 P+ \, ]) q8 B+ i. K% K( C# I9 R; G1 {, F6 J
更多网页制作信息请查看: 网页制作 |