返回列表 发新帖

html5版canvas自由拼图实例

382.6k 8
swmozowtfl 发表于 2015-7-10 22:26:00|湖北 | 查看全部 阅读模式
本文实例讲述了html5版canvas自由拼图的实现方法。分享给大家供大家参考。具体方法如下:
, Z* @; a5 f+ Z$ f1 W+ t代码运行效果如下图所示:
! l" X4 @3 c3 @2 Z
! H: s* v* N. ~5 P) P
                               
登录/注册后可看大图
& a$ G0 M% [* d" \8 \0 s# t: U$ g! K0 X
canvaselement.js代码如下:( x5 r% f- K3 s4 ^" S
复制代码代码如下:define('canvaselement', [ '../multi_upload/core' ], function(s) {& k, E9 s8 Q$ V( H0 f3 e" N3 h
var canvas = window.canvas || {};  @% h# R# H2 J+ U4 |
(function () {, R. G' b3 z! |' c9 f1 [
canvas.element = function() {};
: \4 l" h: }* F8 q  t/ dcanvas.element.prototype.fillbackground = true;
3 Z& c2 o$ K, F& ?2 ycanvas.element.prototype.showcorners = false;( Y3 ~9 R! M4 i. k, t! L0 o8 C" v
canvas.element.prototype.photoborder = true;
3 T* z# n# ]+ m2 ]  }) x! kcanvas.element.prototype.polaroid = false;3 n$ W5 e) y+ N
canvas.element.prototype._backgroundimg = null;
3 i; h5 U2 e2 m3 M! M# gcanvas.element.prototype._groupselector = null;$ D8 A  J3 N/ `. x4 V& p
canvas.element.prototype._aimages = null;
" B. k1 I( {1 \! h+ G4 Tcanvas.element.prototype._ocontext = null;
! N" c( e( X6 f8 e' Q& Jcanvas.element.prototype._oelement = null;
4 [6 B+ G2 D# Y7 e# ^8 o* Ycanvas.element.prototype._oconfig = null;
. m6 R) _1 S; Ecanvas.element.prototype._currenttransform = null;8 {0 a' B9 B" G# D9 W$ z2 i6 h9 f+ h
canvas.element.prototype._prevtransform = null;7 w. _& g( d2 j8 {% c5 M  \
canvas.element.prototype.curangle = null;# h2 |2 N3 O( t
canvas.element.prototype.init = function(el, oconfig) {* P5 k; p4 F0 U; c# R$ R/ i+ D( P! ?
if (el == '') {6 V2 o4 P" |1 s, u3 ~
return;
, ~  e3 y' n0 m: s}
, ], S, s5 i0 e8 }" D/ j# N: Jthis._initelement(el);
/ u2 n3 K  O. r" E3 Mthis._initconfig(oconfig);( K# n2 \; C3 F5 I
this._createcanvasbackground();; T/ w$ f# Y/ C% o
this._createcontainer();
& p5 H  ^) p) Y0 S, k' ?this._initevents();% X' I5 r, K4 @+ r5 L9 h" ?
this._initcustomevents();* Y% ^7 h, q5 U0 y# }
};
2 ^) T0 H1 ^' z3 _; a6 ^' U3 [* I8 Jcanvas.element.prototype._initelement = function(el) {
' v  s6 @+ _8 p: Nthis._oelement = document.getelementbyid(el);
( |4 E. L& b/ t% G; g0 Sthis._ocontexttop = this._oelement.getcontext('2d');
, p: t& J7 d: u* {% X4 W' G};+ a' E1 H9 B  J" j4 r& Z0 J, X
canvas.element.prototype._initcustomevents = function() {( L8 _, _" i0 W7 L1 g) C/ @; a  q6 l
this.onrotatestart = new canvas.customevent('onrotatestart');4 k% R& C  F3 }3 @
this.onrotatemove = new canvas.customevent('onrotatemove'); % l2 N, J3 W- |6 P0 U+ z
this.onrotatecomplete = new canvas.customevent('onrotatecomplete');% E# C$ Z& T4 F
this.ondragstart = new canvas.customevent('ondragstart');
) X3 h* N) J4 W0 z3 |. F. Zthis.ondragmove = new canvas.customevent('ondragmove');* R4 j7 K' t& m. Y- ^$ [
this.ondragcomplete = new canvas.customevent('ondragcomplete');
3 `, Q/ P7 U) e0 ]# R};/ n3 G! u) N7 E8 P
canvas.element.prototype._initconfig = function(oconfig) {5 ]( K6 D# s6 a2 W1 I) R8 ?; q
this._oconfig = oconfig;+ Z" G# ^0 h' }
this._oelement.width = this._oconfig.width;9 h! @! c* }+ |. `
this._oelement.height = this._oconfig.height;
# q4 [0 L; g. }this._oelement.style.width = this._oconfig.width + 'px';
, `1 ]) P2 y0 Z0 K5 z4 j# w: c' h: xthis._oelement.style.height = this._oconfig.height + 'px';7 M/ r+ Z: r9 d
};
" \' u3 l& u; S, s1 P# b4 }0 I% acanvas.element.prototype._initevents = function() {
" ^( Q0 T# I$ ], ]  Pvar _this=this;
9 Y# E0 \8 _2 t3 ]1 z  w& Os(this._oelement).on('mousedown',function(e){
: ^( X# g" f9 g_this.onmousedown(e);
  E; _7 Q5 u9 ?' ?6 r/ l) k});
. C1 Y  w4 h/ `9 W: i) zs(this._oelement).on( 'mouseup', function(e){9 C2 }8 M, x% m% L
_this.onmouseup(e);. x1 k9 G& d$ i$ `! {. p/ W
});
. a/ e0 e9 s; n8 Z8 V- h- c/ Ns(this._oelement).on('mousemove', function(e){
8 }4 ?% X+ ]: P3 Y1 D' d0 u_this.onmousemove(e);
0 M# v' u! n3 R, z. `});) W7 [9 j/ y; d$ `8 j  n2 x: z
};
. K/ q/ j2 w( O. ~' V' A$ lcanvas.element.prototype._createcontainer = function() {0 I9 x) S: O- V4 ~. n1 }8 D
var canvasel = document.createelement('canvas');3 t2 t; z' f6 m% H: H: w
canvasel.id = this._oelement.id + '-canvas-container';
/ K% M. L4 g: m/ n4 A% e! lvar ocontainer = this._oelement.parentnode.insertbefore(canvasel, this._oelement);* n0 u) [+ G7 R0 p
ocontainer.width = this._oconfig.width;: \7 v2 l" V) m1 C. Z# R
ocontainer.height = this._oconfig.height;
; Z# s7 `/ o, kocontainer.style.width = this._oconfig.width + 'px';% l+ M, z9 b' N/ u
ocontainer.style.height = this._oconfig.height + 'px';. p0 ]/ R! i- Q2 C- c8 M2 M
this._ocontextcontainer = ocontainer.getcontext('2d'); / g( k" H% g5 L, Q) |  x
};" \% k5 H- Q- Q" a% Q
canvas.element.prototype._createcanvasbackground = function() {# [" K/ k( ?& L; b
var canvasel = document.createelement('canvas');& ~. ?3 W# y, v7 U* e
canvasel.id = this._oelement.id + '-canvas-background';
$ P: c6 b3 Y: F. ?var obackground = this._oelement.parentnode.insertbefore(canvasel, this._oelement);9 M9 v- |& O+ V+ x0 w
obackground.width = this._oconfig.width;
1 m5 u& p- a& `- S! eobackground.height = this._oconfig.height;  e* f8 T' S5 Y
obackground.style.width = this._oconfig.width + 'px';
+ n% `* w% A  r% w& \obackground.style.height = this._oconfig.height + 'px';
. _! @6 T5 [" |this._ocontextbackground = obackground.getcontext('2d'); ) P" h% r0 `2 i( E9 W. J. n2 p
};
3 U' S) j+ l' S. @  q& i( N9 Scanvas.element.prototype.setcanvasbackground = function(oimg) {
! o' p1 J# ?1 r' j& zthis._backgroundimg = oimg;
6 c! ~) e1 Q% svar originalimgsize = oimg.getoriginalsize();
4 j# G2 _8 A. y8 T% Y" }this._ocontextbackground.drawimage(oimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);
8 s8 l' m7 t. c+ w& P};
# D" x" Q2 P4 i. @. xcanvas.element.prototype.onmouseup = function(e) {' q% v6 O$ r5 o' U+ I, L# ]' X
if (this._aimages == null) {# t: \  L4 G+ \% B& B
return;
# f: ?# U9 B" A; i- h}
! }+ A  I2 C+ c5 O! M& ]if (this._currenttransform) {6 L% o& t/ R; {
this._currenttransform.target.setimagecoords();
& ^8 U& J% w9 P( N6 U}
5 J0 Y3 c. P  q# z/ }5 r, _if (this._currenttransform != null && this._currenttransform.action == rotate) {5 `5 ~* n: v: `# }- F/ N8 U
this.onrotatecomplete.fire(e);0 G$ _" M5 s- x  S
} else if (this._currenttransform != null && this._currenttransform.action == drag) {  b" @4 ?* u! b8 v. b
this.ondragcomplete.fire(e);
4 d( B6 M/ U5 Q6 U* _}
) T9 ~. v; b) v8 h8 R: \" ]) }8 [this._currenttransform = null;" y3 K% J" t. j9 H" z; e
this._groupselector = null;2 |( K* I- J% @' ~& R" t! x  \  s
this.rendertop();# F, T% ~# Z& n/ U1 n+ Q
};
( ]" S8 [$ ]' B; E# fcanvas.element.prototype.onmousedown = function(e) {) S6 {  K& C, K( G
var mp = this.findmouseposition(e);
, v% K0 N" J2 w6 `/ p5 Gif (this._currenttransform != null || this._aimages == null) {' T+ B% B9 Q1 V& V0 [' N
return;
* F  X4 d: n/ l+ N}
' K, ~. O# i; Z2 ?$ s, P/ mvar oimg = this.findtargetimage(mp, false);
. X9 Y" ~6 Q% d, L4 l( r$ Lif (!oimg) {, ~' m  ~( f/ H' S6 y" ~# A  S* Y
this._groupselector = { ex: mp.ex, ey: mp.ey,. ?4 G' j7 m3 o# t0 U
top: 0, left: 0 };
5 {1 q0 o9 P4 |) `}
, _% D8 X2 V7 h& [) pelse { 3 J5 m  J0 O' x- b
var action = (!this.findtargetcorner(mp, oimg)) ? 'drag' : 'rotate';
/ w. Q- V! S3 O' u. l* nif (action == rotate) {
4 W! h$ p2 F7 V, V6 D3 n0 Mthis.onrotatemove.fire(e);* i! k. W5 H6 Q# X4 o; Q
} else if (action == drag) {
0 H, I  x& I0 H5 r# c6 Kthis.ondragmove.fire(e);5 `& y  k% `+ p- g# }+ e: I
}8 }% @  u9 T% N! H6 h8 u2 q
this._prevtransform=this._currenttransform = { * @- K7 o2 D6 s% n. \* K' g
target: oimg,
3 s; F5 p2 y. {+ n6 B2 \action: action,
# S1 x' H3 ~$ L' V  Y; Q  v$ lscalex: oimg.scalex,
: _  E; f0 B9 Z! C' Y  M( Zoffsetx: mp.ex - oimg.left,
; a, X; {" X( r% X0 t+ N& ioffsety: mp.ey - oimg.top,
5 ^5 A2 I5 J4 t* }ex: mp.ex, ey: mp.ey,, b1 V) i; F4 V5 Q! @
left: oimg.left, top: oimg.top,
( o6 z8 F& m  b" k$ F' x, gtheta: oimg.theta
" {  q% K: w8 U# t+ n$ V3 [7 S};
5 |  n/ E8 K; `7 ]: n; X$('canvas_menu').style.transform='rotate('+oimg.theta*180/3.14+'deg)';
$ V  R/ {4 f5 V  n+ `# F$('canvas_menu').style.left=oimg.left+px;  c# }0 f; {: i
$('canvas_menu').style.top=oimg.top+px;* f0 j* T9 I1 Q
$('canvas_menu').style.display=block;
( S& M; T5 f: ]3 u# \this.renderall(false,false);, M; J7 X, [: M& s6 n
}
! V5 |1 {$ Z) L# `( S+ N# x};$ _- y+ N9 k' ^
canvas.element.prototype.onmousemove = function(e) {
" t) @% b1 R2 Z/ Y9 g6 evar mp = this.findmouseposition(e);
" K7 E" b) F9 U; N( {( E1 kif (this._aimages == null) {* v4 T) x, o# _' l. f* l
return;
9 F  O7 p; E! w# F! y}
6 R! J% y- A! K: Lif (this._groupselector != null) {
1 V9 [% G* j" G& C$ j* `2 Vthis._groupselector.left = mp.ex - this._groupselector.ex;- J9 X- }) ^: G3 k* K: v1 h/ I+ v  A
this._groupselector.top = mp.ey - this._groupselector.ey;# X* O3 ^* [) g5 `1 K3 A
this.rendertop();
- @& e% h+ q$ U/ x}- w5 Y4 R4 E1 f' g
else if (this._currenttransform == null) {: S& I2 e7 J$ E" s* l& p% g
var targetimg = this.findtargetimage(mp, true);. K8 Y6 J9 ^9 a' j: x+ A
this.setcursor(mp, targetimg);
) Z+ U" H1 I* W0 ~4 j}
) \* e3 _/ @* O2 p! Aelse {* Q$ \% T1 j; S7 J0 `1 \9 l) V
if (this._currenttransform.action == 'rotate') {' j) q) Q- ~6 J+ L
this.rotateimage(mp);& O: k% C- f: |% `, B/ H
this.scaleimage(mp);! ~0 [6 s4 A  S0 [6 I6 ~+ G
this.onrotatemove.fire(e);& G' D/ ]  W. h9 p) f! V" t+ t
}
& P3 x- @2 }: C; e/ pelse {
8 b, F# M5 }# s3 athis.translateimage(mp);& f! _  c$ g% A1 O6 a
this.ondragmove.fire(e);" Q7 ]; g, u& v2 G' P
}: x3 t3 ?# r  i) }) }& O
this.rendertop();
6 y& h# \+ i% V9 W9 C} $ {0 N% ?. w& j2 B6 ]5 X7 N
};* c1 b6 |+ z8 q3 l$ E- y
canvas.element.prototype.translateimage = function(mp) {
! E" h  N6 m! othis._currenttransform.target.left = mp.ex - this._currenttransform.offsetx;$ y' l5 K( d/ Q+ O
this._currenttransform.target.top = mp.ey - this._currenttransform.offsety;) V1 m' l0 t6 l
$('canvas_menu').style.left=this._currenttransform.target.left+px;
. J, V& b3 h. @5 b4 E$ u7 K$('canvas_menu').style.top=this._currenttransform.target.top +px;! C+ z3 T, p( {
};  k$ ?9 o5 x" t+ B9 L1 ^4 k$ K
canvas.element.prototype.scaleimage = function(mp) {; V; E/ d7 L. N2 l! o
var lastlen =
6 a/ g5 L* u2 {9 w5 _# j, gmath.sqrt(math.pow(this._currenttransform.ey - this._currenttransform.top, 2) +. D. A' ]% [) B" y- x5 g
math.pow(this._currenttransform.ex - this._currenttransform.left, 2));
, r6 Q/ I! X; d3 v$ `  kvar curlen = : X* i: ]' m: {( U3 O& ?4 a
math.sqrt(math.pow(mp.ey - this._currenttransform.top, 2) +  I: B3 D; I% i" H, a
math.pow(mp.ex - this._currenttransform.left, 2));" w3 P# v. {: d9 P: Z
var curscalex= this._currenttransform.scalex * (curlen / lastlen);
6 F; m& v% @' f1 x$ v' Cvar curscaley=this._currenttransform.target.scalex;
8 {5 R: K3 O" z' u# _8 m( X2 gif(curscalex>0.7&&curscaley>0.7){
# n1 @2 c6 ?7 J; O" E0 Pthis._currenttransform.target.scalex =curscalex;  j  n& b! y% _* x7 ^+ h9 R
this._currenttransform.target.scaley = curscaley;0 }' u2 r5 o& ^1 W
}
2 \5 \  ]4 v$ i& m};
4 _8 j; s% n: g" f+ v5 jcanvas.element.prototype.rotateimage = function(mp) {
: S3 ?, N% G$ N9 Lvar lastangle = math.atan2(( ~4 M' p; e5 r; O9 K, k, z: L
this._currenttransform.ey - this._currenttransform.top,
3 L* P# O; D% z4 u# q& q6 m2 l2 |this._currenttransform.ex - this._currenttransform.left
2 y4 v; ?& X* b7 b);4 v4 t! @- m1 p6 j  D1 V
var curangle = math.atan2(  l0 E; |1 @, w* q
mp.ey - this._currenttransform.top,& e  b  y3 D% I; }+ c6 B
mp.ex - this._currenttransform.left
! G, w: J6 x3 A" \);
9 [/ F, p( c, z9 _+ dthis._currenttransform.target.theta = (curangle - lastangle) + this._currenttransform.theta;
# H7 Z9 X8 u; D% Uthis.curangle=this._currenttransform.target.theta*180/3.14;5 D( [% K$ h% _3 L
$('canvas_menu').style.transform='rotate('+this.curangle+'deg)';
3 H; ~$ f! V# R$ ^/ m' J3 `};4 l5 Q/ V' L1 B/ k+ n9 R4 |
canvas.element.prototype.setcursor = function(mp, targetimg) {+ ^4 `' z) t! Q$ c
if (!targetimg) {6 k' c* ^9 e& C, K6 W/ d. V
this._oelement.style.cursor = 'default';
$ @! p9 c0 L; E0 I' d}
; k' }# [3 W2 J$ {& g& M/ Oelse {
0 z# l2 c( a4 wvar corner = this.findtargetcorner(mp, targetimg);
! ^  q$ N* G: e2 w& b% u" ~if (!corner) # A+ J% n* i7 q5 E4 V$ ^
{
8 h3 W, {+ A& e3 Q7 t5 Uthis._oelement.style.cursor = 'default';
$ B: G1 O# Y, u+ k9 \8 u+ }! D}1 u, s; ]3 |6 s+ Q! j9 r+ P# B4 ^
else
6 Y5 P1 y& _' z{! ^6 ]+ j' }8 d3 v' Q0 a
if(corner == 'tr') {
. Q5 e* b$ D- l. y3 ~( ythis._oelement.style.cursor = 'ne-resize';
; h& t7 C( p* F# ^3 q9 K* H6 B) b}- P" O% i: v, ^+ n  b
else if(corner == 'br') {
' G3 L/ q  ?7 l# B" o) sthis._oelement.style.cursor = 'se-resize';9 S; C# P5 o, `* G3 K, R( y
}: h+ x- ?2 `8 J# J+ p6 e$ e1 H
else if(corner == 'bl') {
$ r2 M* ~- H; F) uthis._oelement.style.cursor = 'sw-resize';* J& T. _( Q. w
}
  E! B# E, G3 s5 f/ celse if(corner == 'tl') {4 p1 s. ]+ r8 s/ O5 [
this._oelement.style.cursor = 'nw-resize';
9 f7 a' b7 t6 N/ ^}
7 Z8 S! C* c: Y9 ^else {
" R( {2 Y: B2 ?% K3 L7 othis._oelement.style.cursor = 'default';
! d8 |% T% G1 t7 m' z2 l}
: l6 b1 f& J  M0 e5 q$ t5 R}- J8 Z! @% D% G' H( z
}
1 J% U- o( z8 |};, ~+ T& k2 P' O' f. l! _1 @
canvas.element.prototype.addimage = function(oimg) {7 H6 S8 u  N3 K  e; }
if(s.isemptyobject(this._aimages)) {
- s" l3 M1 j: H/ G& qthis._aimages = [];
; M+ v, h5 }5 J6 U) J4 ~}
0 f- y0 i3 ]2 \this._aimages.push(oimg);
4 s  x  p3 \5 ~* qthis.renderall(false,true);</p> <p> };
+ }% a, j( E+ X0 R" Y' ]canvas.element.prototype.renderall = function(allontop,allowcorners) {, x+ U: }1 \+ |5 Z) Z# W) f+ q
var containercanvas = (allontop) ? this._ocontexttop : this._ocontextcontainer;
" K0 C! ^1 n. f1 ~" e3 Z6 Z0 \this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
1 I3 O# T5 w. rcontainercanvas.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
0 R$ q" p+ H$ jif (allontop) {7 |: S. g, |5 f7 O1 \4 Q
var originalimgsize = this._backgroundimg.getoriginalsize();
7 U- @+ r" T0 v2 Z% P8 o6 D1 Athis._ocontexttop.drawimage(this._backgroundimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);
' ^1 u9 O5 Y' D! U}7 d( |' K+ f4 \* L5 p
for (var i = 0, l = this._aimages.length-1; i < l; i += 1) {
4 D; Q' u2 g- r' P  e; d3 Lthis.drawimageelement(containercanvas, this._aimages,allowcorners); : f3 @7 g: v. `& ]2 s
}/ v, V3 G. H3 t3 q& R) c) V
this.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],allowcorners);6 f6 d$ G, k) c
};
, o( u4 M8 H# q  J8 l* scanvas.element.prototype.rendertop = function() {
( @/ x4 i, o7 B6 l- e( j% othis._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
2 u6 {/ o( F/ B  {) ?this.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],true);+ m! ]- \5 i, O  |4 P  T
if (this._groupselector != null) {$ U1 \9 C* v/ P, }
this._ocontexttop.fillstyle = rgba(0, 0, 200, 0.5);9 H$ ]: i. s( d" ^8 m/ U
this._ocontexttop.fillrect(; }3 ?* V- a, F  f2 J4 {
this._groupselector.ex - ((this._groupselector.left > 0) ?
9 L- |' z/ _; r: {2 h2 d- M! o0 : - this._groupselector.left), 5 c* \" A$ N: w# j& u" }  |: G
this._groupselector.ey - ((this._groupselector.top > 0) ?
) _# V/ N( z8 I0 : - this._groupselector.top),
6 ~* q& [  C' gmath.abs(this._groupselector.left),
+ d( [8 t0 L/ a* _/ c4 \math.abs(this._groupselector.top)
1 R/ j1 y* q4 r/ S);
. ~* c( H# m: qthis._ocontexttop.strokerect(. M( t5 m0 @' I6 ]/ w: h) A4 S: u# z
this._groupselector.ex - ((this._groupselector.left > 0) ?
- `7 X" t/ A" r1 p8 A$ C( v0 : math.abs(this._groupselector.left)),
/ S/ Q. w8 ]* \2 p9 Nthis._groupselector.ey - ((this._groupselector.top > 0) ?
9 u( Y- n: d9 J5 a% p2 f0 : math.abs(this._groupselector.top)), . C* U/ I1 w4 i* s. M. B
math.abs(this._groupselector.left),
8 s! A) Y# ^" L- |# Lmath.abs(this._groupselector.top)
- e2 p3 @! @1 p4 k" H6 l);
) E1 \, k  F- A- S/ u% f6 M9 y/ D}3 s2 W8 K8 n$ M; \3 `$ X2 b4 ^
};
3 o7 w2 r1 ?0 Y4 A+ X1 fcanvas.element.prototype.drawimageelement = function(context, oimg,allowcorners) {
5 t$ v' f% V% I. x  e% Joimg.cornervisibility=allowcorners;0 ^. D. b9 V  ^# S6 B, S! A7 e
var offsety = oimg.height / 2;2 H& W4 c3 w1 d7 b9 O3 n7 |' N
var offsetx = oimg.width / 2;
, {7 S* \- x- C. ocontext.save();
; _# i9 L% {! G  Ccontext.translate(oimg.left, oimg.top);. d1 l) ^6 [: G
context.rotate(oimg.theta);4 r5 C+ W& K+ C
context.scale(oimg.scalex, oimg.scaley);
9 _7 c: e6 H0 |/ s' {this.drawborder(context, oimg, offsetx, offsety);" W) J6 z7 Q3 G1 s- E' V, m
var originalimgsize = oimg.getoriginalsize();8 e3 k( b5 s+ p- l- q) e, O5 d
var polaroidheight = ((oimg.height - originalimgsize.height) - (oimg.width - originalimgsize.width))/2;* n# }& g, n8 B1 _6 H7 p6 y
context.drawimage(+ e) G6 D% P, [
oimg._oelement,
1 o! y% j* e+ f  L+ _4 w- originalimgsize.width/2,
- b* F, x  w6 i6 |" v" W$ R((- originalimgsize.height)/2 - polaroidheight), " {9 C+ s0 m6 p* G- o
originalimgsize.width,
2 p9 i1 y5 y0 q3 j  E6 e% ioriginalimgsize.height# r0 V4 Q4 ?' P2 Z! L  K0 p
);' Q! v- A, f0 r3 ?: A# |
if (oimg.cornervisibility) {
+ z* q/ K# C& f  N2 f, vthis.drawcorners(context, oimg, offsetx, offsety);$ D1 @& V; Z( d1 Y
}, Q% j& r0 @; t- W: k, p8 x. z
context.restore();  M; Z+ K! L2 D: f7 i
};
2 ]) j5 o7 r( \- W8 M, Ocanvas.element.prototype._getimagelines = function(ocoords) {% J8 R0 Y) p# e
return {
/ l4 ]& P: Z" }& q$ B3 I) M9 ttopline: {
3 R/ `7 }; f% M; O, M- R0 N- Do: ocoords.tl," X0 C7 F. j: V! {% F! M# q4 y
d: ocoords.tr
- w$ c8 i8 M' \$ y9 q  s7 R7 F},
  M& V, N; p6 ^. y& Crightline: { / C# x# W1 i% T: @
o: ocoords.tr,: T6 j' Q# u6 R# |
d: ocoords.br % v2 Z( T) d9 j
},
; o4 m/ o2 \* c8 ^bottomline: { , z9 h7 J% u6 k0 ]$ K
o: ocoords.br,3 ^7 C  B  l, X5 P: m+ z
d: ocoords.bl
# v+ N8 F  z% c& K},+ I% F$ z7 q9 i- V
leftline: { " o- [6 }3 z, S+ {: f7 ^# F
o: ocoords.bl,& \, |# R5 w/ `& r3 _2 M. Q
d: ocoords.tl
8 M3 @" o6 e+ s" f5 d}, P$ W1 H: C: K- U( @. v- T* R
};8 K% a5 o4 ^* r8 C1 ?
};
! y4 @! u; K) v+ Icanvas.element.prototype.findtargetimage = function(mp, hovering) {
9 C; V0 ?, c, e8 W, dfor (var i = this._aimages.length-1; i >= 0; i -= 1) {& z# Q' }. ?! K
var ilines = this._getimagelines(this._aimages.ocoords);
+ Z" L0 s' C5 x* d3 E1 ?var xpoints = this._findcrosspoints(mp, ilines);1 g! F" k7 m( O2 K# T
if (xpoints % 2 == 1 && xpoints != 0) {, y$ b- m! R3 Q( e8 t/ \; B
var target = this._aimages;
1 p+ A% T* p. n* o' x1 Nif (!hovering) {
1 h# o3 [7 v+ g$ Vthis._aimages.splice(i, 1);
6 Q9 b: {7 i) F0 athis._aimages.push(target);' b1 t. N! V+ |. H3 }
}. e' \- b" y" ~, j# F9 e) o
return target;
& S4 `4 a& q4 k$ a: e8 Y# x, Q}* F, u) l2 f. w
}
* R3 ?- C5 X7 E4 H: l! ~return false;
# ?) c/ M7 ^8 U8 D5 [};
5 T5 W9 o, S. H' y- O# jcanvas.element.prototype._findcrosspoints = function(mp, ocoords) {
) Y& G& q5 v2 @9 ~3 D4 l' @4 N0 ?var b1, b2, a1, a2, xi, yi;
9 Y# u0 ~- E" q' f2 [var xcount = 0;8 Y* G1 A- a( h/ j
var iline = null;' v9 T8 |+ ?8 e1 G8 y3 z
for (linekey in ocoords) {
3 N- g& y* b" Y4 wiline = ocoords[linekey];
& d' b* j8 b6 fif ((iline.o.y < mp.ey) && (iline.d.y < mp.ey)) {
% o2 `9 v. J- icontinue;, I2 s) G2 z. m$ A2 R9 Z
}
4 F0 {+ _0 m4 X8 Zif ((iline.o.y >= mp.ey) && (iline.d.y >= mp.ey)) {
: T. e" i! B1 y0 @- K2 |3 ocontinue;" _8 m3 J% S3 s$ n
}! ?- n/ ]; @9 f! B! ?9 O
if ((iline.o.x == iline.d.x) && (iline.o.x >= mp.ex)) {
7 J2 L6 R0 B( q7 Uxi = iline.o.x;
; J0 e4 a' X" t5 A& Uyi = mp.ey;
5 {% N; p& U8 N4 l}
1 k/ F9 k$ N6 `4 [- O3 ~$ y3 ielse {; {6 M3 `$ U9 N; n$ ^: L9 A- b2 t
b1 = 0;
9 l0 ^6 A2 u4 W, U  n$ H! P, Db2 = (iline.d.y-iline.o.y)/(iline.d.x-iline.o.x);
2 P# m% ~2 a. F9 i: [a1 = mp.ey-b1*mp.ex;
* G/ b5 X1 w# z+ t  B/ N1 C' ?! Ka2 = iline.o.y-b2*iline.o.x;6 Z; O) v; v1 \
xi = - (a1-a2)/(b1-b2); ) h8 D# ?% {- v6 R$ [3 K3 d5 D
yi = a1+b1*xi; 7 {+ c; Q' Q& `; M1 a" z
}- C6 e$ [0 e5 R6 Y# q. u
if (xi >= mp.ex) {
& Q3 j9 ]/ u8 @xcount += 1;
# @' Z6 M8 R5 f: p( t}% j: W) m4 |7 U3 k- S* P3 L
if (xcount == 2) {
8 [: i8 R- z1 s4 Y# }$ D) {# ^break;7 \+ a" B! p7 k. z- `3 p# j5 F+ @
}: k" X9 L. k# Y: g
}+ i6 i+ ^& q4 F1 n3 G* [4 W
return xcount;
5 |; l: T! c' P# {};! ?4 L' R9 D2 l  ?3 n: }: ], x% C4 ^
canvas.element.prototype.findtargetcorner = function(mp, oimg) {7 f7 P7 r" I' i6 A* h' G& a
var xpoints = null;
/ ]( A% Z5 C2 X9 p7 p1 Xvar corners = ['tl','tr','br','bl'];$ V+ G% ]# ?: {/ ~+ F! H. M4 r
for (var i in oimg.ocoords) {
9 a4 C* ^+ Q7 \$ Dxpoints = this._findcrosspoints(mp, this._getimagelines(oimg.ocoords.corner));
8 N3 S& E# Z% @; c# u5 I% Hif (xpoints % 2 == 1 && xpoints != 0) {
  ]. L( _8 X" G  ^6 Vreturn i;
4 `! D' U$ B, ^! i9 S- J% f}
+ a3 }4 b5 `$ d5 o}
" F5 m! ]) _) J6 X% I' _return false;6 x7 o0 J+ w, y
};8 ?) V7 ~: Y# e: d/ z: Q" u
canvas.element.prototype.findmouseposition = function(e) {
- K. ]" j) ]  n' D3 ]; avar parentnode = (e.srcelement) ? e.srcelement.parentnode : e.target.parentnode;3 H* t% ]% h- i8 V( T
var issafari2 = !s.support.ie&&!s.support.firefox;
2 k/ M' z  j( K$ ]2 I% kvar scrollleft = document.documentelement.scrollleft || document.body.scrollleft;: }/ v" X0 q; `6 x8 U# b6 K  X) d1 {
var scrolltop = document.documentelement.scrolltop || document.body.scrolltop;
$ A& y8 t7 m! W3 y, P2 o4 U2 uvar safarioffsetleft = (issafari2) ? e.target.ownerdocument.body.offsetleft + scrollleft : 0;$ E6 k! m2 k# |2 k
var safarioffsettop = (issafari2) ? e.target.ownerdocument.body.offsettop + scrolltop : 0;) [4 r8 G3 d; H2 `7 o4 r
return {
# W8 ?( P) s7 U. H0 vex: e.clientx + scrollleft - parentnode.offsetleft - safarioffsetleft,2 B3 a% O5 E5 A2 g6 }
ey: e.clienty + scrolltop - parentnode.offsettop - safarioffsettop,
. I3 @" I' ?$ h. Xscreenx: e.screenx,$ j) g. A# V/ f2 k
screeny: e.screeny
! s2 q0 \2 j5 C( c& K  T};
1 o% Y7 N& o3 U6 s: b};8 p" T! g* S; T: R; ?$ r3 X# k5 G  Z
canvas.element.prototype.drawborder = function(context, oimg, offsetx, offsety) {
/ q4 T: a6 x. Z& @: W6 cvar outlinewidth = 2;2 d: D- ]5 M2 t7 c9 a  B, M
context.fillstyle = 'rgba(0, 0, 0, .3)';
+ `& S/ j' @! a  T4 r7 L: U  |context.fillrect(-2 - offsetx, -2 - offsety, oimg.width + (2 * outlinewidth), oimg.height + (2 * outlinewidth));
! S0 n! Z* D2 H2 Hcontext.fillstyle = '#fff';& S; T3 I; l5 m7 w
context.fillrect(-offsetx, -offsety, oimg.width, oimg.height);2 Z) o( C# Z" n2 J. Z0 E
};* O5 `- S( a' D  p
canvas.element.prototype.drawcorners = function(context, oimg, offsetx, offsety) {
& H5 D1 _+ }0 }8 d7 c6 c8 Scontext.fillstyle = rgba(0, 200, 50, 0.5);
" s' e3 o0 N: F) econtext.fillrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);- Z" j2 p  C1 a$ P" {
context.fillrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);4 s. Z1 ?4 l$ w7 h5 T
context.fillrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
) J' b3 S/ o6 O) \0 J% \$ x# Ccontext.fillrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);  s2 ]6 g( W0 l. L5 L4 I9 E5 F
};
) R2 M: b' Q& P8 D; |canvas.element.prototype.clearcorners = function(context, oimg, offsetx, offsety) {
3 r( ~3 U1 T4 I$ _, gcontext.clearrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
% j# Q4 O3 f0 f0 g: Kcontext.clearrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);
# {5 j" f6 N7 V7 R6 K' Fcontext.clearrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
( y3 h9 G$ h' {0 o. H4 V6 t" h  h1 |context.clearrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
0 v! a6 {# w! c) g2 Z' Scontext.restore();; y& M5 D& {' I/ [+ T+ |
};4 {$ i  t% d- |% U
canvas.element.prototype.canvasto = function(format) {
; B3 r( Q+ b8 X) z$ h8 c! uthis.renderall(true,false);
3 z" C9 I4 q; x3 N+ |) Qvar containercanvas =this._ocontexttop;$ P, Z1 N2 @: G7 ~3 W
for (var i = 0, l = this._aimages.length; i < l; i += 1) {
9 K7 U& {! L- n; [1 z$ Ovar offsety = this._aimages.height / 2;
, e0 j4 N& F+ T( G; Q5 X$ Bvar offsetx = this._aimages.width / 2;# d9 k! y! n, Z! ]8 x/ B
this.clearcorners(containercanvas, this._aimages, offsetx, offsety);
) q! l/ M1 z  ?: Z3 r" x}
3 u# j& b. s) E' y9 Z' xif (format == 'jpeg' || format == 'png') {0 {1 j7 {; X5 V1 U; E, W4 y
return this._oelement.todataurl('image/'+format);$ _& p: O/ p. o
}
/ E8 }1 z. x, [" O# b6 [0 ^5 L};
  y( D0 d  E9 `2 s# ]. [* Acanvas.customevent = function(type) {
7 E, \' U8 K5 I8 n1 Mthis.type = type;, w) W( r4 x) ^# [$ H& j: x: E
this.scope = null;
" d! @" G: P: [6 d9 [this.handler = null;5 L& B! i0 H* u  R% w0 P) O
var self = this;5 W7 a! m+ C% D, [
this.fire = function(e) {
# ~4 j- {& @/ m9 l* l' F/ qif(this.handler != null) {$ B4 U( E" a0 g0 R4 {* y- G6 H
self.handler.call(self.scope, e);, J4 W3 W4 x2 M/ b$ Y& G
}# p) w. V% q0 F& q2 V
};- g8 s7 H2 G- g" z
}; 1 N4 l$ R3 J( Y6 S& V! ?# o! Q
}());; ~0 p$ t& n0 Y5 K8 F
return canvas;' F9 }$ @( Q; \
});
# w" \5 a- {$ D; jcanvasimg.js代码如下:% p8 r: L* q1 ~; A
复制代码代码如下:define('canvasimg', [ '../multi_upload/core' ], function(s) {
. ~. M& n* ~5 X/ j+ {9 w. `var canvas = window.canvas || {};% D" \4 F( v+ V( f$ j  Z
(function () {/ G( E0 B/ J; w9 ~# M3 C; C
canvas.img = function(el, oconfig) {
$ T0 H, L; g, q8 B2 r7 ?this._initelement(el);
/ s6 J. z4 F& B) Z# A4 v( ?this._initconfig(oconfig);
/ T1 _( h% Q7 Z1 O" }this.setimagecoords();0 w) }4 k  p" {5 }- r" t
};9 ~& T: r' }/ i6 X/ i( |
canvas.img.css_canvas = canvas-img;# n3 L9 S# U, K$ w: `
var default_config = { 7 E8 o" a% w0 D  {  V
top: { # t0 }% Z0 s6 U  @1 k
key: top, . O8 R5 c, {1 j- h# \  P6 r, ^) k
value: 10 ( h3 S% ?( h% s, U2 p5 l
},
+ ]" E% a0 f  s+ ~, fleft: { " K7 |! A  R) B* d: X; L
key: left,
$ N; _8 E2 g! F  x& ?, I9 U1 ?value: 10
, q( S" E7 I  p. |; ]: ]},
; h+ h+ s8 b( ^" b/ {9 mangle: {
, N3 R0 G& d% {; o: }key: angle, % k2 x# |6 Y9 ]  V  l$ q' \  v: u; g
value: 0 ! B; d4 @3 G, i6 ]
},
1 ?% P: D- u: x( [6 t; Jtheta: { 4 N! x( J: a4 }7 f/ G/ h: {
key: theta, 3 C6 j% m/ R- I
value: 0 # ?: i. T+ l7 J0 ^
},* r( d3 f# Z3 D9 r8 r
scale-x: { * z+ \1 k$ i/ b$ z% d  q
key: scalex,
/ K  m  _$ m+ u5 g* l) pvalue: 1
9 S) F1 I4 v9 T% T' Q2 m$ `},
+ p( k7 c7 K9 T0 R% Q* Hscale-y: {
& G4 L, H( b2 ikey: scaley, " Q% d/ m& d# Q& h/ W
value: 1
3 m" @4 J( A3 g+ s+ l- [},1 w3 k1 v5 d! K1 z8 ^; S9 e, N
cornersize: { ; e0 t+ r7 O) \2 E7 }9 [
key: cornersize,
# N3 h7 m: U8 s+ ~2 O# kvalue:10
7 O; n: e; e/ _$ V/ m( R1 k}," _+ V* F. s) F; t
borderwidth: { 3 _" ^2 X* Y; T+ w% o1 l1 O4 K
key: borderwidth,
6 m" V7 x' w6 D8 M* s8 pvalue: 10- z- v% z0 ^' h2 B
},
' f" ~7 K/ I, b# u  v* J! S7 V6 T. }polaroidheight: {( h3 _6 ^/ `* _$ O4 ^! M
key: polaroidheight,1 u5 @* _/ H7 P; e$ I+ Z- a: D9 @% [$ A
value: 40
+ N- n$ }! r+ j; D! M},( b3 q( E7 o3 u- _2 D6 l
randomposition: {: w6 J7 c6 v1 h/ @- l
key: randomposition,
5 x  X! `9 d4 R+ Svalue: true
2 d+ @# ~6 c0 }/ E}
9 g$ n" N+ o+ ~: v! G};5 K1 U2 J2 w) n, H; |
canvas.img.prototype._oelement = null;6 o( _  P4 G9 k3 k. h
canvas.img.prototype.top = null;9 }! z" g$ s; t2 N
canvas.img.prototype.left = null;
' O+ p7 [/ M8 v+ m$ e7 Ucanvas.img.prototype.maxwidth = null;
+ g+ V7 z* d( N8 x/ n7 W; K) ocanvas.img.prototype.maxheight = null;9 g/ S# X3 [. _$ \
canvas.img.prototype.ocoords = null;
  D: c% h0 z) D& {8 f; wcanvas.img.prototype.angle = null;
+ r2 W2 ~0 G8 E' ucanvas.img.prototype.theta = null;- ^4 x$ a& V& M! `3 n
canvas.img.prototype.scalex = null;
9 p9 L6 v" O) D  `/ ncanvas.img.prototype.scaley = null;
" m* [3 D# E9 J! w4 Dcanvas.img.prototype.cornersize = null;
- P! O  b$ R0 \& x& I. T6 s5 Rcanvas.img.prototype.polaroidheight = null;
4 q9 {; F% ]( y1 q1 Bcanvas.img.prototype.randomposition = null;  d, k8 l9 B* N* X
canvas.img.prototype.selected = false;
4 c6 w  C+ _5 o/ Vcanvas.img.prototype.bordervisibility = false;
( D; h* ]: x; r* P! V' T* Scanvas.img.prototype.cornervisibility = false;
0 o+ d+ {3 g/ Ecanvas.img.prototype._initelement = function(el) {
4 w! {; k- c+ s$ C) vthis._oelement = el;
# E. P' Y% f0 w};. g6 M4 @- Q6 Q
canvas.img.prototype._initconfig = function(oconfig) {. z, r* v% y6 T, u( v
var skey;
' Z' l8 L( e( y  A% F9 X# |for (skey in default_config) {
2 u6 U8 A4 i1 D- _var defaultkey = default_config[skey].key;
4 l' a( f& G% ^" Y) iif (!oconfig.hasownproperty(defaultkey)) { // = !(defaultkey in oconfig)4 r4 P5 k: G) X+ Q: l' @8 I
this[defaultkey] = default_config[skey].value;
: t$ a  m7 T- c+ N}
/ h6 H3 {) n  m* C! r  Delse {" Z: U1 t0 Z& ~
this[defaultkey] = oconfig[defaultkey];; T8 d, }  l# k+ I, f
}  d- \" c! |4 P4 P
}4 B; M- `( M, U+ X$ G
if (this.bordervisibility) {
8 O* w5 O8 _2 L! x# f/ hthis.currentborder = this.borderwidth;
/ A% G  J* |9 z1 B* }) e  A. ^" P}& y) @& M3 k- m2 V) F. z$ |
else {) h, c5 q" H: B3 i
this.currentborder = 0;. c& G$ E# f7 Y2 s3 ~3 ]. u/ d- u
}
, y. g- O1 l# Q8 T3 F! ?2 Q1 mvar normalizedsize = this.getnormalizedsize(this._oelement, parseint(oconfig.maxwidth), parseint(oconfig.maxheight));# r5 f% t) F5 r2 ^4 }: Q
this._oelement.width = normalizedsize.width;( s1 @' Y  h+ m$ V5 g
this._oelement.height = normalizedsize.height;
5 u9 V8 Z6 ~$ lthis.width = normalizedsize.width + (2 * this.currentborder);
0 r1 M- L& K+ c' ^this.height = normalizedsize.height + (2 * this.currentborder);  \9 o( w4 ?1 w: R! U8 {
if (this.randomposition) {9 h; o4 [$ ?4 N4 p0 Z
this._setrandomproperties(oconfig);; B" M, D& b6 y) v' j
}
# p- J% l3 Z+ c. ]this.theta = this.angle * (math.pi/180);
3 q) v7 N/ V' I# e, v6 O};
! a+ z+ G2 f* D/ b/ W% s+ v; Ecanvas.img.prototype.getnormalizedsize = function(oimg, maxwidth, maxheight) {
6 o" V1 w: a6 T5 @% wif (maxheight && maxwidth && (oimg.width > oimg.height && (oimg.width / oimg.height) < (maxwidth / maxheight))) {
2 @+ |5 H! N0 _normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);! i! o3 I, s+ c* d
normalizedheight = maxheight;
8 N5 D- q8 r4 S! ^7 s! s- x- c}
3 Y4 q' {3 E& `6 Q& Selse if (maxheight && ((oimg.height == oimg.width) || (oimg.height > oimg.width) || (oimg.height > maxheight))) {6 u2 h& n3 u2 J( v0 H' L0 n
normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);
- q7 t: W) y  a+ M7 b# B- w6 gnormalizedheight = maxheight;
# ?4 W. s  c3 S' v9 ~+ [}2 |& n* q; p6 V6 o
else if (maxwidth && (maxwidth < oimg.width)){
. @' T3 R" W5 M8 @4 h- a( a" n+ Wnormalizedheight = math.floor((oimg.height * maxwidth) / oimg.width);
+ F7 w) {6 V( a2 K; e/ p& ~) {normalizedwidth = maxwidth;, \5 |! Y0 x5 e3 `0 f; j2 J$ f% p
}
; [0 M0 H% S2 aelse {. l; w3 j- B0 q6 s8 V
normalizedwidth = oimg.width;
1 ]9 B" r. B! {; W, O) c! D5 Cnormalizedheight = oimg.height; , Q& f! g2 R3 M  J* J0 @
}7 f" Q% j' r4 ^; U
return { width: normalizedwidth, height: normalizedheight }( q" ?2 X, @2 y0 G$ h
},- w3 G7 c/ d. |& U9 ]
canvas.img.prototype.getoriginalsize = function() {
' P# p- b" A9 \7 T$ [  Breturn { width: this._oelement.width, height: this._oelement.height }
, ~( h* ]/ ~+ b! v};0 u3 Y. P4 Z7 p
canvas.img.prototype._setrandomproperties = function(oconfig) {
) E' v+ G' r- \if (oconfig.angle == null) {- |' t- `( V/ r
this.angle = (math.random() * 90);
" M% G; Q, x  U; |! E. _}& J7 R' r, k( k
if (oconfig.top == null) {
( i5 v' |- z. ?( y- g; Ithis.top = this.height / 2 + math.random() * 450;6 f0 K9 f8 O2 g9 d- m% t
}
4 F% g+ D: V' Y& c$ B5 Tif (oconfig.left == null) {" [! ~# `1 t/ z6 J5 u! g6 P/ ]! u4 n
this.left = this.width / 2 + math.random() * 600;) O0 C- Q- r/ t+ M: p& p
}2 K, f0 A% @/ {
};" w5 g8 o1 `9 }* v; j
canvas.img.prototype.setcornersvisibility = function(visible) {+ ~+ t% Y) c8 x; {
this.cornervisibility = visible;8 f6 N4 R6 g5 Y0 s5 h7 M" I
};
% {8 p& Q# X* y6 a" P4 X2 Hcanvas.img.prototype.setimagecoords = function() {
7 X, ~# G2 M6 C5 V8 c5 J7 kthis.left = parseint(this.left);/ l+ }6 {; \: c  j/ |/ x: X
this.top = parseint(this.top);
7 `9 s3 [0 I3 d+ c! w5 Tthis.currentwidth = parseint(this.width) * this.scalex;
* o8 W3 I- f6 Y7 Rthis.currentheight = parseint(this.height) * this.scalex;
; q/ J" {7 Z8 U5 X/ \% F' |this._hypotenuse = math.sqrt(math.pow(this.currentwidth / 2, 2) + math.pow(this.currentheight / 2, 2));$ Y4 ]( f/ O; R& {
this._angle = math.atan(this.currentheight / this.currentwidth);
# }9 h& j5 _! qvar offsetx = math.cos(this._angle + this.theta) * this._hypotenuse;. c5 b, J  x$ F5 @2 Z
var offsety = math.sin(this._angle + this.theta) * this._hypotenuse;! v" U% O" m* f0 n$ i1 G
var theta = this.theta;% H& w# ^" c* l" B0 {4 ^# h5 Z" e
var sinth = math.sin(theta);
& v& y+ a' x3 t' Q+ dvar costh = math.cos(theta);
2 b% `; V6 ?6 y% dvar tl = {$ _; N! p% e& \7 {
x: this.left - offsetx,% c( G: Y* a! y! O- e
y: this.top - offsety
0 ?! Q, t- T1 I$ [1 c1 r+ _};
+ M$ d) J! d/ ?. dvar tr = {
* ]7 k! }) L7 z; {x: tl.x + (this.currentwidth * costh),
6 C) D" E) w1 [. Xy: tl.y + (this.currentwidth * sinth)  s9 X9 }* _- C1 e
};( {1 V- K  [1 @) ~* D& h
var br = {3 \$ \- ^, z, m2 M9 p- \! A% l
x: tr.x - (this.currentheight * sinth),5 T$ _# q2 s5 w3 L& ?: {
y: tr.y + (this.currentheight * costh)
$ v: @1 F: o. c6 w& o, e( ]};, b, a& f. G, _4 F
var bl = {/ j- J: k- E8 q0 Y
x: tl.x - (this.currentheight * sinth),
$ @2 j3 n0 c+ i8 L/ n7 ?4 L5 i; |y: tl.y + (this.currentheight * costh)
& V& g1 h9 D. j) O/ @/ z4 g};6 x( k0 f+ w- l0 D$ L7 s. M* ?, \
this.ocoords = { tl: tl, tr: tr, br: br, bl: bl };& H) {; n$ x& i$ x
this.setcornercoords();
- r" C! ?4 y5 ~9 w# V. a/ M};
$ X, P  J3 m2 fcanvas.img.prototype.setcornercoords = function() {# `( p3 f) m0 g1 k
var coords = this.ocoords;6 k) ]( s" Z5 [6 }
var theta = this.theta;
9 d% I& A; q6 m! C" H: ]" I8 Ovar cosoffset = this.cornersize * this.scalex * math.cos(theta);7 B! s1 [& R* `
var sinoffset = this.cornersize * this.scalex * math.sin(theta);% o$ d% L( r: `$ @: F1 q9 {
coords.tl.corner = {0 y# ]8 i/ G, F' N2 s) d" N( t% W
tl: {: v" L, ?! `% k5 x
x: coords.tl.x,
6 V/ y, }) e! t! \# n8 oy: coords.tl.y
" T9 x+ V2 |+ Y* ]; a},
; c+ y$ V1 \* m- }tr: {
, `8 y  I3 \: e3 rx: coords.tl.x + cosoffset,
- ~3 S1 a/ j: z8 j5 r+ \& t+ `y: coords.tl.y + sinoffset
! a6 |4 \/ O, O+ |# t},
2 T6 G* `5 p4 n2 q2 Q( p4 Kbl: {: z( u  K( l- K/ f
x: coords.tl.x - sinoffset,
+ K4 M" ^" o$ t4 G) N$ Py: coords.tl.y + cosoffset" ]7 a# {6 I0 U2 b) ~( _7 G2 R
}* @( ^5 N5 h5 g3 V, |
};
. g5 s) U' K" Kcoords.tl.corner.br = {! \' j8 Y. d5 t
x: coords.tl.corner.tr.x - sinoffset,
3 T' d" @& n0 @* }/ U: i7 o$ `y: coords.tl.corner.tr.y + cosoffset
, F2 ?2 _& [+ A: M4 X) E  r9 C};8 z. p$ p# P1 I& S0 h" k
coords.tr.corner = {8 q5 \% Q9 _5 S
tl: {
4 N3 ?# e: w6 M* Qx: coords.tr.x - cosoffset,
' i1 u* t3 U3 B6 U) G) D/ e. M9 ey: coords.tr.y - sinoffset2 V0 F% D4 j8 H  i
},
+ T$ F' N$ |; j$ ]0 q4 `- {tr: {  b! x% ^: ?9 k) U
x: coords.tr.x,3 X. c& K0 o8 K- {' d# ^& n
y: coords.tr.y
& H. G( ~$ z0 k; x0 H},  [/ @+ Y: A- Z5 t$ K
br: {
, o" Y& b6 ]. _1 ax: coords.tr.x - sinoffset,3 H8 ^2 h8 t" _% |+ I4 v
y: coords.tr.y + cosoffset
. g5 Z. o9 C( g) z/ D; R}& a. Q' O+ z* X
};
: c; e& B6 ]/ r6 icoords.tr.corner.bl = {4 J, z! c; A: I. X# l6 j% {
x: coords.tr.corner.tl.x - sinoffset,4 ?# K7 k& P* {/ H2 _6 i# Q* Q% y
y: coords.tr.corner.tl.y + cosoffset
: Z: q9 {. a* F  r( g! a! Z) I1 D};% z. X$ a; W4 R" D' m6 @$ R
coords.bl.corner = {( H3 V, l. j* I5 p+ n* \
tl: {
) G- X4 R5 d% w& w5 p( Wx: coords.bl.x + sinoffset,; r" Z" Q& D8 _5 b, H
y: coords.bl.y - cosoffset) R0 Y; L! f, I& Y  A5 _/ ~9 ^
},
9 |8 S1 P* L" C* j( u  sbl: {5 R3 {! S' Q. Q5 ~! J. S
x: coords.bl.x,
) d& T& p$ R3 s+ U# g- }6 dy: coords.bl.y7 ~3 a8 e6 g! y
},7 [7 ]6 o8 B  v" s( p* w, ^, L" u- ^! d
br: {# B* P, b( Q1 a' h3 J/ N4 ~
x: coords.bl.x + cosoffset,6 T5 y# e+ y! ?
y: coords.bl.y + sinoffset% |5 ^1 Q3 q; H% U4 u9 C( N
}
, K1 T4 G) H+ P' J3 q: V( n8 o};
- F$ b2 C: }# @$ L. n) Scoords.bl.corner.tr = {' [0 T( C; [0 X
x: coords.bl.corner.br.x + sinoffset,7 c0 P/ r: H. H" Z
y: coords.bl.corner.br.y - cosoffset
0 w3 W  x* O8 J};$ B7 s1 e# L6 [/ g6 g7 Z
coords.br.corner = {/ M; j0 ?) W5 Y) N$ I
tr: {9 S6 Z: v# C8 Z7 L) j8 x5 c& P
x: coords.br.x + sinoffset,
. _) J6 d4 |+ F' F2 Ry: coords.br.y - cosoffset
% Z! P% p3 O1 P! L- S; |/ F},
; n5 E8 p, Y  Y* Q0 [4 ]$ Dbl: {
* M5 t7 `' ?# x$ {: Dx: coords.br.x - cosoffset,; p2 f& E' w; _6 w4 K, c+ r9 V
y: coords.br.y - sinoffset
# B3 b1 _5 U* W5 Z0 N% ^+ g},4 {' X  N! F1 C" s0 [
br: {
- m6 v: `) e7 z/ Q' a0 g# _+ O. ox: coords.br.x,: `5 L- u6 ~8 x% Y: S1 O( x! U
y: coords.br.y" ?5 P- a, ~6 o: [
}; b' m, D% e4 _1 @
};+ k" v. t# O4 X3 `
coords.br.corner.tl = {; U* n! a5 p: R  Q9 e& |
x: coords.br.corner.bl.x + sinoffset,
6 Z0 r1 X) {9 J9 cy: coords.br.corner.bl.y - cosoffset
  ~! F3 }' c9 ?# X( E  t};- n! @0 p* v' B2 r! H4 Q
};
9 _' j6 U# N/ `" n5 q3 I}());9 l4 P# n7 l. V, W7 g/ T
return canvas;
: b# K- U( Y7 g});
5 j6 x6 w- M' Epuzzle.html代码如下:6 Q7 f: U( p$ K& z' W3 |4 w
复制代码代码如下:<!doctype html>1 p4 x! r$ ~! M6 j4 R' p; f
<html>
; ^* H  Z; F* Z2 J<head>3 t: ?8 U. ~1 d& h  t+ J7 p
<meta http-equiv=content-type content=text/html; charset=utf-8>
2 m' p- z5 j) t9 h5 z# I<title>insert title here</title>" j2 Q6 H6 V4 s2 }' J. l5 h. B
<link type=text/css href=html5_puzzle.css rel=stylesheet />
4 {" e+ m/ z, @9 X, _<script type=text/javascript src=../multi_upload/seed.js></script>7 ^) @7 g1 _3 Z
<script type=text/javascript src='html5_puzzle.js'></script>
, h( g0 n- b$ D( M6 ~5 D( e: T</head>- E' e  l- K8 a; x% f
<body>
/ d& l# ~3 [! o" i6 D! |5 |  e' v6 N<div id='html5_puzzle'>2 O# |" Q* z' d$ S3 `+ }' H- J) d
<div id='puzzle_left'>) z2 L6 Q/ l: v" J% ]
<div class='puzzle_column'>7 K+ j: ~, g% i+ d& N' |
<ul>/ h9 ^# k; u, c5 E' @. ]+ b% m
<li><img src='small_img/1.jpg' data-index='1' /></li>
# O& m. e+ O: K3 G3 j<li><img src='small_img/2.jpg' data-index='2' /></li>: Y7 m" N) [$ p5 }
<li><img src='small_img/3.jpg' data-index='3' /></li>6 ]" m7 W% ~% k$ C  o" t% n
<li><img src='small_img/4.jpg' data-index='4' /></li>
2 e* Y. H5 t. P<li><img src='small_img/5.jpg' data-index='5' /></li># J* s3 ^, P) f: z: K* B
</ul>
3 E% {5 ~. `- F/ I6 d</div>7 I1 b* ~3 o8 Q0 h& B9 ~; K0 m
<div class='puzzle_column'>9 G  a' S$ c) S8 A% o; g
<ul>
$ Z" |, w* @6 E2 @4 c% x: f9 ]<li><img src='small_img/6.jpg' data-index='6' /></li>
. u$ u4 S7 T- w# y0 t<li><img src='small_img/7.jpg' data-index='7' /></li>
- q6 O3 @3 @" _% B+ }<li><img src='small_img/8.jpg' data-index='8' /></li>
1 `2 o4 j2 y: k* r: I<li><img src='small_img/9.jpg' data-index='9' /></li>+ E0 a' `, M6 O* f5 \5 Z% J: ]: A9 c
<li><img src='small_img/10.jpg' data-index='10' /></li>& P2 |; Q+ o7 v$ K3 z& D% G5 V
</ul># z: a$ E9 v3 e1 d9 r- J: i; P- Y
</div>
5 r! E7 ~0 O& z0 \- r/ N</div>8 S7 S# ^, `$ R/ r
<div id='puzzle_right'>
; D1 x0 u/ \! ^1 M( L/ v<div id='puzzle_canvas'>" K( B. b- k6 E
<canvas id=canvid1></canvas>
9 K! @* R/ C% n8 R% t<div id='canvas_menu'>
; b* U' o' X& I2 L, O6 F: {<a href='javascript:void(0)' id='photo_delete'>删除</a> <a
$ c, ^- z2 S4 x( z- J$ ihref='javascript:void(0)' id='photo_update'>更改图片</a>* g1 a+ u1 b+ R7 V
</div>
0 V( R* V2 `, t! b6 R, w: M, b, u</div>
' }* ]* `; W7 I( t<img id=bg src=big_img/1.jpg width='600' height='450' />
* b/ ~! ~% c# ]% s- y1 T2 V</div>/ V! Z' v' S3 o+ s! T" `2 X* l. W
<div id='puzzle_bottom'>4 v# m) ?! U! N" m5 R8 w+ Z
<a href='javascript:void(0)' id='add_img'><span>添加图片</span><input+ F* t8 h; P( _' R2 r- B/ P
type=file multiple= id='fileimage'> </a> <a
0 X8 g+ C/ a+ G' l7 E2 n8 `  phref='javascript:void(0)' id='upload_btn'>上传</a> <a>点击图片可以旋转,拖拽,% B1 a- R+ `6 z7 K! u: j
缩放哦!</a>+ W3 |; e3 B! b  H0 Q/ o! X
</div>
4 I% H: {" o% e% p# C) z# V</div>
# ~# W# T& H9 b<input type=file id='test'>( [5 k7 W! K4 H  Q# |% J* T
<canvas id='test_canvas'></canvas>
, M: B) u* i6 K1 l. X3 _</body>
0 ?0 r7 p9 Z& j$ r: m</html>+ I/ b' e/ v' d3 [5 @
html5_puzzle.css代码如下:
# C6 W5 m, y+ e  w9 d9 t复制代码代码如下:@charset utf-8;</p> <p>#html5_puzzle {
" C% {2 c, K1 u4 W- x. @1 ~font-size: 0;* a3 j; q8 @+ I; a( j3 G
}</p> <p>canvas {$ V6 z! Z: W* p( s
background-color: transparent;
2 q- {. Z8 I4 c9 x2 }( rleft: 0;4 V* t) |) E5 e8 m
position: absolute;: c/ K& L# b# l# K$ O  E, e4 R
top: 0;
% h0 H6 ]5 Z( o}</p> <p>.puzzle_column,#puzzle_left,#puzzle_right,#add_img {
9 q1 R: }* {" C+ v2 qdisplay: inline-block;- q7 ^) T! J5 k9 |5 Q& C
}</p> <p>.puzzle_column li {
7 Q: f2 E+ S- e" f3 E: ?" Udisplay: block;
6 O* E  M# b9 z6 a$ N; zmargin: 5px;
* @9 h) t5 a' Oborder: 1px solid #ffffff;9 o, f6 \4 I( P; R3 c) a
}</p> <p>.puzzle_column li:hover {; v2 E* i# O8 m$ V0 w! }
border: 1px solid #3b5998;" {! X' E) ^9 i5 l% n( {0 M" B
cursor: pointer;' Z  p4 ^2 j" R1 e* `9 W; V
}</p> <p>.puzzle_column {
! z, G: |. J3 k: ?9 B8 I7 R2 zfont-size: 0;
, m, n0 Y3 O4 N0 e( X  G* x}</p> <p>#puzzle_left,#puzzle_right {% a1 A& P+ |* h8 [% `
border: 1px solid #3b5998;! l( |& r4 }/ X% M
}</p> <p>#puzzle_right,#puzzle_bottom a {
7 p1 X* s# ^5 t& q! h3 a9 M) xfont-size: 14px;. N. ]. e$ ~+ w6 h. G
margin: 10px 0 0 10px;
6 M& T( |! _7 f6 V}</p> <p>#puzzle_bottom {
" W; D% N% _8 F: |- X- U  f" Imargin: 5px 0;, ]' n( K' X0 A. U
}</p> <p>#puzzle_canvas img {8 f- i  }* U2 G" u5 t/ N
}</p> <p>#puzzle_canvas {
1 }5 ^0 c  @2 I) z( |% \6 q6 ]overflow: hidden;
  e# [2 b$ s. j' W( {0 Q$ cwidth: 600px;1 H' |% Z2 f* J
height: 450px;3 B# E( p( x" I0 |. l# J" h/ [
position: relative;
: B) C' ?8 ^4 |1 V! [, A}</p> <p>#add_img input {) y$ ^: r# J: E
position: absolute;
+ c5 v9 l& A) s$ H. a+ R5 s5 Nfont-size: 100px;, u0 g! m1 g- h4 _/ N" b% M
right: 0;
, W' b0 l/ M! J) C- dtop: 0;/ M- ]1 {2 j: G* w% X1 |, K% P
opacity: 0;! v& h/ A0 h! U6 r( ~
}</p> <p>#add_img {$ t# R( N9 u' J& k
position: relative;- B* Z& T+ }' ]! f- |, A
display: inline-block;
) a8 G* }: U, X) O5 \: k+ Y( v% `, |background: #3b5998;" M# p0 R0 r6 U7 V! a
border-radius: 4px;
- F3 y. F5 x7 X  Kpadding: 4px 12px;
$ x1 p- X1 V1 ?, s  boverflow: hidden;
2 N4 o+ I. l5 Wcolor: #ffffff;
+ c# |* O4 p- f* y5 C1 n}</p> <p>#bg,#show_list {% z6 q0 A$ b4 x  ?5 e2 G9 t
display: none;
0 H8 E* {3 ~" w/ ~}</p> <p>#canvas_menu {
- }  ~$ x( Z. U8 m: k4 k4 X, n, uborder: 1px solid red;( x; X% ?' U! {" u4 \
position: absolute;# v: G8 {3 G( E8 e
z-index: 5;
1 L0 i8 u2 I: ^" |: ^9 Htop: 0;) T8 A' `( P/ Z/ i' z( ?
left: 0;
$ Z+ t) X0 W; k/ L1 ]display: none;8 A$ e8 \1 M9 v- v) X3 f% @  K
}</p> <p>#canvas_menu a {
% O" _( _5 y  q; mdisplay: inline-block;
- t6 p/ h# H1 j0 H" h) A" Q}</p> <p>#test_canvas {) ^+ h! V9 N9 Y* w
top: 700px;
- j. a* t  H# w5 g( ?- c- r}. u1 \7 [0 l. `
html5_puzzle.js代码如下:
( Z; n& @' q8 l1 @1 V% {复制代码代码如下:require([ 'img_upload', '../puzzle/canvasimg', '../puzzle/canvaselement' ], function() b/ _9 V8 o: M: y4 `4 z
s, canvasimg, canvaselement) {, s) W* y, C$ w, W
var img=[];5 K0 B! f8 i, D( B' {% W
var canvas = new canvaselement.element();
/ [7 O  H2 o/ t! ]canvas.init('canvid1', {
' P  }2 a/ e6 l; O  mwidth : 600,
- r9 G7 B  q# s: L# Iheight : 450
) L  h5 A% ^3 I% e- @});; Y6 J0 q# j( g0 F
s('.puzzle_column img').on('click',function(e){
! T" K2 B. `" ^$ B2 R% Uvar index=this.getattribute('data-index');- G) O* M* J, i5 ~0 B/ p
$('bg').onload = function() {
' z* `2 d0 W9 X7 R2 q1 |var ctx=$('canvid1-canvas-background').getcontext('2d');& s0 I% U! n% a" e7 s. E
ctx.clearrect(0, 0,600,450);6 V6 _( D% N0 W5 r0 K) n
img[0]=new canvasimg.img($('bg'), {});  D5 Q5 H7 g8 s$ j
canvas.setcanvasbackground(img[0]);4 W! ]  H5 t" j4 x* w! i3 N  t1 e
};
7 @+ `% @: G) n: r% L# `+ F$('bg').setattribute('src','medium_img/'+index+'.jpg');
+ {$ Z; y7 L  qe.stoppropagation();7 L+ j) V) t1 R% s' s+ e
});& ]6 w- _) Y2 G! f6 ^1 p# _; |  i
var canvasdemo = function() {
1 a( `6 z8 X9 K& Ireturn {
! r" U  C/ Z" S' f1 ]+ pinit : function() {
; T: C; C( F5 V5 Wvar img_list=dom.query('#puzzle_canvas img');2 K, {+ C6 t: J) l6 P0 p1 K' C' M
img[0]=new canvasimg.img($('bg'), {});
1 u' P% R/ y2 ~* ys.each(img_list,function(i,el){+ ^+ J, I$ h, [( c( E
el.setattribute('data-index',i);
& _9 r6 J8 \! r+ N: P- O; T" S3 s' ximg.push(new canvasimg.img(el, {}));
! ~9 `/ a2 X2 ^7 T4 d2 @canvas.addimage(img[i+1]);# o. J6 |$ P6 n
});
9 A" ^0 N! C. g$ m/ ^+ @canvas.setcanvasbackground(img[0]);5 d  D0 `' D% m* \- e8 R1 @# w
this.cornersvisible = (this.cornersvisible) ? false : true;9 Y1 d, B0 ]* F$ ~5 j4 b
this.modifyimages(function(image) {
& r$ g4 c) P- ]0 }% S7 ~image.setcornersvisibility(this.cornersvisible);- k9 d/ R/ S7 O
});
% j* h7 |) |; \2 [  C},
1 O* C# g4 H- x3 pmodifyimages : function(fn) {
( u$ o1 B$ j' P. _9 p( ofor ( var i =0, l = canvas._aimages.length; i < l; i += 1) {
4 [3 H% `9 c" l, N% c% cfn.call(this, canvas._aimages);+ ?4 o- E0 T9 |
}
: D" y  A! k9 E# I0 ^, ^! s$ zcanvas.renderall(false,false);) G. {. S" w% b1 p$ l" C7 A
s('#puzzle_canvas img').remove();+ B% U4 z, w& O) V! w8 M* A
img = [];
4 s" v1 a/ {2 X. g" U+ i: F}
, z' j% g1 X+ x# j5 s* ~0 F};
& f, d! u. }  _0 l2 X}();
! f/ M5 W* U7 H  Xfunction getcurimg(){
* y% v: l, M% B2 E% b6 Nvar oimg=canvas._prevtransform.target;- Q% A9 ~0 C1 |5 B/ w, V
for(var i=0;i<canvas._aimages.length;i++){5 S, z& ]  A1 N' p, x9 d' O. E! c- i
if(canvas._aimages._oelement.src==oimg._oelement.src){" F( @, E  u4 u; p
return i;: G. m% e* A( I5 {
}' l8 g" r. T3 k. P7 ^% `/ U
}
  W+ `5 ]9 H" E- ?1 V}
; m. e) d; R  y+ ~s('#photo_delete').on('click',function(e){& n$ ^) h  D0 V! z
var i=getcurimg();
$ K1 x$ e' i  b1 [) A) T8 D: ncanvas._aimages.splice(i,1);/ ^! P- C" Q$ `* `% j7 o
canvas.renderall(true,true);) r2 F9 a! m3 Z9 Q# q( O/ l
$('canvas_menu').style.display=none;
& u8 F7 R% L. ]# B2 g, B! R});5 k; K8 m; ~2 ~! Z: d, b5 A
s('#photo_update').on('click',function(e){* \' z' Z+ f3 L* p7 q+ M
$('test').click();
* ^, U7 D) j& J9 O" m});
  m6 O- b  c5 j( ]% y. \' ]s('#test').on('change',function(e){
! u) @( \& P+ \7 p9 U; B( v, U: }var files = e.target.files || e.datatransfer.files;& y+ @0 o( H& `5 `
var reader = new filereader();
6 a7 S& M6 w$ q* @# B: sreader.onload = (function() {: w. B% L0 D' ~1 h' c
return function(e) {& Y( I" ]  k/ k+ L' m; E! _
var dataurl = e.target.result, canvas1 = document.queryselector('#test_canvas'), ctx = canvas1.getcontext('2d'), img = new image();& \1 O/ Q& Z! G) F2 d: k
img.onload = function(e) {8 a  A' d, u' h1 n; r8 g: L+ B
if(img.width>200||img.height>200){# D* W- k4 C) Q- f4 R; {5 k8 v1 b+ e
var prop=math.min(200/img.width,200/img.height);7 f' h, t0 z3 X3 e+ C$ f0 @
img.width=img.width*prop;
  s3 C! H1 Y- H) ]+ {img.height=img.height*prop;
: y1 G+ m# M7 n- U! p; p}
3 C: |6 ^) R6 @9 @" t# z5 F5 ]" acanvas1.width=img.width;; \- Y" f6 H% \" d+ N* w# F
canvas1.height=img.height;8 M5 ]& w9 w2 K$ h- z2 l9 H
ctx.drawimage(img, 0, 0, img.width, img.height);
: j" c+ v8 ^# ?! {4 y" gs('#canvid1').html(s('#canvid1').html()+<img src='+canvas1.todataurl(image/jpeg)+'/>);9 `. v; h/ q2 B' {( @) a
var t = window.settimeout(function() {
5 t8 }5 W! _* F3 M& \var i=getcurimg(),target=canvas._prevtransform.target;# F" E6 K3 [5 h" s$ n/ ]' R
console.log(target);+ a9 V  t5 R1 |9 ]2 J% K: f
canvas._aimages=new canvasimg.img(dom.query('#canvid1 img')[0], {
: r4 Y" {) l# ttop:target.top,8 f" p/ [4 ^! {& s1 L
left:target.left,9 L6 _, B( r1 D3 Z3 _
scalex:target.scalex,
$ ?9 a! F0 o$ N4 V$ J7 K" tscaley:target.scaley,3 \; u: A- s! J) O+ ?
angle:canvas.curangle* Y- M5 B" F# g; Y* W7 |
});% a+ F% W: D* z- Q
canvas.rendertop();
: K% g( p8 K* z+ ]5 e! I2 _cleartimeout(t);
) f7 B: |. l# e. j. p2 g$ Ms('#canvid1 img').remove();
% P( K5 ?2 c/ D+ }; ]; `2 ^9 [9 q8 K},1000);6 Q3 u6 N! P5 O
};
# o" ]9 i1 X* timg.src = dataurl;2 w5 i1 M% T! j- H
};, z% F% o: `; R0 w: ~  z' |
})();+ I9 B5 [: L: I: m
reader.readasdataurl(files[0]);9 I* P1 |% ^2 L5 E6 `
});' @2 q9 d& r9 {
s('#upload_btn').on('click',function(){
2 u+ f9 @4 ?: A. ]var imgdata = canvas.canvasto('jpeg');! L  y0 S4 Y. y' s% P5 ]
var imgvalue = imgdata.substr(22);
$ E- q$ Z; E/ C) Fs.ajax({9 e. ?7 \. Y6 p5 Q3 @* t$ j9 H
url : 'http://localhost/html5/upload1.php',
2 _# I: G# k1 ^, [- ]' e0 ?type : 'post',5 Y: A. m! B" ^3 Z( H8 C
data : {# |/ r, u8 o0 c8 P
imgdata : imgvalue,7 G! t  M+ L, S2 P7 D( }& \+ N- m5 w
file_name :'mix_img.jpeg'" t8 U" j7 d+ H; C) o
},& @) f7 U, T+ ~
datatype : 'text',$ g( r0 W0 K2 x8 E# K. [9 b) t
success : function(data) {4 [7 i& Q5 M+ W9 e4 Y  j( }! M% \4 V
alert(s);# P) H) ^4 G, r; T& f" \
}
( b: w, g) g' w3 p+ G; r});0 {- u, V) J  x* b
});2 }. u3 R1 i6 o7 u% M
});
* c9 U6 _0 k0 j& R至于用html5 input读取图片,这很简单就不贴出来了。: }8 v' i) b4 @9 _
0 ?8 [9 ?2 Q+ ]. m$ R, s. ^. g
更多网页制作信息请查看: 网页制作

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