返回列表 发新帖

html5版canvas自由拼图实例

382.9k 8
swmozowtfl 发表于 2015-7-10 22:26:00|湖北 | 查看全部 阅读模式
本文实例讲述了html5版canvas自由拼图的实现方法。分享给大家供大家参考。具体方法如下:
$ G2 J& d- S$ f0 y) t代码运行效果如下图所示:
/ I7 Y' T) O0 X2 Y  X) P* |* X  E
/ G4 s. k' T* Y% c  \; |" o
                               
登录/注册后可看大图
" ]4 _# C' t1 W" s+ D% k
canvaselement.js代码如下:
9 z4 _; G% F1 ]/ Z& H$ e' M$ a复制代码代码如下:define('canvaselement', [ '../multi_upload/core' ], function(s) {
% \& V# [  u# x: |var canvas = window.canvas || {};( ?9 [* l- Y0 W7 K: |
(function () {, {5 S$ e) g' B: G
canvas.element = function() {}; ) C+ H) X/ r0 A/ A; _
canvas.element.prototype.fillbackground = true;& K7 \3 V  ^- T% ]
canvas.element.prototype.showcorners = false;
5 w8 x! k0 T8 b7 S0 S4 o/ dcanvas.element.prototype.photoborder = true;. x( m6 D5 f5 `' T, ^
canvas.element.prototype.polaroid = false;$ p% |0 d# |8 D+ x" V
canvas.element.prototype._backgroundimg = null;# q' x7 b3 S4 p0 R$ O! A8 q$ b
canvas.element.prototype._groupselector = null;  O9 v5 D: i2 s. V0 Q
canvas.element.prototype._aimages = null;
1 C0 `' N2 _1 Ncanvas.element.prototype._ocontext = null;
, J+ R; V( E! N4 m6 \) lcanvas.element.prototype._oelement = null;7 }# P' q, w6 g( d+ C# T/ x
canvas.element.prototype._oconfig = null;% x- {3 y: U# U  Q5 R4 x- Z
canvas.element.prototype._currenttransform = null;5 Y$ M- n2 m0 z& X
canvas.element.prototype._prevtransform = null;
7 d4 a6 B4 b! v$ M  n7 T( zcanvas.element.prototype.curangle = null;
! d/ M6 Y1 y7 }  Bcanvas.element.prototype.init = function(el, oconfig) {
( y" s# T4 `9 {: Q! t' n% t" Oif (el == '') {: s/ o: i0 @7 Q1 {, H; v0 ]4 u
return;
) q8 [/ \% X0 D: P5 g) j" M( ^}( _: }7 b9 K. g9 S
this._initelement(el);
* B6 _) L& }: q2 A6 Z8 athis._initconfig(oconfig);  z& ~( t% q/ q  Y6 U$ A) u5 Y# e" |
this._createcanvasbackground();1 P2 K$ V! v9 D' G. T) q' y
this._createcontainer();- W: t% C* A0 e, M- i( O1 p# o
this._initevents();# Y  U+ T2 U6 U# }& e: U
this._initcustomevents();9 T- g' \3 X5 E3 S2 @% b, C, t- H
};) L1 T$ m# k" g' F8 }7 G1 N
canvas.element.prototype._initelement = function(el) {  P* r5 ]/ }1 X5 s2 s, o
this._oelement = document.getelementbyid(el);
9 _! f  {% Y2 tthis._ocontexttop = this._oelement.getcontext('2d');
- m# X% z, u$ w, ?};* [$ z3 K, I& e% M' S
canvas.element.prototype._initcustomevents = function() {
) Q! i+ q. I; b2 p5 ithis.onrotatestart = new canvas.customevent('onrotatestart');
* P! P' }+ I( ^- pthis.onrotatemove = new canvas.customevent('onrotatemove');
1 t# [9 V* p5 r7 _% q  Sthis.onrotatecomplete = new canvas.customevent('onrotatecomplete');* g5 D. ~4 N/ H
this.ondragstart = new canvas.customevent('ondragstart'); 7 }7 q4 Z" Z5 h8 Y0 Q9 `# ~; m
this.ondragmove = new canvas.customevent('ondragmove');
8 ^+ R% D2 v$ Q1 v# C: M: Nthis.ondragcomplete = new canvas.customevent('ondragcomplete');
- V" Q) O/ H7 N, w7 ^" V};
, \' D% ?) v' O  l% m: j. ~3 @% hcanvas.element.prototype._initconfig = function(oconfig) {
" E1 X+ u4 P" p, Fthis._oconfig = oconfig;2 a* ?6 W5 e: \8 F6 B7 M/ E2 G
this._oelement.width = this._oconfig.width;9 M# F& j! ^& H8 F- m$ }$ ?6 w- v: L+ w& C
this._oelement.height = this._oconfig.height;) G* f) h( y( ^! R2 w7 e# B5 A$ a) W
this._oelement.style.width = this._oconfig.width + 'px';
$ M% z5 T, V( P. e5 C6 g7 othis._oelement.style.height = this._oconfig.height + 'px';6 N  L$ @7 y5 Y) A# F
};2 @; D) i4 }, l9 M; g- z( Q
canvas.element.prototype._initevents = function() {
2 j0 {. \' [6 _: W) Uvar _this=this;/ P+ c! n0 y& {9 r
s(this._oelement).on('mousedown',function(e){: {+ l& b( P# _& j9 d+ W
_this.onmousedown(e);
8 Z1 z7 t& x" e});% u1 y: W" v& ]% W2 z
s(this._oelement).on( 'mouseup', function(e){
3 Z4 q+ E' u" d) T_this.onmouseup(e);+ x! _3 M# m- G( S% ~
});
2 ~# E/ l- z! g6 {! Z) X2 w& ~s(this._oelement).on('mousemove', function(e){* f8 e( q& P5 A  H8 [( m
_this.onmousemove(e);
4 ?7 f3 L* M0 Y  V: d* j4 I3 f});8 t; ~; }0 j) V1 d) y
};9 O* d' B- U4 K" n+ o) b
canvas.element.prototype._createcontainer = function() {# y  g! |: [  v0 I# {, W
var canvasel = document.createelement('canvas');
! Q( X- X% M$ j) m6 k0 Mcanvasel.id = this._oelement.id + '-canvas-container';
) D6 X5 n% d5 \' v4 tvar ocontainer = this._oelement.parentnode.insertbefore(canvasel, this._oelement);7 J1 ^) f# A, n
ocontainer.width = this._oconfig.width;" Z! X2 a% E: d+ U$ P& o
ocontainer.height = this._oconfig.height;
9 C+ ~& Y" F$ T8 a4 O9 b# rocontainer.style.width = this._oconfig.width + 'px';
4 ?: E) U9 X) B# Nocontainer.style.height = this._oconfig.height + 'px';
& M/ l' F8 a" z& Lthis._ocontextcontainer = ocontainer.getcontext('2d');
; x0 X, S3 ~' w* i. F  f% m/ l' `" ^};) K- V4 o5 z8 M+ i4 o, y9 v3 \
canvas.element.prototype._createcanvasbackground = function() {
: a9 M7 C  O2 C; F; l  a7 e) Hvar canvasel = document.createelement('canvas');
. o( w1 m* Q9 lcanvasel.id = this._oelement.id + '-canvas-background';
7 i0 A  N( _/ V: ~var obackground = this._oelement.parentnode.insertbefore(canvasel, this._oelement);. h9 P2 }  m# J: [: k0 u6 |
obackground.width = this._oconfig.width;: ?, u/ ]! ~2 H
obackground.height = this._oconfig.height;
, G3 t3 U! ?, S7 |) ]3 V1 ]obackground.style.width = this._oconfig.width + 'px';5 H/ q0 K: V$ x! f4 X3 s3 h
obackground.style.height = this._oconfig.height + 'px';
' B6 o. v  ^& r' y" dthis._ocontextbackground = obackground.getcontext('2d');
6 H) D; D* [0 \8 L- {};" w+ U6 d/ F8 X1 N( Y
canvas.element.prototype.setcanvasbackground = function(oimg) {  n( M; W0 N  A' G) {9 l
this._backgroundimg = oimg;
# ]4 M* b5 y: Pvar originalimgsize = oimg.getoriginalsize();. V9 a& U* p3 m1 |7 @8 j  A
this._ocontextbackground.drawimage(oimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);1 K8 E9 ~& Y2 ?  N% ]1 J% M
};; i6 n$ ]7 w$ j
canvas.element.prototype.onmouseup = function(e) {
( R! p' Z5 c2 }6 T4 gif (this._aimages == null) {
4 J* ], i" }0 F: Z6 t: N% y7 |return;
  \) Y  G# k& r7 z}9 u) X6 V# v. K; S0 L+ j
if (this._currenttransform) {" ?6 y4 Z& l; F: H7 y, [
this._currenttransform.target.setimagecoords();/ O  H3 X9 {) \9 D) a: m3 l
}5 Y' A: Y7 K4 W1 m$ ]
if (this._currenttransform != null && this._currenttransform.action == rotate) {
3 O5 Z5 D; h% _$ I9 {this.onrotatecomplete.fire(e);6 k: j8 h8 _7 q: m1 ~1 `
} else if (this._currenttransform != null && this._currenttransform.action == drag) {
1 [$ l* W, f2 H/ Y0 Vthis.ondragcomplete.fire(e);) O) Q- G, }9 h6 Q: f7 \# u
}- ^, m: ~  L+ [1 E
this._currenttransform = null;1 j2 E' o, ~3 B4 T2 Y# e
this._groupselector = null;
, }* v0 k. F" k* R. G7 K9 Pthis.rendertop();( X+ \1 r8 h. L  \/ C
};) R5 k3 S* k6 S7 v' ]
canvas.element.prototype.onmousedown = function(e) {; ?" ?8 s3 l3 ]6 L, w7 \
var mp = this.findmouseposition(e);  c* u" U4 H# K
if (this._currenttransform != null || this._aimages == null) {
3 x9 O7 s0 u' o& Y8 K8 \/ _, Ereturn;
0 X3 Y: y" D) K}
/ q  y# f7 G/ c. G2 Q$ |- Yvar oimg = this.findtargetimage(mp, false);+ C& u4 C6 e4 X' m+ C% w
if (!oimg) {; w1 _4 g8 h: D1 m6 ]; J& u
this._groupselector = { ex: mp.ex, ey: mp.ey,
  N! v, [6 V, D- ?6 x& I1 T% Xtop: 0, left: 0 };
2 d! p/ _' ~& l/ U' U4 G8 I# L}
8 `: Z5 T3 {9 \" F- S, O5 aelse {
* Y0 p0 R: ^' C0 a; p6 E) _0 Rvar action = (!this.findtargetcorner(mp, oimg)) ? 'drag' : 'rotate';
- O7 ?) C: X' B$ {: t& W& q- Aif (action == rotate) {4 L, ?1 i* \6 a( M7 E
this.onrotatemove.fire(e);
* s1 D; S$ O3 \' a} else if (action == drag) {
+ z- e+ f5 |3 R: p2 j* ?this.ondragmove.fire(e);
- y4 v" o" v" r7 h# B}; C) m9 ]' z1 w; Z9 R9 I; M
this._prevtransform=this._currenttransform = {
4 i. Q$ M! k% H, F* ^) w7 i5 w, Ytarget: oimg,, z4 @4 q+ B4 ?) R8 z
action: action,5 f8 F/ Q, |; ~# ^& Q
scalex: oimg.scalex,6 N' x) U: M: ^! I
offsetx: mp.ex - oimg.left,& g7 G: Q# l/ o$ u. Y! N
offsety: mp.ey - oimg.top,
5 a* Q4 F5 Z: ~' C; u% kex: mp.ex, ey: mp.ey,
8 }1 H" p! f9 @  [. eleft: oimg.left, top: oimg.top,
+ }8 P% ^' l1 X, c- [0 L( [theta: oimg.theta
7 V/ h* t" Z% R3 i; T' Z};
+ N/ d" T& D, o$('canvas_menu').style.transform='rotate('+oimg.theta*180/3.14+'deg)';9 G% o* ^1 b, a& X5 z
$('canvas_menu').style.left=oimg.left+px;
3 ?5 C5 Q$ _8 D  E2 c# `$('canvas_menu').style.top=oimg.top+px;
& \: z. k5 d6 s  C; \9 X$('canvas_menu').style.display=block;# E% x) c* d: X/ |
this.renderall(false,false);# u  F6 }6 Y; t0 }( P" v: D
}
% E7 d1 H, r: J) |5 R7 h4 D" Q};1 P( _: v. Q, J2 E$ y
canvas.element.prototype.onmousemove = function(e) {9 V. p) y2 y2 p- \
var mp = this.findmouseposition(e);1 L5 G4 D6 J; o0 R
if (this._aimages == null) {. g: {% `; r9 S! E5 z) c& O
return;" c! K& x6 X# N" T3 T. `: x
}
3 @0 H! R# t8 y$ K+ b! g, jif (this._groupselector != null) {. @5 v8 o* Y, L4 W
this._groupselector.left = mp.ex - this._groupselector.ex;
* s0 n" k/ \# D, e# A6 jthis._groupselector.top = mp.ey - this._groupselector.ey;7 z- y% r3 I+ O5 D, U! y6 @! A
this.rendertop();2 C4 x" K/ V6 k! s8 [# p$ E+ M
}
( e( F* q6 k9 Y/ N% P% lelse if (this._currenttransform == null) {
1 H  Z9 B, l& q0 Y  Ivar targetimg = this.findtargetimage(mp, true);( P3 V0 s% {! m6 @
this.setcursor(mp, targetimg);1 s" S5 Z* s9 n# \+ Y7 K- R
}% o  U  ]' F' K8 |
else {
/ P; ^& V. P$ B- F: ^: c+ oif (this._currenttransform.action == 'rotate') {/ H' Z9 |$ L+ n- [2 w# ^' p
this.rotateimage(mp);, R, I% c% F1 h& T  I9 @" e
this.scaleimage(mp);
7 I/ v- b! c/ P/ [" g& x- f% wthis.onrotatemove.fire(e);' r8 _( C! X2 c8 ]1 Y4 }9 J
} 4 }* N9 Q  \7 J+ w$ \0 }' s+ V$ H
else {
: `" s6 |8 W+ r. lthis.translateimage(mp);
0 ^) p; Q9 c* f, q( l# a9 c& G2 Ethis.ondragmove.fire(e);( c2 I3 ~! O% y# o
}9 B  I# c1 r6 p- I
this.rendertop();( a" y. \- ~! }* H9 K
}
* N8 b; g9 V$ D0 ~! ?% o, M6 W1 w};0 e8 ?& p8 c- f( i
canvas.element.prototype.translateimage = function(mp) {
$ l5 J6 T( m6 u" V, v" a4 S, S1 Qthis._currenttransform.target.left = mp.ex - this._currenttransform.offsetx;
) X: m! k  T. e" R& a5 Kthis._currenttransform.target.top = mp.ey - this._currenttransform.offsety;
! S9 G7 F( @) E8 I0 ~$('canvas_menu').style.left=this._currenttransform.target.left+px;/ A) j  W8 s# [1 s  {: o
$('canvas_menu').style.top=this._currenttransform.target.top +px;) S2 N* A, x9 ]0 q: T
};! E; S9 M$ O/ R6 T+ ]0 K% Z- |
canvas.element.prototype.scaleimage = function(mp) {0 l% u& ^7 N7 k- V
var lastlen =
6 y1 ~8 r9 W" C# @$ f: Nmath.sqrt(math.pow(this._currenttransform.ey - this._currenttransform.top, 2) +$ P* @8 L) r; U2 l5 [9 H' O
math.pow(this._currenttransform.ex - this._currenttransform.left, 2));3 ^8 q3 }6 o2 ]1 U5 r% Z; J
var curlen = / K' E$ B. \# k7 m& ?6 w- S
math.sqrt(math.pow(mp.ey - this._currenttransform.top, 2) +
8 u$ ?- }% w, u8 pmath.pow(mp.ex - this._currenttransform.left, 2));
: k4 {9 {9 _8 @: v/ X& Ovar curscalex= this._currenttransform.scalex * (curlen / lastlen);  s" d) V/ J  {- r$ I% P5 b& x( Z
var curscaley=this._currenttransform.target.scalex;
9 Y6 a) D) D: l' h* O2 Fif(curscalex>0.7&&curscaley>0.7){
2 v- l; S% X, r% |5 ]/ q0 K  E1 kthis._currenttransform.target.scalex =curscalex;
+ D6 }) C6 D" R3 @this._currenttransform.target.scaley = curscaley;9 l& H2 `5 A' E0 X$ n/ k  g: A  L
}  w% j& `5 c0 D* \- O
};9 K2 c2 i( c; `5 D; m3 n0 \
canvas.element.prototype.rotateimage = function(mp) {( X( A( y6 {) M4 X! j
var lastangle = math.atan2(, c8 h, e4 G: X8 t; }0 _' F' T
this._currenttransform.ey - this._currenttransform.top,
- f8 N* R+ t- F; u1 n, g5 x( Fthis._currenttransform.ex - this._currenttransform.left
, K6 F+ R; F! s! _/ ]);  h5 Z; f( f! n/ y; n1 a
var curangle = math.atan2(
& |- k; J& H; o3 G/ ^mp.ey - this._currenttransform.top,
" P1 I7 ^3 N+ P$ n- r$ k& y/ E$ P' |mp.ex - this._currenttransform.left
! d0 ^% {, F: s4 q7 A" f);" F& c. b0 Y* \+ R. P1 F0 o& o
this._currenttransform.target.theta = (curangle - lastangle) + this._currenttransform.theta;% x/ }6 @! {  Y( G0 O
this.curangle=this._currenttransform.target.theta*180/3.14;3 q9 r3 n- k0 b# `; s
$('canvas_menu').style.transform='rotate('+this.curangle+'deg)';2 k' o( U+ Z' d. s& L
};
$ r% a- L, Q9 u! @9 w% p& pcanvas.element.prototype.setcursor = function(mp, targetimg) {7 J/ q: H& M) o( C! [* F
if (!targetimg) {. a* ~, B" R( g
this._oelement.style.cursor = 'default';6 v8 z7 p% O, c) L
}; `' O6 D2 O! v4 P! D5 m: N( j: y
else {
+ A' \# b; n) _' Q( |var corner = this.findtargetcorner(mp, targetimg);2 n" u5 C6 D' E
if (!corner)
0 F3 {2 M: i- Y9 A{+ P: i$ A0 w0 h
this._oelement.style.cursor = 'default';+ G; t7 G* u( e1 R" O9 d. q
}! |9 w3 x  l# S$ l0 ~4 _
else
  i3 Z0 O: z6 w2 {% V{
2 @/ F4 K3 L) p/ U. U* W, H: w' a8 tif(corner == 'tr') {. F# k7 l, A) B3 E
this._oelement.style.cursor = 'ne-resize';$ ^; I7 j; Y! a  ^% ^( ?- G
}
- O( d4 u; |, Y) a- V+ l  Y, lelse if(corner == 'br') {
% }" v5 m6 P& P$ W8 ^, xthis._oelement.style.cursor = 'se-resize';+ P/ Q' P! N9 H6 j: S# [* n
}
) `& P  q% U+ S4 zelse if(corner == 'bl') {
) k. E& ~0 I& k4 I& ethis._oelement.style.cursor = 'sw-resize';
9 [) N4 I! c" K" \0 i7 }$ N" X}
- A3 {8 L9 G! Xelse if(corner == 'tl') {
3 C% C* R1 [) b  K/ Gthis._oelement.style.cursor = 'nw-resize';8 N( z' A: O9 K6 |
} - q& ?# g( [1 S; _/ A, ?5 V
else {9 K# ^) m- W3 K
this._oelement.style.cursor = 'default';
+ ^4 Z4 w: e5 d1 [}
( r8 A7 D# n" z: h0 F  b}
8 i2 L  P4 l6 R, e! q0 Y) S+ q}- y1 p- q( W+ i  N7 t9 J
};. d2 n% ^( I# ^8 _! ^1 k8 q
canvas.element.prototype.addimage = function(oimg) {, @* e; v' M, T% S2 D& g, a
if(s.isemptyobject(this._aimages)) {
9 ^' _4 Z' f( N0 d. m5 |( Fthis._aimages = [];
: a+ Y" r& d$ i) T* d* `! q5 C3 L}) J2 a3 z6 [- T- C0 Z
this._aimages.push(oimg);$ X7 U( e' ~' V
this.renderall(false,true);</p> <p> };
; Q6 c  f# G  S1 ?3 F. A* \canvas.element.prototype.renderall = function(allontop,allowcorners) {3 u& C; J2 G0 X, `( z, K' b
var containercanvas = (allontop) ? this._ocontexttop : this._ocontextcontainer;
, h+ v0 w$ b' Vthis._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
' m" L+ M7 {; c( _1 \, ycontainercanvas.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
: e4 ~7 Q- y) t; k( G$ D( fif (allontop) {
; c" x, Q* r& @0 K4 s. k! evar originalimgsize = this._backgroundimg.getoriginalsize();
2 }& Z( \: y# w3 |6 v' h0 d+ o( jthis._ocontexttop.drawimage(this._backgroundimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);
1 G" t# J  Y( t  a% N}
5 j8 d" r  h; G/ ?for (var i = 0, l = this._aimages.length-1; i < l; i += 1) {
) \! M' X, s  M" Y. h3 @3 ~this.drawimageelement(containercanvas, this._aimages,allowcorners);
+ O) ?$ _2 J/ k+ U7 h1 ?}
5 J0 }! B2 I* n  p/ ?this.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],allowcorners);
7 Y( N! A  d  j% }9 d};. a0 N+ d8 M, S3 T4 s3 W6 M
canvas.element.prototype.rendertop = function() {( ~# u2 L  ?4 L, A6 k/ B, f! B
this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
' h) w9 S/ R0 V& y/ jthis.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],true);1 D8 k# J4 t  i( U. n/ u( ?! N
if (this._groupselector != null) {- V9 C8 e! a- ]
this._ocontexttop.fillstyle = rgba(0, 0, 200, 0.5);" i$ m4 ?- X0 u. {. D5 ~
this._ocontexttop.fillrect(& D& H/ |8 }' x; |
this._groupselector.ex - ((this._groupselector.left > 0) ?
0 D" |0 a4 @/ _0 ~- f. L0 : - this._groupselector.left),
5 I% e7 F  v+ C7 ]# i' ~this._groupselector.ey - ((this._groupselector.top > 0) ?
# t# Z# O6 p/ b* [! p0 : - this._groupselector.top), ! m9 [+ K5 a  a! s; H
math.abs(this._groupselector.left),
) b# r9 }3 J- t$ ~: T* imath.abs(this._groupselector.top)0 l& a" ]- n( ^' Z- ]! K2 k2 ~
);) B/ E, e- I* K# f- T. \
this._ocontexttop.strokerect(
# A" c4 P( @' F. O$ Cthis._groupselector.ex - ((this._groupselector.left > 0) ?
" R' q8 |5 q2 B& n, i  Z0 : math.abs(this._groupselector.left)), / M: q. V4 Z% a8 W
this._groupselector.ey - ((this._groupselector.top > 0) ? 4 |& [+ U% L+ X6 K' L. r7 ^; H, a
0 : math.abs(this._groupselector.top)),
- k2 ^3 _! I1 \! n2 r( p) b( K8 Umath.abs(this._groupselector.left), $ V6 b, \; @# t; s6 M/ K. v1 G
math.abs(this._groupselector.top)
: M6 X' g5 B0 y3 _  q: G+ `);
" a! C1 N1 J7 I+ c0 t}
/ t" t8 v" A) V8 o};
# [' e2 }+ K. I) ?, hcanvas.element.prototype.drawimageelement = function(context, oimg,allowcorners) {
, ^& ~2 Q5 F9 p5 A8 c4 v. A. Q# uoimg.cornervisibility=allowcorners;
: E& E+ ?) G, f+ A8 Yvar offsety = oimg.height / 2;0 h7 B- Y4 m+ w' L- G
var offsetx = oimg.width / 2;+ D) c; o* j& X1 V& m3 B
context.save();+ O% F* n4 o. ~  H  O
context.translate(oimg.left, oimg.top);
- O+ X& u: {6 M. e( U% X3 V& ^, a. R4 J# qcontext.rotate(oimg.theta);
# V6 ]) L  i% @context.scale(oimg.scalex, oimg.scaley);
& f/ r' g  C. B# X  ethis.drawborder(context, oimg, offsetx, offsety);, s& [9 J. r# u( Y
var originalimgsize = oimg.getoriginalsize();2 O* p9 N: L, c& `' c3 }# \% y. [% Z& I) S
var polaroidheight = ((oimg.height - originalimgsize.height) - (oimg.width - originalimgsize.width))/2;9 ~: U, Q- V' F
context.drawimage(
+ |5 P! U. B% p- ioimg._oelement, # V# B' @0 I1 u! h
- originalimgsize.width/2,   C2 a: ^9 p3 U# z
((- originalimgsize.height)/2 - polaroidheight), 5 Z: r$ ]0 V* a, ?; K2 @9 P& T- A
originalimgsize.width, 5 P  v8 ]- a) A
originalimgsize.height7 Y, {; k4 e; v
);% r0 X9 e3 |- b( C
if (oimg.cornervisibility) {
6 c+ a5 P! u' Z/ @. ?this.drawcorners(context, oimg, offsetx, offsety);7 ~" g- |5 B: d5 Y3 u& F
}8 x0 D, I3 d9 C8 r+ }# j& ^
context.restore();
. a' D# J8 Z2 X: r5 P9 i/ v& E) r};: X+ Z# l9 i! ^% ?+ k" b3 x
canvas.element.prototype._getimagelines = function(ocoords) {) R: ]3 h/ M# i& E% C$ X
return {6 O% [# @5 x6 H7 f( Q8 d: O
topline: {
" ?3 a9 g/ l! {1 G# C: e9 r- [o: ocoords.tl,
% ~5 k% j8 n7 W# U3 _' T3 Bd: ocoords.tr
% `$ K4 a( V/ X. ?1 m0 N},
, k8 f/ p  t: G4 q7 i# }0 brightline: { / Q" g! p0 O9 ?% W+ I7 l9 n9 x# @
o: ocoords.tr,+ d; D+ k$ P# {% [/ T5 x; _9 u
d: ocoords.br + Y1 F7 x  j- ~6 q
},
& D. s$ O2 N* v/ `! lbottomline: { 4 Z  |8 j: t- S2 I5 h  e" \5 Y& A& O
o: ocoords.br,% F$ B/ b' h4 j" c7 X# W' r
d: ocoords.bl
( R0 v5 e2 I1 L, Q( \},) Z! L1 r6 t) M2 z- S. B$ G+ S! A
leftline: {
0 j! M  h" Q0 ~/ e/ l$ oo: ocoords.bl,$ X0 I, G$ m, p$ k
d: ocoords.tl
3 T" ~0 Z$ N" x}9 `4 S/ F% s5 Q
};+ a( y8 I: Q. v1 B* F: V4 w
};6 Y) B( l, P2 @6 x0 G. L
canvas.element.prototype.findtargetimage = function(mp, hovering) {
8 E+ T6 Y3 ]& C- d0 D6 `9 J' [) jfor (var i = this._aimages.length-1; i >= 0; i -= 1) {
: z4 T$ n$ p' ?  G( D. d) I0 Nvar ilines = this._getimagelines(this._aimages.ocoords);6 A$ q, Q1 G7 F5 |
var xpoints = this._findcrosspoints(mp, ilines);
7 Q! o- y" j$ ~0 ^* P  M3 i8 J2 rif (xpoints % 2 == 1 && xpoints != 0) {
, p7 b4 \& Q, V" |# i' o$ Kvar target = this._aimages;
8 {2 ^# o' H; [2 f3 N. dif (!hovering) {
- e9 ^- j$ R! ]  m+ ?this._aimages.splice(i, 1);  J# w/ z) Y% Z- \, s" E
this._aimages.push(target);
; P8 Z0 z6 H# F9 Q. q6 z}- M. J) Z8 M5 d' j1 g
return target;
* }, X+ O% U. N& B- ?  @}5 l1 H4 z$ m6 `. w2 S7 V. M
}. a# F/ ^) G0 s9 Q' n% ?" b  w
return false;
( f8 [, q; o. f* Q4 i3 O}; 8 @. t$ ~) o) R# M  B, s
canvas.element.prototype._findcrosspoints = function(mp, ocoords) {
6 J" f; j4 n$ O: hvar b1, b2, a1, a2, xi, yi;
  ~/ ^$ m; r) H+ Q# ~# |- Pvar xcount = 0;3 w2 Q* m  y3 S& P" m& d
var iline = null;$ z" J2 P1 ]$ y, N2 T7 o3 Z- c
for (linekey in ocoords) {! {* t  L# ~0 y' u, x& `5 c3 U) o! `, ?
iline = ocoords[linekey];0 t" y: U" t% T! a4 W3 F
if ((iline.o.y < mp.ey) && (iline.d.y < mp.ey)) {
$ c- W" n3 k, v- z) Y+ b3 z+ }continue;0 S: w- D1 w* \! h
}
. Z  V; H: o. @" aif ((iline.o.y >= mp.ey) && (iline.d.y >= mp.ey)) {5 l3 U9 @/ @$ T3 \- D( S5 o
continue;
8 H4 ~% c) x5 P7 m( ~  |6 g}: s" a6 [5 P5 m! \
if ((iline.o.x == iline.d.x) && (iline.o.x >= mp.ex)) {
& B8 n: k2 l) N& w7 M% m9 n$ exi = iline.o.x;$ {6 f9 k. x. L% _
yi = mp.ey;6 s! \6 l# S) Y: o2 a" Y
}# V# `" Z& x  G9 i* X" w
else {8 q$ N/ t& L2 j$ M+ n# m
b1 = 0;
. E3 h4 i: I# ]! k/ H6 f9 wb2 = (iline.d.y-iline.o.y)/(iline.d.x-iline.o.x); , B- u$ V# W( i, a$ w
a1 = mp.ey-b1*mp.ex;3 @& f: _7 ]8 [# P9 q" l
a2 = iline.o.y-b2*iline.o.x;
- |8 m1 @7 S+ x6 Z) b" B0 C7 oxi = - (a1-a2)/(b1-b2); ! ^" |. J7 U1 }1 {* H4 g" P: k
yi = a1+b1*xi;
2 d+ e$ |, w" ]}
, N) S5 U( j# Y/ {  U  Z+ x6 a2 Sif (xi >= mp.ex) {
  [5 S# a" {: A( x  f) t# Qxcount += 1;
5 z# e5 }7 S; a7 a  {& M}. G# e! I& @3 Z$ Z
if (xcount == 2) {* W' D$ d3 l; j% ], w6 W
break;
0 M( k& x# z1 E$ L5 a. R9 z* |1 o% {}
; m7 r1 |( J6 O}
0 P- W: b" X" p& _! Dreturn xcount;
) C3 A6 M$ J9 }- Z/ a2 r* s% A};8 z3 r5 c# w5 O% X: i9 k: z
canvas.element.prototype.findtargetcorner = function(mp, oimg) {
8 d( [. g# q. H9 K3 r( rvar xpoints = null;
/ x7 V/ M7 C2 d0 G, d; ~) j4 Ivar corners = ['tl','tr','br','bl'];9 g' q) k* r' _7 b
for (var i in oimg.ocoords) {
- N- b, Q: p) {" Cxpoints = this._findcrosspoints(mp, this._getimagelines(oimg.ocoords.corner));3 Y3 ]/ o) f( q' h# S# A: h
if (xpoints % 2 == 1 && xpoints != 0) {7 ?1 ]5 L& [" ?. g. z7 `
return i;, I" s, ?0 H' G
}
; @- N6 g# U4 I5 e5 D}/ w/ J* f% _* T1 g* s
return false;
3 p7 Z% j6 F" x};& B, ^- r" c$ b( J9 L4 j
canvas.element.prototype.findmouseposition = function(e) {, |, J% p, H9 D
var parentnode = (e.srcelement) ? e.srcelement.parentnode : e.target.parentnode;
: H+ [7 M: u8 f2 C- p+ K7 ]" Fvar issafari2 = !s.support.ie&&!s.support.firefox;0 Y7 u: b! X& a$ s3 Z6 U% ?
var scrollleft = document.documentelement.scrollleft || document.body.scrollleft;
0 P7 h: j+ _( n& \+ y  u) ~var scrolltop = document.documentelement.scrolltop || document.body.scrolltop;
0 L+ E8 K2 n) m( g% a8 l" dvar safarioffsetleft = (issafari2) ? e.target.ownerdocument.body.offsetleft + scrollleft : 0;
& i+ s$ X3 }% o. z1 k/ H1 Zvar safarioffsettop = (issafari2) ? e.target.ownerdocument.body.offsettop + scrolltop : 0;$ A$ z6 E$ [" q7 r8 o5 _
return {; S- a" l6 H/ `  @/ ]. l, x
ex: e.clientx + scrollleft - parentnode.offsetleft - safarioffsetleft,' d7 r& m7 E8 }* P
ey: e.clienty + scrolltop - parentnode.offsettop - safarioffsettop,
2 {" O5 C' x  o1 J8 w0 Escreenx: e.screenx,  F1 c4 U) {! g
screeny: e.screeny- `% F' t6 P: |
};
+ u" E- I9 N2 P$ W};
" Q7 x+ l0 {# p+ y& Lcanvas.element.prototype.drawborder = function(context, oimg, offsetx, offsety) {
  j: n( h6 X: B0 i$ U( Vvar outlinewidth = 2;
1 H9 |5 _* R3 V/ F5 scontext.fillstyle = 'rgba(0, 0, 0, .3)';
0 w- W- `. K: r* Q; x$ n8 z! Q% ]context.fillrect(-2 - offsetx, -2 - offsety, oimg.width + (2 * outlinewidth), oimg.height + (2 * outlinewidth));9 f5 C5 ^6 w* u" h
context.fillstyle = '#fff';& ~7 Q/ v; [2 e0 M0 z1 Y& \. U2 x
context.fillrect(-offsetx, -offsety, oimg.width, oimg.height);
5 c, U' V2 l! a+ X) h};
2 B  a$ K' S' v9 rcanvas.element.prototype.drawcorners = function(context, oimg, offsetx, offsety) {
7 z  A5 b6 J0 ?2 h1 Xcontext.fillstyle = rgba(0, 200, 50, 0.5);
& [+ k- y: o% s- icontext.fillrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
1 o+ E2 x& g. ]0 Q- icontext.fillrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);: i. R% c* u2 ]3 O, A3 b; V8 a
context.fillrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
" u: v5 Z2 ^9 ]context.fillrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
; x# e3 [- q- j6 ~$ J$ h};8 p. P2 X9 C& X  W, @
canvas.element.prototype.clearcorners = function(context, oimg, offsetx, offsety) {
, x- E& f  T4 }' w6 K& B5 _& wcontext.clearrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
. v& J) r& {: l# I/ G6 }context.clearrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);
0 ~1 s7 ~  V4 ~, W4 ]* `3 }context.clearrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);' S! _% h. W) F& O
context.clearrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
9 F, z( |/ W) W* n8 tcontext.restore();
" \; ^# S2 w1 Z" i' [3 ?};
9 m0 w. N4 l0 v* Q% N% s. ]; Rcanvas.element.prototype.canvasto = function(format) {: _$ v$ l6 c$ P
this.renderall(true,false);1 d8 u; E9 s5 z  h
var containercanvas =this._ocontexttop;! b$ m2 B; _" E4 K1 N
for (var i = 0, l = this._aimages.length; i < l; i += 1) {
% g( ]' W: k& R% c; Y, E6 q, wvar offsety = this._aimages.height / 2;# N# g" o' P% w- S0 w; T; P1 P
var offsetx = this._aimages.width / 2;
. H7 S! D. Y; x( qthis.clearcorners(containercanvas, this._aimages, offsetx, offsety);2 h: F8 B* t4 s
}
, A7 O5 E: t& A. `; M. ]4 U; ^if (format == 'jpeg' || format == 'png') {3 ~/ M: [) h  q& e2 Y. m$ i
return this._oelement.todataurl('image/'+format);
8 R( W, Z8 O4 a}( q# {) m7 v' t& v
};
. |- f; h2 j' _( \; h* rcanvas.customevent = function(type) {
! U) G( I6 A$ D. Othis.type = type;
  }; d2 `2 X4 u) u- ~this.scope = null;: h* g( E! E; I6 u- y* \
this.handler = null;$ ?$ Q6 K7 K+ O9 f& V/ c/ x+ c8 E
var self = this;' Q; o% g/ L$ @: V' H% s& \
this.fire = function(e) {
" F5 y. M& ^, c; z) k! tif(this.handler != null) {: y' l& k) k1 }( X: c: l0 T
self.handler.call(self.scope, e);
5 I4 W1 ~) s5 Y; N" \: G) N}: P1 E# p. p3 }* P$ h
};
2 @0 g( p1 K/ B9 M* g};
: T3 R; U% p5 Y}());
2 u( Q( {+ I; ~  wreturn canvas;0 R, E# y* O' M4 J, D2 V, T. k
});! O0 |' ?. |( ?* e& g
canvasimg.js代码如下:
, d- q7 h! S6 g% u3 n, \' n复制代码代码如下:define('canvasimg', [ '../multi_upload/core' ], function(s) {" L% A" ?6 E5 l  B0 B
var canvas = window.canvas || {};' k# G$ f/ h, l, G1 J2 D: ]8 W
(function () {
  ~& h# H- V5 j" ?canvas.img = function(el, oconfig) {) f5 `: H, a0 l% |
this._initelement(el);1 h6 b: B' g! I5 W* J
this._initconfig(oconfig);
# H% T8 }3 t9 V6 K% I0 c  h9 ~this.setimagecoords();9 v# k9 a. U7 G( A6 d3 ?
};# l  n# f# p5 J" Q
canvas.img.css_canvas = canvas-img;% v( d$ L' O1 `1 a- @
var default_config = { * @7 b# o& X  {
top: {
# M/ p$ f: A! _4 a- F! kkey: top,
" x1 r: n, p7 k4 H. {" j3 Jvalue: 10
$ g$ O$ F+ @/ m. o, `4 T},
$ g% e9 q, U6 E* V  zleft: { 6 D9 N) S# z) S$ o5 C
key: left, 7 V' A1 m+ I; r- ]# x1 Y& V! L' s
value: 10) K% ?& s! X  y" V( m
},
% h# P" i* K4 n) C3 Langle: {
, J$ r. e3 W9 y+ O  W) rkey: angle,
/ y  ?( K0 u+ W3 P8 J' qvalue: 0 $ ]# g2 y; F) Z3 N- ^
},
8 I: C, R3 x3 _& j1 j; Y. ~" ztheta: { 7 ?5 r. ~3 ?3 f6 M- H2 E
key: theta, * E" a9 f  ~7 v1 C2 B7 E8 M
value: 0
& F4 g: @! @* T$ L* N. [},9 N/ r7 Q7 [4 F% ~
scale-x: { 4 I* a3 R( a, j) S
key: scalex,
+ g8 K4 @5 G  avalue: 1$ [: F2 ~. F' g4 |
},
7 _& t# `9 I; W+ N' H* ?; A9 c7 Ascale-y: {
& A! x: [7 t2 ]7 T" l0 I2 `key: scaley,
5 K* G2 a; ^+ @# evalue: 1% ^: x1 C; ^, ?
},
5 X0 K) l$ j& L: m  r! \cornersize: {
$ w0 {5 l* ]7 w0 ]) F, g, [' W$ nkey: cornersize, 6 s( L3 F3 R$ {; z% v2 `
value:10
. j$ Y& K% h( w& A. t& m},
" ]" n) g3 n  yborderwidth: { + U' d; N% W/ O1 ?& l9 |- O
key: borderwidth,
5 I9 A) K5 w/ J5 U( i$ jvalue: 10
( V/ H6 c* M  p},
  X: ^( R2 \: k3 _polaroidheight: {! k1 M" r) f7 p5 w9 y' @" H
key: polaroidheight,  A: h- g: `: Y9 n+ V; c% r: N
value: 40
( K) y  k2 h1 @( x% B# b$ R2 W},9 M$ P6 x; e$ T0 o
randomposition: {
8 d9 y. M& x) F- E% }7 k9 fkey: randomposition,
" K6 r# \; A3 r* ivalue: true% Q2 t5 {+ ~0 q) M9 H% m6 ^
}
* \' f4 v0 a4 ^1 m5 I4 Z};7 Y% c# x  H& R- m& Q9 I' b
canvas.img.prototype._oelement = null;
/ l2 |" x/ s2 p' E) O: f/ K( d; ucanvas.img.prototype.top = null;. V& [) s7 a. ^+ c! N2 ~8 w& I
canvas.img.prototype.left = null;$ E, h) O. Z4 C2 X, W
canvas.img.prototype.maxwidth = null;+ ^- s3 ~, k) D4 x4 m4 \8 ^
canvas.img.prototype.maxheight = null;
6 `, ?0 |" R# B: H5 _canvas.img.prototype.ocoords = null;
- _( N; b+ q. x. n4 r3 W/ ]8 @/ c: Fcanvas.img.prototype.angle = null;
3 e& c- G, f2 q9 b6 V9 U9 gcanvas.img.prototype.theta = null;; r' @) }/ \7 U8 d" k1 e
canvas.img.prototype.scalex = null;
) @/ L6 n' k% N9 p# w( Lcanvas.img.prototype.scaley = null;, B* J: x/ L- B  N+ p
canvas.img.prototype.cornersize = null;! f5 o9 t. }' ]* N1 A2 b
canvas.img.prototype.polaroidheight = null;
; Q* C. T) x; ~' qcanvas.img.prototype.randomposition = null;
7 B  s  M. }; K& gcanvas.img.prototype.selected = false;
" }" ]2 }6 R# U4 B, W) pcanvas.img.prototype.bordervisibility = false;
% G. @' h5 A. v; B; Icanvas.img.prototype.cornervisibility = false;
( F; a' |% |1 N- `" _. k6 Z& Xcanvas.img.prototype._initelement = function(el) {
0 Q+ I& _( P$ v1 w3 N. i; othis._oelement = el;
) ~/ v/ ^5 I0 z0 G% F8 g};$ g. @6 q+ [; z% N* U  }
canvas.img.prototype._initconfig = function(oconfig) {) |8 F& h! I# Q, n; v
var skey;
2 c/ R4 F9 j1 E* rfor (skey in default_config) {7 R3 W. |# K2 j' h, P
var defaultkey = default_config[skey].key;
/ ~+ a' N* i3 j- P$ T0 Y' Rif (!oconfig.hasownproperty(defaultkey)) { // = !(defaultkey in oconfig)
3 I: U1 O# j! Y- ^2 n% jthis[defaultkey] = default_config[skey].value;9 H% Z" H3 Q+ H
}/ u, m4 T2 P# T/ ~' c
else {
1 z) Q* n- L6 n2 g* |; C- }this[defaultkey] = oconfig[defaultkey];
/ E% O1 `% @. B( T$ ~% B}
; d' h! j0 p4 W0 \3 Y( S: Y}  Y1 w' G3 K6 z$ u/ C4 O. g0 ^
if (this.bordervisibility) {$ s" j( Q/ H1 `' N0 ?3 }
this.currentborder = this.borderwidth;
! |% n" F1 W' u+ S8 ]}3 d3 \4 G6 T& c% n- Q7 \6 @
else {" d; i, E+ K! z$ v" n
this.currentborder = 0;
' G" w  u7 @4 V/ w$ I6 [" Y  |! w}& g( p5 S8 O$ D8 A# Y7 X+ \4 l
var normalizedsize = this.getnormalizedsize(this._oelement, parseint(oconfig.maxwidth), parseint(oconfig.maxheight));
  z+ U# E. n! c7 \. v/ lthis._oelement.width = normalizedsize.width;
+ U8 u% v. v& S/ m, e) Vthis._oelement.height = normalizedsize.height;
0 e1 {! H8 ^1 H- F; E  Z) pthis.width = normalizedsize.width + (2 * this.currentborder);2 b: ~9 }$ R# J& }+ x, e
this.height = normalizedsize.height + (2 * this.currentborder);
/ q3 L  X2 h. O' B; Hif (this.randomposition) {
7 l( y5 N$ R1 P9 @2 J+ ^, Cthis._setrandomproperties(oconfig);0 b* g  t9 W: s. Y1 y! v
}
# m' F  C* m0 {; d/ [this.theta = this.angle * (math.pi/180);
4 a, M2 [/ B; ^$ M9 ^- X};
& b* h4 z& w) Ecanvas.img.prototype.getnormalizedsize = function(oimg, maxwidth, maxheight) {5 K/ y( J/ a( m- h, S9 h: A* u3 k
if (maxheight && maxwidth && (oimg.width > oimg.height && (oimg.width / oimg.height) < (maxwidth / maxheight))) {/ b# x" D7 V0 u% I
normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);
) m0 p& \; F6 P$ V* inormalizedheight = maxheight;" U6 t$ e7 O8 R: E% r0 c8 O) d
}2 v) h/ E: L4 H7 O+ G. w8 ]
else if (maxheight && ((oimg.height == oimg.width) || (oimg.height > oimg.width) || (oimg.height > maxheight))) {2 R2 I3 H, N& O- d0 g$ z. j
normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);. r1 M3 m; N1 V& I
normalizedheight = maxheight;; E/ z* ]/ s3 y4 w  W1 f( |4 b% M9 H
}
0 @0 L/ n1 m5 F+ F- velse if (maxwidth && (maxwidth < oimg.width)){
9 Q) G! @, V9 A; m3 E5 `normalizedheight = math.floor((oimg.height * maxwidth) / oimg.width);( e6 z0 c9 V9 q. A* }. g. I
normalizedwidth = maxwidth;2 _+ {2 l4 I! n6 A+ H$ G% J8 x
}6 G$ W9 }. O# N/ \
else {  w" x. e6 u6 |( |3 ^* n
normalizedwidth = oimg.width;
. J* d: |) a8 S" ~& @. Gnormalizedheight = oimg.height; 6 k2 E' g* R* r& v9 x9 W
}6 l  c; s& l/ _; A3 x$ c
return { width: normalizedwidth, height: normalizedheight }
/ F8 `1 L7 }4 g! _7 t6 e4 ?" C6 c9 r},  h9 h( C& J! g: {0 M6 G. m
canvas.img.prototype.getoriginalsize = function() {
7 t9 r4 k3 \% r( q  greturn { width: this._oelement.width, height: this._oelement.height }
+ }, b/ m+ U( Q: [' e, g# E* |};
& a3 J0 O# u2 g4 ?0 ccanvas.img.prototype._setrandomproperties = function(oconfig) {* t  S1 Q" d7 F; `" b- u
if (oconfig.angle == null) {2 k4 e" g0 ^* d7 T; V
this.angle = (math.random() * 90);/ |. z: S: C7 L1 h6 h
}& t( N% \& o8 R9 }+ e  ]- O
if (oconfig.top == null) {* ~1 V9 G' x9 X- E% ~9 H+ P* Q- z
this.top = this.height / 2 + math.random() * 450;
3 w3 o% k( @, x}
- z& @( l9 J: j- |; eif (oconfig.left == null) {
9 a" V* Y' u9 u& athis.left = this.width / 2 + math.random() * 600;& c, O& i( ^0 c, V/ f  |+ d0 E
}
' l& h9 _4 l: T' e* @1 s4 E3 d1 M};
, j  y$ {' p. o! a2 pcanvas.img.prototype.setcornersvisibility = function(visible) {
: X0 W8 T" \% W0 q7 [& Cthis.cornervisibility = visible;; L5 c( a3 f5 G1 h$ r7 C0 W3 j2 v
};
/ \- N  S* c6 Z5 {% G/ n" _canvas.img.prototype.setimagecoords = function() {# K( U, c3 j0 w' {& ~- C2 F
this.left = parseint(this.left);6 ^" e. t6 H! [1 a; ?' E' ?
this.top = parseint(this.top);, X0 Y- k6 G  y. w! f6 o
this.currentwidth = parseint(this.width) * this.scalex;& z8 `/ Y/ f/ e" N) U( A' w
this.currentheight = parseint(this.height) * this.scalex;& q/ b& T2 {4 e/ K9 \" ~& a
this._hypotenuse = math.sqrt(math.pow(this.currentwidth / 2, 2) + math.pow(this.currentheight / 2, 2));+ J  W* ?0 U; N  v: I0 O* C) \
this._angle = math.atan(this.currentheight / this.currentwidth);* [7 _# I: v2 a; X8 T6 ^- n# a
var offsetx = math.cos(this._angle + this.theta) * this._hypotenuse;: u; u4 G0 S+ [, w, q
var offsety = math.sin(this._angle + this.theta) * this._hypotenuse;. l; }$ H4 L! e+ Z- g. d
var theta = this.theta;
5 g  E  J3 v1 r) Q, Cvar sinth = math.sin(theta);, y- {4 b# D) y+ ?5 z+ }% c
var costh = math.cos(theta);% r6 j- M' M% p: D' g6 r! o. t
var tl = {* s' C& e# l& ?7 C6 Z
x: this.left - offsetx,
4 W" R/ w( c* F2 u6 q  `, c1 g) Dy: this.top - offsety
, p* I" ?) \9 p9 E  s9 w};1 M' C7 e- K6 M" Y( f$ g
var tr = {
, o# S: P: o3 b( jx: tl.x + (this.currentwidth * costh),
% P1 y; x9 K! R! {y: tl.y + (this.currentwidth * sinth)
1 H2 y* C- ?0 I) K};3 x6 [7 L# {2 h* g
var br = {  J" i; b: I8 o
x: tr.x - (this.currentheight * sinth),
  C; f1 U1 l& z! l3 N" \' sy: tr.y + (this.currentheight * costh)
' ]1 c% h* D. I& s};
& r- a9 ^* [9 \0 |* H$ X, Nvar bl = {
4 \5 m# g: N: o5 T5 \7 ^" q/ _7 b9 `x: tl.x - (this.currentheight * sinth),
9 T1 D% R3 K0 H. d: Ey: tl.y + (this.currentheight * costh)
% Z" |- u6 _3 \( f# q& P  J+ c};) r$ T6 P% `5 ~$ @
this.ocoords = { tl: tl, tr: tr, br: br, bl: bl };6 ]9 r  Z4 V- h
this.setcornercoords(); 8 T7 S" S  h( V: W. L- F
};
* M# Z6 P4 l1 Q4 k0 [5 e2 |4 mcanvas.img.prototype.setcornercoords = function() {
* j3 f& x" g, t0 A8 T9 y+ fvar coords = this.ocoords;
0 v3 f0 P3 g$ T! v$ q0 R8 qvar theta = this.theta;7 \) |, R& z/ w: v
var cosoffset = this.cornersize * this.scalex * math.cos(theta);5 P$ ]# c# y) [+ l# i
var sinoffset = this.cornersize * this.scalex * math.sin(theta);
" ?, E& j* g" s3 z& p; b  g0 Gcoords.tl.corner = {. t- X3 I" Z4 W9 h* d
tl: {
% V9 G8 \* ~2 m; Y% C- r( Sx: coords.tl.x,- p5 C" @5 F9 ~  Z5 h1 ^% @5 V
y: coords.tl.y
" L0 Y7 b9 o1 m},
8 o4 d2 E# g5 q, _tr: {: k" n) J* a7 G5 |, [. t; Z# m! X
x: coords.tl.x + cosoffset,
. R: G" j. m" j" w- Ly: coords.tl.y + sinoffset% K( f( b& s4 n( v0 x
},
) h. |/ N9 [3 }8 N" w4 y1 ]bl: {
+ f8 x/ J+ P1 R' q$ o/ Z7 l4 f! P# ]x: coords.tl.x - sinoffset,
3 Q/ _6 x5 [! i/ my: coords.tl.y + cosoffset
9 H4 E6 X5 U) ?" B6 T2 [! @! c: w}
! x6 r* {1 D1 ]};
1 Z( O0 e- z) p! Hcoords.tl.corner.br = {
$ K8 @, n! a" R7 ex: coords.tl.corner.tr.x - sinoffset,+ F4 h; ?. N( U9 B  p' G! [$ ~
y: coords.tl.corner.tr.y + cosoffset
, q& M, h$ U) d: x) O};
- Y1 h) O7 {; q3 m' R3 Wcoords.tr.corner = {3 v  a/ n: S, p3 t2 {  G# K
tl: {
6 _" z- a$ i. M: ?. h3 s7 \2 fx: coords.tr.x - cosoffset,3 k1 k9 H, {" Q
y: coords.tr.y - sinoffset
9 l+ G' e$ e7 j& m( D* L% H},! f- P% h, e; c0 ]9 I
tr: {3 e! b& o9 n- X+ Z: ]
x: coords.tr.x,) H& c  M- `/ Q9 V3 R4 \1 P' {! M
y: coords.tr.y
5 O5 Y" ^6 K1 \& S% C- N2 q},
* e4 N' _+ K" ^& T4 J7 Rbr: {
# `5 e0 X0 M. ]' G$ H6 [; r2 yx: coords.tr.x - sinoffset,
. v+ ^- y9 _! b# Y1 k8 p! }3 N# cy: coords.tr.y + cosoffset
! T% Q- n/ K% ?$ B4 w$ D) Y  r2 L5 O}
+ R8 t0 f5 D4 R3 O, ?& X! v! j2 s};0 M. x! s! p) }* \( N2 |" [$ V
coords.tr.corner.bl = {
9 X+ O$ b( g" w# A! f7 [  Cx: coords.tr.corner.tl.x - sinoffset,
* ]3 E7 G+ E/ V/ E$ G- ^8 P0 Fy: coords.tr.corner.tl.y + cosoffset: F( I7 f( O" G( J/ |
};! b7 P, z$ Z& [5 S
coords.bl.corner = {
* a  j9 j! Q! d! L  Otl: {" O4 q2 D% s; y- x9 h
x: coords.bl.x + sinoffset,+ ?5 k1 O, K7 ^& S4 v& Q9 Q
y: coords.bl.y - cosoffset0 z' J9 j3 _9 S8 X) r$ W) @
},  Z! ~+ K9 G1 X( s/ k. f
bl: {
7 D/ s7 ]) k# C' ], I4 X/ Yx: coords.bl.x,/ I- D+ `) F3 Y, N' R  J
y: coords.bl.y0 l4 N5 K5 s* {- l; E
},+ B1 G7 a3 m+ }( p7 J& y; z4 v
br: {
; M+ J9 S1 H4 }2 _, m0 F# e+ R) G- d& Fx: coords.bl.x + cosoffset,
* y; f- j9 m0 y& O" E! Hy: coords.bl.y + sinoffset5 H. |5 i1 r2 s) i6 {9 K9 O) R. ~
}7 R" h# b* W& ~# Q  h
};
1 h+ r- o. |0 zcoords.bl.corner.tr = {/ w8 t$ D9 U9 x& X3 N& e5 \
x: coords.bl.corner.br.x + sinoffset,
2 s# B3 P/ f5 P; qy: coords.bl.corner.br.y - cosoffset- w/ g% A+ A; G3 T
};
: T; n; M- T4 G0 G6 q9 X& wcoords.br.corner = {& F* K6 ~( `* x& E( }
tr: {* t' k$ @- O/ L) Z+ z
x: coords.br.x + sinoffset,+ S, `# I! M4 i9 G" A$ R
y: coords.br.y - cosoffset$ b& l0 f* s$ X8 W2 t7 x- W
},$ l( @2 S# S0 W. v9 {& w) D& s
bl: {
  f3 ?- P1 ^6 z3 f, `3 `4 |: ax: coords.br.x - cosoffset,
& h& B" Z/ O  N, zy: coords.br.y - sinoffset
' r, I2 \) K6 C* z2 e1 C; W},
* ^" a# z3 x, o- `br: {/ W2 d! K' k9 Q( g5 A, ^
x: coords.br.x,
# K( Z8 P  b% W8 J5 ~0 T  Q* ]y: coords.br.y
1 J1 {' |. k- Q( @8 v8 q}) S  M% ?) @% z
};
: ]8 s6 D$ D1 f7 ]coords.br.corner.tl = {0 M3 v9 j- ?# q/ M0 c- B
x: coords.br.corner.bl.x + sinoffset,+ w+ e. \. z* _& c- U* F/ W
y: coords.br.corner.bl.y - cosoffset
, K. Z- p' Y( I8 m- h+ y};' p1 z. \8 [5 M. ^7 L2 n/ `
};
3 T, A5 h2 v1 H7 u) ^}());
$ b+ C$ b* [- Freturn canvas;$ n* S& U, s) g2 c, F. e
});* p: m# K5 }5 R9 N: w$ I8 d
puzzle.html代码如下:
0 C' B: O  a8 c/ @( G$ h复制代码代码如下:<!doctype html>
; ]: m5 e5 W8 Y7 D+ ]6 M<html>% m3 K0 G/ |! g0 s  l
<head>
. e# `% I, S9 k7 {/ o0 F<meta http-equiv=content-type content=text/html; charset=utf-8>
$ N2 L: O0 V! ~' s' [% Y<title>insert title here</title>
0 z. g( {2 s+ ]7 ]1 R<link type=text/css href=html5_puzzle.css rel=stylesheet />. o4 u( K( I# G* _1 s) ]
<script type=text/javascript src=../multi_upload/seed.js></script>
4 V% T6 Y; l+ ^9 f" I0 x<script type=text/javascript src='html5_puzzle.js'></script>3 P9 O- i, s/ d# m
</head>
0 A: T  v( a6 f6 k, |5 T+ u<body>: i  N9 v* _) n& C
<div id='html5_puzzle'>
# O8 F' N  D! U<div id='puzzle_left'>
/ K2 J' S( f/ E8 v% Z9 b<div class='puzzle_column'>9 \, I( @  j- P
<ul>8 d9 l! ?) I- P; y
<li><img src='small_img/1.jpg' data-index='1' /></li>
* ~' H; Y- s+ `3 T<li><img src='small_img/2.jpg' data-index='2' /></li>, u* c8 F: ~0 {. \
<li><img src='small_img/3.jpg' data-index='3' /></li>1 P% z/ G. Q1 V3 h& p3 G! H! Y
<li><img src='small_img/4.jpg' data-index='4' /></li>
6 e9 A- Z* Z3 P% c" o) V<li><img src='small_img/5.jpg' data-index='5' /></li>
6 w+ Q4 ?2 a- s  K. W) t& G7 d* o</ul>
! M8 ^, F( \6 a</div>
/ _) G6 ?% P5 V6 f<div class='puzzle_column'>$ {- o6 e" c; h
<ul>
0 C$ A# v" K' b8 `2 q8 r<li><img src='small_img/6.jpg' data-index='6' /></li>+ I/ `% R$ d6 Y  e2 R
<li><img src='small_img/7.jpg' data-index='7' /></li>' h' P' f. a  |- l+ `# n  z
<li><img src='small_img/8.jpg' data-index='8' /></li>
. v1 z2 I3 o& W& X& \5 ]9 y<li><img src='small_img/9.jpg' data-index='9' /></li>" S2 ^8 t2 `, q+ C/ D
<li><img src='small_img/10.jpg' data-index='10' /></li>
! \0 V+ |: V$ O4 @! l# D' y</ul>. K% [6 h1 j) F  p9 t- I
</div>- q. P& J/ l% U' E
</div>
$ i) F4 p% t$ s4 {% g; o  Q" l. X<div id='puzzle_right'>
; J! w1 U4 i! l- s& a' C% f2 n7 w<div id='puzzle_canvas'># G* P4 D2 _1 F- m2 C
<canvas id=canvid1></canvas>
1 @  x0 z2 W! J+ S$ s0 |' ~. F<div id='canvas_menu'>) o% B, k# N6 y9 K" t1 I: w* }2 G
<a href='javascript:void(0)' id='photo_delete'>删除</a> <a/ N# [3 y9 y# r( n
href='javascript:void(0)' id='photo_update'>更改图片</a>
* }' i7 s& V$ c$ V6 x</div>
, f: k! Z& Q* l6 l3 O4 y</div>
) H* H! f' x5 v. m& T<img id=bg src=big_img/1.jpg width='600' height='450' />$ C! V% p! i( \# x' ^
</div>
9 a8 M/ V+ M; }+ p- [% P/ l<div id='puzzle_bottom'>+ s" G; }# n5 W
<a href='javascript:void(0)' id='add_img'><span>添加图片</span><input9 {6 Q0 r3 C7 a) h" B2 z/ [, F6 @
type=file multiple= id='fileimage'> </a> <a7 W& |  ?  S! H' ?' A7 u
href='javascript:void(0)' id='upload_btn'>上传</a> <a>点击图片可以旋转,拖拽,
* _! K0 G8 o/ H3 j. B/ _7 L+ N0 |( ]缩放哦!</a>& J% l4 O" h  w) J( O2 |# g3 U# g! K
</div>
, s0 b% S% n* K! l' d' k% I5 Z</div>: d1 M' W1 I* a1 a: X1 m
<input type=file id='test'>+ q7 J) l5 F3 v/ C
<canvas id='test_canvas'></canvas>
/ n/ g/ A/ J$ k0 `3 f. D* z</body>" D/ i' F3 r# v* |- ^
</html>
" i/ j) S% `- @$ @2 L1 ]/ m$ khtml5_puzzle.css代码如下:2 o* Q. i7 W3 i- V( R# J
复制代码代码如下:@charset utf-8;</p> <p>#html5_puzzle {
% i7 C, C7 e  A5 i( C; Zfont-size: 0;
& K# l' U3 `& K, Z}</p> <p>canvas {
. t+ F0 |# _# Y" D+ Ebackground-color: transparent;
- C7 {' {+ j( A5 ]3 M+ Uleft: 0;9 z' @" H' F9 o* k, ^( e
position: absolute;6 s8 L# }+ P$ {4 N( m- C9 h
top: 0;; J: C; K& ^0 k, V- f
}</p> <p>.puzzle_column,#puzzle_left,#puzzle_right,#add_img {9 E- l+ h9 ~# z, K  z
display: inline-block;4 W7 ?4 J, B+ C  G( D
}</p> <p>.puzzle_column li {
  K" b6 e+ L8 j: F2 n& pdisplay: block;
# r# o  f% C' V7 \margin: 5px;
9 N' [) c* ?. i9 |border: 1px solid #ffffff;
) e7 X* I- K- \}</p> <p>.puzzle_column li:hover {$ L( J1 Z" _0 R2 L4 f8 Y
border: 1px solid #3b5998;
$ k$ [; ~4 S" _* x* X& ~7 Ecursor: pointer;
% c3 L) b) p/ L" i}</p> <p>.puzzle_column {$ o: x& _) {9 Q# Y, l6 K
font-size: 0;( A4 @3 [! ^$ G! `4 ^( f
}</p> <p>#puzzle_left,#puzzle_right {) E8 x1 X) D; X
border: 1px solid #3b5998;
' E* I" [; S6 E9 v7 y$ g% b, B, V}</p> <p>#puzzle_right,#puzzle_bottom a {
1 @% g  ]3 |! F" W8 ]% t; ^! ofont-size: 14px;2 W# O, ]) u) k- q- P) A4 g
margin: 10px 0 0 10px;% Q: V  t+ O: t- J
}</p> <p>#puzzle_bottom {3 c7 t3 e0 o' p. j
margin: 5px 0;4 `' y6 [  E0 z& N$ H  |' d
}</p> <p>#puzzle_canvas img {4 q. V5 y$ F  L+ a% w% K! k# k2 S
}</p> <p>#puzzle_canvas {& A! [' |6 I' c. k$ ^
overflow: hidden;
; s8 F5 `, M* T  l1 b, Swidth: 600px;
% E& B1 \4 a1 S+ M. \# [height: 450px;6 o3 M( |$ b' I% n* E# o6 e
position: relative;  Z. ^/ |# C4 v* S0 \
}</p> <p>#add_img input {9 j6 e4 x4 a, {: Q6 b+ y, X( Y
position: absolute;) |% ?% e3 \; c: u( G. O7 S
font-size: 100px;
$ I1 Q3 w( J) O9 M; _right: 0;. ?8 l  W4 Y# G2 V) O
top: 0;- ?! _! ^' r: |9 f: V
opacity: 0;9 X7 R/ g# o7 \3 ?
}</p> <p>#add_img {+ _6 v: [$ I2 q4 W( _3 w( N* ~
position: relative;+ N' L4 p$ J' {; y3 ~# r. `
display: inline-block;
' }3 r$ a1 B4 F7 H3 Ubackground: #3b5998;
4 U  r( K) v) {border-radius: 4px;& F+ D% w: @$ N- c' c
padding: 4px 12px;/ O; E) R9 W9 ^5 ]
overflow: hidden;
& O: o4 W( u; Icolor: #ffffff;3 R9 k# k. o6 M( y" ?4 {) G+ f& ?
}</p> <p>#bg,#show_list {4 e. L( a1 S1 T2 y! R) n# y
display: none;
1 A6 t7 ?9 t% K6 |* p" d8 \& {3 S}</p> <p>#canvas_menu {/ z7 U  x) x) |; V
border: 1px solid red;! P- o" j" B* b9 B+ ~+ M
position: absolute;, {- ?5 Q1 _) H4 h# b9 A0 V
z-index: 5;8 N' _* S7 b! D8 V
top: 0;
, I4 m( t0 j" _. y+ H) C4 Xleft: 0;
* J8 w. {) ?  l; o8 Rdisplay: none;, U) ?5 Y- ]* F3 h7 i
}</p> <p>#canvas_menu a {0 |( E# c% a% n! i* L7 ?1 [
display: inline-block;
3 O  @" I: n3 l% z+ a, p}</p> <p>#test_canvas {
, S$ }7 `$ s: P! W" o' n; gtop: 700px;8 y. i# l3 w+ E/ O9 L) d( G$ X
}! Y  x, {3 R! q! k/ r" U
html5_puzzle.js代码如下:( M" P6 N: A6 K; c2 z8 ?
复制代码代码如下:require([ 'img_upload', '../puzzle/canvasimg', '../puzzle/canvaselement' ], function(
0 l) k0 a- W# _/ Q: Vs, canvasimg, canvaselement) {8 f( X! ?$ _: M
var img=[];2 I! Z) b5 Z- E! R2 A# `
var canvas = new canvaselement.element();
, O3 p% _- \9 t8 m9 V+ \0 ~* Rcanvas.init('canvid1', {/ l4 F2 U% @/ ?/ K9 @& ?
width : 600,
6 r& r" p2 S' E0 G5 M* m- U# V1 rheight : 450! e% i, r8 H' V) l- L
});' i4 a/ h( }) \% _8 U/ \4 M
s('.puzzle_column img').on('click',function(e){. d+ M( y1 z- m4 W# M
var index=this.getattribute('data-index');* w/ k# H4 w% R6 ^: n! C: M0 q
$('bg').onload = function() {' f; [: a: ~# w; k; y
var ctx=$('canvid1-canvas-background').getcontext('2d');9 }$ t# J1 F! `. Z) K
ctx.clearrect(0, 0,600,450);
# c/ }8 H$ J% ~  ?, ~img[0]=new canvasimg.img($('bg'), {});
$ p& {. w/ y2 ?- d. C; S% icanvas.setcanvasbackground(img[0]);' ?% C% q% A' G5 t
};8 v7 j! j; Z) A* z; f; B
$('bg').setattribute('src','medium_img/'+index+'.jpg');
7 g: y2 O( o3 V0 ke.stoppropagation();1 ~* B2 \) K4 e
});
  c1 ?5 ~2 l8 @) @% L/ \var canvasdemo = function() {
, d( B- c' }. E; p3 greturn {
+ @/ X+ b0 j% I. w5 O2 X0 Qinit : function() {) E8 M( e& T8 _" l
var img_list=dom.query('#puzzle_canvas img');
4 ?( J$ Y) ]7 [9 ]# M& Uimg[0]=new canvasimg.img($('bg'), {});0 G# X' A% @. A; F4 s
s.each(img_list,function(i,el){
+ U  V: n! h8 u4 X% Nel.setattribute('data-index',i);# R2 @! M" U8 G/ ^4 O7 y7 M& K6 M
img.push(new canvasimg.img(el, {}));
! o2 A( @5 _- v: ^- y. N# Ecanvas.addimage(img[i+1]);
$ P) K" B$ C" R3 p) v" s* U});
* A, l  O( V8 S! H& x. Z1 L. zcanvas.setcanvasbackground(img[0]);
; a# i% [% l$ g7 G* [this.cornersvisible = (this.cornersvisible) ? false : true;
- v4 V: E# B  p* |this.modifyimages(function(image) {
" A5 X% b5 V+ `image.setcornersvisibility(this.cornersvisible);$ v5 Z& Y7 E# p$ [( d7 k" ^
});
8 K: ]" r2 V, H) R! i% a! y5 C},
/ Z& ]' e4 b  U) m+ T) _modifyimages : function(fn) {
6 m3 s0 O" P$ Q9 k( E  Rfor ( var i =0, l = canvas._aimages.length; i < l; i += 1) {1 Y  S! l( ~/ [  R2 C
fn.call(this, canvas._aimages);, U0 b* \2 v5 ~9 L1 U5 H" ]
}
3 S+ |" w2 r2 R* M- @. l/ B; D7 ^) |canvas.renderall(false,false);! ?, E/ p9 e" @7 _# ]
s('#puzzle_canvas img').remove();5 k3 w2 b& `6 l' u. h
img = [];( F" k, y; N4 S( B
}
) ?0 z) b5 o% `7 u" Q};. a3 E, s1 C0 M- L4 b
}();. I* e% n1 w5 k* \9 N1 t" U
function getcurimg(){
3 o3 I$ d1 M2 Q) r: |var oimg=canvas._prevtransform.target;+ i! l/ B+ I3 z1 E/ e6 Z
for(var i=0;i<canvas._aimages.length;i++){' v) a5 ]3 @- W  d
if(canvas._aimages._oelement.src==oimg._oelement.src){" j) Y' l! w% p! o
return i;
+ b, a- A0 w/ Q4 o9 Y% O" H4 A}5 K) j3 ^  G' Q
}
7 Y; y0 w7 Y5 _& b# Z}
9 _3 F9 |$ M8 v) y8 ~+ ps('#photo_delete').on('click',function(e){
4 q/ D* ~! M7 C/ K) |var i=getcurimg();/ L* y6 E; n1 \( U
canvas._aimages.splice(i,1);
9 {3 P  ~" o  w' o' jcanvas.renderall(true,true);
4 x, X4 V$ _5 e0 @3 p7 o8 L( w$('canvas_menu').style.display=none;  c4 H( H1 s/ q
});0 P' f& [! W" I& G4 i
s('#photo_update').on('click',function(e){
& w; }* f7 K  ~2 @0 o$('test').click();
8 J+ R- g" A  L8 {5 X0 S# Q});
: B! h1 X% E3 P! |' Qs('#test').on('change',function(e){+ Q+ |: {2 C  X* s- b% ?: T
var files = e.target.files || e.datatransfer.files;, i, l( ]- J9 Q$ k/ f0 y
var reader = new filereader();# c- z8 [7 B- S1 h! E
reader.onload = (function() {% j- Z: U2 A' ]8 D# f+ n
return function(e) {2 j# O9 N1 M; h( {7 U" q3 d
var dataurl = e.target.result, canvas1 = document.queryselector('#test_canvas'), ctx = canvas1.getcontext('2d'), img = new image();
* x# x2 [  K- @# A6 @img.onload = function(e) {
- h( A0 X- y2 _( iif(img.width>200||img.height>200){
, k" U9 M+ a1 N7 L! Rvar prop=math.min(200/img.width,200/img.height);2 `5 Z. n, |( h. q/ k, h
img.width=img.width*prop;' |' F% l3 \& C' i# ?! F
img.height=img.height*prop;( y/ z; f5 V( y: t
}8 z4 _2 K* _. {8 ]
canvas1.width=img.width;) E" W+ i' h' a4 U
canvas1.height=img.height;
( H$ h4 N0 D# p% lctx.drawimage(img, 0, 0, img.width, img.height);
* A  _" Z( B- n% ^; Gs('#canvid1').html(s('#canvid1').html()+<img src='+canvas1.todataurl(image/jpeg)+'/>);
: ^$ M$ X# p7 G' C4 V6 gvar t = window.settimeout(function() {
4 K1 R% Y6 i* n5 }, D" q) Z- avar i=getcurimg(),target=canvas._prevtransform.target;6 A% t  Z; B# n# v* F3 Z4 E/ |+ \
console.log(target);
/ i7 }0 L, \' D. c: z0 Xcanvas._aimages=new canvasimg.img(dom.query('#canvid1 img')[0], {
2 S5 N: J% C' T$ w& Z) B6 Y5 L8 S) Btop:target.top,, _0 m8 A' d3 a4 ?) v
left:target.left,
6 |- F  D' g1 Gscalex:target.scalex,. c4 K  t# s: q3 f
scaley:target.scaley,  I% R/ @4 A; d3 @# V
angle:canvas.curangle, j: N* z. z4 T* ~) a. p) m; g
});
! `( L% \9 Z0 N3 vcanvas.rendertop();4 ?; B) u- O0 X9 G4 g! ~- O
cleartimeout(t);
) R( j( B( X9 S4 I- `s('#canvid1 img').remove();. |3 |/ v( B, w5 m
},1000);
4 @7 ^: Z5 J8 o% P$ I0 M1 C: H: M};- D0 O2 ]- b9 y- U4 J( x: I2 K
img.src = dataurl;* Y2 d. l: f1 {" k
};
+ q5 W2 e# _( E+ \* z; W})();
" o- T. A# m* Ureader.readasdataurl(files[0]);2 U% ~8 T3 b( @  }8 K! H( _
});  f6 r* b, y7 O$ |5 Z, w
s('#upload_btn').on('click',function(){* |1 `* q  J- m% R5 n
var imgdata = canvas.canvasto('jpeg');
, O( `" ^: b( C3 |var imgvalue = imgdata.substr(22);
4 J- Y0 C! ^( i! F" s: ss.ajax({. r5 i2 ?1 K* b( K- r
url : 'http://localhost/html5/upload1.php',
1 p/ O, `6 S! @. z  k' |* P' atype : 'post',
! ?, i# f& ~, n1 [# ^data : {
/ H% r# \) M2 d; ?; dimgdata : imgvalue,
6 z. k( U; H2 W3 `6 t5 i, D3 k3 lfile_name :'mix_img.jpeg'
- l3 f4 n' S0 [& P},
+ O. j: m& i* ?4 s& E, Odatatype : 'text',
: p& C/ W+ T1 Y4 j, Osuccess : function(data) {
9 c8 q' v" o7 y  s4 W  C: R1 Galert(s);' n" p1 o  ^! d3 [
}
# E% Z- k2 g1 t7 |});  h! o* h; s! H1 P" P! s
});
8 z2 p; ^  n: U( h2 ?7 z9 y});2 [: J* P6 o4 g& Q  P
至于用html5 input读取图片,这很简单就不贴出来了。
5 D7 f; a' F" [1 |% }, E: e4 a5 m. v0 `/ ?
更多网页制作信息请查看: 网页制作

回复|共 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客服 返回顶部
快速回复 返回顶部 返回列表