本文实例讲述了html5版canvas自由拼图的实现方法。分享给大家供大家参考。具体方法如下:4 a( ]# K$ W) v" u6 z6 I( z+ h7 {: K) f" W
代码运行效果如下图所示:
& ^+ r9 ~4 Z/ T& U& K& g7 @/ {9 r/ m" M0 f) Z0 k1 R
canvaselement.js代码如下:
3 I# \; o- A! s复制代码代码如下:define('canvaselement', [ '../multi_upload/core' ], function(s) {* K8 s7 D( A; K9 q
var canvas = window.canvas || {};6 V9 p' `$ L. {6 U: [: k9 h
(function () {* K8 Z P8 ?/ }/ a1 H# }; `
canvas.element = function() {};
9 b3 X) k6 F' c2 |' a1 e& xcanvas.element.prototype.fillbackground = true;$ j1 b) X5 }' M2 @: C) G! y
canvas.element.prototype.showcorners = false;1 ]* j; P' h0 z/ Y, \% ^
canvas.element.prototype.photoborder = true;. B3 ~; S- x+ ]' h' Z
canvas.element.prototype.polaroid = false;
- j# R4 }$ {$ w! r b3 rcanvas.element.prototype._backgroundimg = null;# L6 _9 x7 s! C( F! c
canvas.element.prototype._groupselector = null;8 ^3 B: g6 ?2 @- j9 b
canvas.element.prototype._aimages = null;
! F/ `3 b& Y8 j7 E) x3 Xcanvas.element.prototype._ocontext = null;
$ P: S) K* K4 ]$ }& R% p% Z" Xcanvas.element.prototype._oelement = null;3 x- d) s. L/ `/ ^$ u/ X
canvas.element.prototype._oconfig = null;% `3 A; U# ?" |, @
canvas.element.prototype._currenttransform = null;% W2 ?$ s" c4 X) ^& y
canvas.element.prototype._prevtransform = null;
1 @! p+ O4 a* y6 O9 ocanvas.element.prototype.curangle = null;( i& O2 W# o$ O' y8 a7 m i
canvas.element.prototype.init = function(el, oconfig) {/ `* C4 v0 \7 g& W- n
if (el == '') {
/ G/ L' R1 g5 k3 }2 V6 d2 Creturn;4 j' S2 P5 _' A( F
}$ }" T% w2 z7 }2 L4 Q- A. t( @
this._initelement(el);
' r7 k, ^+ Y' v p: ]( othis._initconfig(oconfig);
( y$ B# o1 y% w: @+ d% F7 Nthis._createcanvasbackground();, A: @. T5 s8 K/ S7 f1 M
this._createcontainer();
- x" E2 ]+ i/ V% ethis._initevents();
: Q1 k& B* y, uthis._initcustomevents();# W+ d3 {/ @! [
};& c, i0 ?$ ~( |$ K+ p
canvas.element.prototype._initelement = function(el) {; n. H; c$ g. [/ u9 i9 Y
this._oelement = document.getelementbyid(el);
$ @( V1 S a1 X4 p. r% q2 B$ [this._ocontexttop = this._oelement.getcontext('2d');
5 O. N% e ?' _$ ~- a' d. g* G};$ U0 [+ V( z6 A K$ J; P c7 e
canvas.element.prototype._initcustomevents = function() {' o! ~$ ~2 v# c
this.onrotatestart = new canvas.customevent('onrotatestart');. ]5 V* L! q* [( z
this.onrotatemove = new canvas.customevent('onrotatemove');
6 h: ~; O* i: q O+ qthis.onrotatecomplete = new canvas.customevent('onrotatecomplete');
3 N/ d! k) ^( i, \0 G9 N0 e# K6 Xthis.ondragstart = new canvas.customevent('ondragstart'); & [1 a9 L+ Y& N
this.ondragmove = new canvas.customevent('ondragmove');" m6 ^$ m o6 V& v3 [0 M
this.ondragcomplete = new canvas.customevent('ondragcomplete');
- f7 }7 e2 C6 J/ h& L};* e3 h8 r: t, O( y
canvas.element.prototype._initconfig = function(oconfig) {
) `7 D" V1 B; T4 J* wthis._oconfig = oconfig;
7 \7 Y" a( P# b9 }) i1 A' Nthis._oelement.width = this._oconfig.width;
) M1 S9 l* m+ z1 u3 nthis._oelement.height = this._oconfig.height;
$ n9 y4 \: o$ ]8 k" }- tthis._oelement.style.width = this._oconfig.width + 'px'; R, Z* e d" _1 M' a$ _: ]9 k
this._oelement.style.height = this._oconfig.height + 'px';
& U" J8 ]* r% W' ?8 j};% x% B+ f- N- A% w Q
canvas.element.prototype._initevents = function() {1 ?$ s, z2 D+ r$ w9 ]
var _this=this;. k: A" u$ i7 W& t8 S
s(this._oelement).on('mousedown',function(e){6 Z: U5 z0 U" l1 B- V3 J
_this.onmousedown(e); C }0 ^7 P4 u7 F
});
9 Q. q, a' d. W6 s, \+ ns(this._oelement).on( 'mouseup', function(e){
3 F% j' k0 m! f4 r_this.onmouseup(e);9 T0 |1 K' V4 ~6 g8 ?3 @2 r
});1 A( e2 Q7 x1 P0 }
s(this._oelement).on('mousemove', function(e){
t( l* ^3 ?& E* G2 j8 ?" ~1 u_this.onmousemove(e);( m6 Y j" K9 g
});
5 }( G0 E" g) z7 r7 L. e};
5 l7 ?0 s3 N3 m1 x. Q9 z% Hcanvas.element.prototype._createcontainer = function() {
* A2 e$ K+ U$ A3 w) _" k4 Ovar canvasel = document.createelement('canvas'); V5 |/ w5 G& h- b* d- j5 ?
canvasel.id = this._oelement.id + '-canvas-container';$ @ K# M6 a* J# e
var ocontainer = this._oelement.parentnode.insertbefore(canvasel, this._oelement);
) S; t4 P0 ~( K6 g# N Focontainer.width = this._oconfig.width; O9 _1 c8 h+ t) O& S
ocontainer.height = this._oconfig.height;
4 w4 S8 F* C. ^' }0 |ocontainer.style.width = this._oconfig.width + 'px';& G, P# V$ y1 T& E# \, H+ ~
ocontainer.style.height = this._oconfig.height + 'px';
+ k' ?' P) T; Y, ~this._ocontextcontainer = ocontainer.getcontext('2d'); 2 {" {2 A2 Q+ u9 X& ~0 A; F
};% p6 Z/ Z* i- S+ c7 g
canvas.element.prototype._createcanvasbackground = function() {
% U" J) _, I5 }$ t2 Q* K% f' Qvar canvasel = document.createelement('canvas');" C( w% J7 P# g6 a: D
canvasel.id = this._oelement.id + '-canvas-background';
; W: A: v( @9 ]/ ?2 Yvar obackground = this._oelement.parentnode.insertbefore(canvasel, this._oelement);3 J4 E- L4 U; E& L$ W
obackground.width = this._oconfig.width;
9 L( e5 C0 L; |obackground.height = this._oconfig.height;) {( v) _8 ^$ Z& S% Y! E; X
obackground.style.width = this._oconfig.width + 'px';
: Y) u( o( y/ Q, }obackground.style.height = this._oconfig.height + 'px';! `9 i" S E0 i- a' g0 Y( z
this._ocontextbackground = obackground.getcontext('2d'); 9 i# ?: J/ e' t( n$ [
};
# H9 N% O# f' l# E) ncanvas.element.prototype.setcanvasbackground = function(oimg) {$ i8 k7 J* `, k# x5 m. l6 m; u$ U
this._backgroundimg = oimg;& S3 { V' K* R8 p4 p+ V
var originalimgsize = oimg.getoriginalsize();8 K* i8 G n' Z$ u4 E% |% d
this._ocontextbackground.drawimage(oimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);/ ?* ^4 i1 z3 r7 L+ |3 E! t
};! N# Z a( y" p& t' g
canvas.element.prototype.onmouseup = function(e) {) F5 S( X4 ^" Z6 o. H
if (this._aimages == null) {
# Q/ A7 |/ E4 c5 T7 u4 G" qreturn;/ U4 v( H& Q3 y ?6 C3 I
}* F& v H; s& h. x
if (this._currenttransform) {7 P; z3 F& w" m9 G5 ^0 ~
this._currenttransform.target.setimagecoords();
, {( o% i( }; k; o: P}
- Y" \8 ]' H6 X! oif (this._currenttransform != null && this._currenttransform.action == rotate) {
. W4 O! T6 ]- j" e+ a& tthis.onrotatecomplete.fire(e);% e9 ?* t6 a9 k, K' v
} else if (this._currenttransform != null && this._currenttransform.action == drag) {
+ G5 y3 H! T* A5 b1 e/ _this.ondragcomplete.fire(e);5 E$ i2 b3 l: y. M8 A1 J
}
& f; ^: N$ R: @% R+ X, H5 ]this._currenttransform = null;! ]* P/ ?0 N6 o2 d' I
this._groupselector = null;
! z/ P/ B/ g' m- Z: @' [5 ?4 H: Pthis.rendertop();
% O$ {" ]3 g2 n; j/ \1 g, H* M};
. F3 {- ^2 l& n n2 [canvas.element.prototype.onmousedown = function(e) {
* S8 K9 A+ ?( X, _) zvar mp = this.findmouseposition(e);. c8 U& `! |7 X: r, x5 j
if (this._currenttransform != null || this._aimages == null) {
8 I+ ]& P3 n2 U8 M6 x( Dreturn;
# @6 t( U" w! [5 V9 s/ F}4 X4 V- A' W" _1 U$ m, @0 J0 u4 y
var oimg = this.findtargetimage(mp, false);
& J& F! [! ], X% f- ]& cif (!oimg) {7 u/ U( H# r& j3 A4 C7 o
this._groupselector = { ex: mp.ex, ey: mp.ey," C# K. ~4 }( J i( @1 F5 P/ G0 r
top: 0, left: 0 };$ W, j- Z' d7 |6 @3 l3 ?+ s Q' H
}
) i% N, d' p* ]! C: u) _, welse { * D @1 T7 ~% G$ r
var action = (!this.findtargetcorner(mp, oimg)) ? 'drag' : 'rotate';: Y; ~! m; J8 n" y+ C# \
if (action == rotate) {
- i, M! @5 j3 S+ M* O) sthis.onrotatemove.fire(e);
4 i. D9 r$ L* T3 T& ?} else if (action == drag) {' h+ n' o- O9 E* I8 O/ h: l
this.ondragmove.fire(e);- Y! P: O6 b4 h: x% F3 l, Y* a( a
}0 d& [# p6 j. K; q
this._prevtransform=this._currenttransform = { 6 Z5 C0 D1 M) A2 @( N
target: oimg,
/ F9 a- R) Y8 M9 s2 q; maction: action,
' a# s4 o* R0 G. q/ U9 D' Mscalex: oimg.scalex,
# O+ [- \8 J* U5 u/ T6 K* p6 N. Zoffsetx: mp.ex - oimg.left, f, K; s( C* u l4 ^0 P
offsety: mp.ey - oimg.top,
, M4 E2 u4 S. T8 l' eex: mp.ex, ey: mp.ey," _9 E! A' g- x! l8 q) }
left: oimg.left, top: oimg.top,
8 C6 i' r- ?' T5 W/ {; A6 E: s9 ntheta: oimg.theta W# U0 k; E$ q7 L3 L, V9 R
};
- v9 T4 B0 R" ]4 h: o/ v$('canvas_menu').style.transform='rotate('+oimg.theta*180/3.14+'deg)';# X0 {5 g( D! S6 | T8 u+ N
$('canvas_menu').style.left=oimg.left+px;2 `6 b) i5 u \; ` e
$('canvas_menu').style.top=oimg.top+px;
/ E2 \# A& [) w- H% j/ ^; R7 p3 Z$('canvas_menu').style.display=block;$ X+ m( B, v* E( ~
this.renderall(false,false);7 [, ~0 w; j" ^% K- K. {
}
* ]: ~* N% Y3 v/ y2 Z- a};, x8 u* K8 ]1 e S7 i. p7 h6 E8 ?7 J
canvas.element.prototype.onmousemove = function(e) {5 J" U7 v4 ?3 P! \! y6 v
var mp = this.findmouseposition(e);
f% j5 `) ^/ X% c4 Dif (this._aimages == null) {: U9 Y% V3 w4 U8 [1 a
return;
% r! X. A7 _" t}) m) p( F( y/ T8 H1 A/ W
if (this._groupselector != null) {# V! A& O& K5 ]
this._groupselector.left = mp.ex - this._groupselector.ex;" h6 Z, F; G# ^, N) z) V0 P- p) K
this._groupselector.top = mp.ey - this._groupselector.ey;' ~3 P+ i, H2 R9 s9 d) D }/ q
this.rendertop();
1 M; n) R8 {: w3 `5 R+ R/ K}
% N* |$ `7 I" V/ Q4 D. e" Z1 Delse if (this._currenttransform == null) {$ M- Q! P! C2 I
var targetimg = this.findtargetimage(mp, true);. `( o# c5 ]- L' s, E
this.setcursor(mp, targetimg);
* @! q: _0 H ^/ ]0 |: o}
$ q0 `9 v/ D. d2 z3 j1 C3 Q3 Belse {
# n$ ? b! V# Xif (this._currenttransform.action == 'rotate') {- S3 j0 V4 P6 z7 Z! E$ c4 Y
this.rotateimage(mp);
7 T! l9 c9 Z( xthis.scaleimage(mp);
4 F7 z0 c, t& e% m1 tthis.onrotatemove.fire(e);
0 P5 |; S) G9 t) h: y! Z} ! E6 V* }8 P+ `* G
else {
& Z1 B' |) |( K* i) M' K( T. B5 Pthis.translateimage(mp);
4 D8 f! R2 X6 r: k9 I. Z! fthis.ondragmove.fire(e);2 [( _/ k1 K6 U8 \5 L
}9 ]' s4 a i9 @5 Z$ h5 Q( I
this.rendertop();
: f! f8 ]+ @/ U8 x} + q. Z8 T5 M, x, M6 ?
};" p6 `9 X+ H" Q& O8 |
canvas.element.prototype.translateimage = function(mp) {
5 ?, ^# \8 ?5 y+ u# Nthis._currenttransform.target.left = mp.ex - this._currenttransform.offsetx;. y% o. C; ~. Q5 d3 R
this._currenttransform.target.top = mp.ey - this._currenttransform.offsety;1 Y& y* }* O. `# k, f6 B: N% |* N9 ?
$('canvas_menu').style.left=this._currenttransform.target.left+px;
9 p" f0 Y% M% T. B$('canvas_menu').style.top=this._currenttransform.target.top +px;
% w7 C: \" x% i};
E$ I' \. n0 h9 Bcanvas.element.prototype.scaleimage = function(mp) {* F7 F: O+ T6 ^$ V! ?4 _
var lastlen =
& x4 w( x* d% `* ]/ B7 \3 qmath.sqrt(math.pow(this._currenttransform.ey - this._currenttransform.top, 2) +
8 q+ I3 d- X$ Qmath.pow(this._currenttransform.ex - this._currenttransform.left, 2));9 q, `( I2 c9 ?- \! z6 @) S
var curlen =
8 V+ o: q; N$ s9 rmath.sqrt(math.pow(mp.ey - this._currenttransform.top, 2) +
/ L- m$ o% M5 c1 |math.pow(mp.ex - this._currenttransform.left, 2));
: ?1 s6 I2 L" b% V2 Mvar curscalex= this._currenttransform.scalex * (curlen / lastlen);# E8 @: l# T0 I: `* }; F6 }
var curscaley=this._currenttransform.target.scalex;$ s; H8 I, i' o: [: t# T
if(curscalex>0.7&&curscaley>0.7){
( |6 l P( S( K9 Ethis._currenttransform.target.scalex =curscalex;
9 `+ F. j, i& Q: Y) Pthis._currenttransform.target.scaley = curscaley;% E3 e8 q' R0 E" O
}
; y1 ]0 W4 |: U8 ]5 v};$ G% b& o$ J# y4 C! k+ G# A
canvas.element.prototype.rotateimage = function(mp) {
% X& {, M. _& c" xvar lastangle = math.atan2(: X7 f7 R2 _5 r
this._currenttransform.ey - this._currenttransform.top,
) L y' I& t5 u6 k4 n: h% Lthis._currenttransform.ex - this._currenttransform.left2 K! n( Q; \8 z7 R% ^$ \. z2 n) ]
);2 p4 Z6 P; n( j) z1 J8 P" A8 t' W( j( ]
var curangle = math.atan2(
. [5 z5 J+ H& H/ kmp.ey - this._currenttransform.top,/ q Y' M& Y' ]3 Y; g
mp.ex - this._currenttransform.left
; |" V; t7 ]8 A2 e. @9 I);; L! n" h* B3 w/ V# Q
this._currenttransform.target.theta = (curangle - lastangle) + this._currenttransform.theta;$ T | F% s. K3 v/ H
this.curangle=this._currenttransform.target.theta*180/3.14;
! g; S5 J- u7 H' {% E9 n$('canvas_menu').style.transform='rotate('+this.curangle+'deg)';
) c! S6 m! Y) L};
+ |6 A3 Y8 f/ M1 C; ?3 i/ ]* Zcanvas.element.prototype.setcursor = function(mp, targetimg) {
8 X h' B3 c3 v, `- P5 Cif (!targetimg) {2 T' D# g/ D. ~' [) f" Z
this._oelement.style.cursor = 'default';9 G( _5 \5 J# Q6 g+ s& y0 l
}
( G/ j- i' |! ielse {
6 h8 Y8 ?3 d' `- _var corner = this.findtargetcorner(mp, targetimg);7 E d g$ S7 o
if (!corner)
: Q1 n7 e' h6 |3 w0 |{6 G* _6 Y2 a$ {$ f% ?
this._oelement.style.cursor = 'default';5 t- s2 v0 J! p; W: v
}) x5 t! Y& x; S' N, B: C
else
0 I# h x5 z1 \+ [- ]. H{
5 v+ @# q% l2 ? l3 x) Hif(corner == 'tr') {
3 O9 q" Z: W8 dthis._oelement.style.cursor = 'ne-resize'; _/ J2 N. k0 j! V2 E/ j# _
}
B$ w0 Y$ L) j$ z. \& j1 }. `2 {else if(corner == 'br') {
% U% h5 V0 v, l4 U' i `9 fthis._oelement.style.cursor = 'se-resize';
+ x( R4 z) V% u4 O' S}
- e$ n0 C5 @/ B) Kelse if(corner == 'bl') {+ A6 A: ]4 f+ r9 B7 m
this._oelement.style.cursor = 'sw-resize';/ b2 @. f3 j- C2 w
}* Y$ V9 A2 {* W/ ^* [/ T) a
else if(corner == 'tl') {" g2 E0 U* G+ w' W, N [
this._oelement.style.cursor = 'nw-resize';
* C* m* R& S) w6 K}
- Z: K7 U, N! k% melse {
$ v4 ?) P$ e$ Q; J6 F- L8 X3 d- Q2 Cthis._oelement.style.cursor = 'default';
! R: j% I l7 z}2 A2 l! h+ {3 D& Y0 ]5 Z
}8 I. x7 f. Y9 n4 ^
}
' \3 W" _/ B2 b+ r- s8 J! S};
( J* E' W: n. R# M# g+ K8 Rcanvas.element.prototype.addimage = function(oimg) {
, M" d/ Z" g* cif(s.isemptyobject(this._aimages)) {8 ?/ t; x# ~9 c$ [
this._aimages = [];
. ]- w0 C# A1 }- o" b}
5 ^- |7 S5 \! p" H- j6 kthis._aimages.push(oimg);0 |3 K9 l: C+ q# I& Z* C
this.renderall(false,true);</p> <p> };9 x: A+ }( Z+ X: c
canvas.element.prototype.renderall = function(allontop,allowcorners) {
7 D n2 N W4 V1 T7 Gvar containercanvas = (allontop) ? this._ocontexttop : this._ocontextcontainer;
$ _% U ]- @: Gthis._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));9 I3 c) e0 W8 `; A7 f7 t+ V3 c/ H
containercanvas.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
5 c( z$ ?/ `0 k5 X' Cif (allontop) {4 ~4 d3 N( P; I+ u( b
var originalimgsize = this._backgroundimg.getoriginalsize();
. s( I! O& l& S- ^; j7 Rthis._ocontexttop.drawimage(this._backgroundimg._oelement, 0, 0, originalimgsize.width, originalimgsize.height);
- J% r7 k5 Y b* ^, K9 j( @1 [}; p+ H, o: \- t" N c
for (var i = 0, l = this._aimages.length-1; i < l; i += 1) {, f6 H8 `* i5 c! f, y
this.drawimageelement(containercanvas, this._aimages,allowcorners);
/ G! A- H6 F( ~; P; p% t, B}9 ~; ?. y- J& L9 a$ [& {
this.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],allowcorners);
3 r% O9 J, D5 P3 }% Z* c! c: K6 e3 p}; {8 h7 u; p/ {6 r9 Q, X" l% J5 g
canvas.element.prototype.rendertop = function() {* j* P8 n1 v% I, w8 d* G
this._ocontexttop.clearrect(0,0,parseint(this._oconfig.width), parseint(this._oconfig.height));
, F- A. M0 t) ?; n* bthis.drawimageelement(this._ocontexttop, this._aimages[this._aimages.length-1],true);
& C* ?8 X9 F' J- y. ~/ Aif (this._groupselector != null) {5 ^# G0 |# X7 ~/ E, {9 u7 h; L
this._ocontexttop.fillstyle = rgba(0, 0, 200, 0.5);
9 V* N% n! \( l2 ^/ T0 C J- pthis._ocontexttop.fillrect( @/ K$ E" j7 X5 O$ f6 X' d1 [
this._groupselector.ex - ((this._groupselector.left > 0) ?
! H9 u8 X2 h8 m% Z$ y/ L( `3 p; U0 : - this._groupselector.left),
6 t, G( ]! U( W: L3 E8 Zthis._groupselector.ey - ((this._groupselector.top > 0) ?
: e3 T& S$ c4 C0 : - this._groupselector.top),
% W+ }! P& m& n; u1 n; T8 L$ ]2 Imath.abs(this._groupselector.left),
& k1 a+ v6 x' \& jmath.abs(this._groupselector.top)4 N, u- Z5 W4 W, j5 r, h; t6 k! a
);8 b+ |& g, ^/ H8 `
this._ocontexttop.strokerect(
1 v# a# ^( X8 x- Ethis._groupselector.ex - ((this._groupselector.left > 0) ?
3 f6 L0 B. s5 l: F" w8 o$ f5 K" L0 : math.abs(this._groupselector.left)), 2 L' \6 q% U' b" _
this._groupselector.ey - ((this._groupselector.top > 0) ? 1 d; k5 D0 d8 ?2 `: t
0 : math.abs(this._groupselector.top)), d, ? t$ ?4 u" Z4 p* C- O: Z! t
math.abs(this._groupselector.left), 5 b2 }7 h. O) A$ Y1 s
math.abs(this._groupselector.top)( r) H8 k1 ~6 B' ]! q/ o' C3 b
);) t" V) N/ P) }0 d @4 b) N: n0 e
}
0 V! d/ r2 k( X9 H4 E+ ~};
6 r9 D; ~4 {8 R. k3 m& ?6 b" Bcanvas.element.prototype.drawimageelement = function(context, oimg,allowcorners) {7 ~* J) I$ h* o& D$ l
oimg.cornervisibility=allowcorners;. k. r( K2 n$ J# B9 p
var offsety = oimg.height / 2;
% F6 g' ^" {8 M( e% q. O2 z/ S/ z& ~var offsetx = oimg.width / 2;
. Q+ Q ~) }8 x: D! ~5 t2 f1 ccontext.save();
( G+ F" f: T7 i3 hcontext.translate(oimg.left, oimg.top);
' Q& j U, W3 e) Xcontext.rotate(oimg.theta);( N: z8 M, e$ p" q. N F& g
context.scale(oimg.scalex, oimg.scaley);- M) a0 n; h/ I( X
this.drawborder(context, oimg, offsetx, offsety);
/ G) e8 B r$ G. I: G1 n4 Pvar originalimgsize = oimg.getoriginalsize();! x1 o" ?+ z/ N- X3 s6 I
var polaroidheight = ((oimg.height - originalimgsize.height) - (oimg.width - originalimgsize.width))/2;
6 a! b6 n" `$ a- z% G1 @$ x: o& \1 Tcontext.drawimage(8 Q( c! [1 @4 o$ T# Z8 t
oimg._oelement,
5 k i, y1 ^. {; H- originalimgsize.width/2, ; K3 W% e, S- n1 R0 a T4 Z
((- originalimgsize.height)/2 - polaroidheight),
+ Q0 T! G# N/ w0 g, E4 z3 loriginalimgsize.width, # T+ r2 E# ], V( z! n0 K2 @
originalimgsize.height
5 T3 \& ]) w9 V: X# T5 c# q); \" T) J) G$ }) j
if (oimg.cornervisibility) {3 Y( ~& [3 L' I; P+ ^# p2 l
this.drawcorners(context, oimg, offsetx, offsety);
; `: y6 P& z; p" M, }; Z$ Q}
% a U1 s4 l2 Z# B2 Q; kcontext.restore();
5 p3 a+ M! ^$ n8 O1 {};
! t3 N) b- D3 @canvas.element.prototype._getimagelines = function(ocoords) {
9 s1 ~" ?' t+ O. e3 hreturn {, N9 \2 d G( X6 k
topline: {
) ]( D2 m& v$ A: x" S' Eo: ocoords.tl,
8 w! O! a5 _, L1 pd: ocoords.tr
( F; Z; _0 X! a5 P},
3 s8 w5 B, v) ~) }rightline: {
: D; a$ L$ U2 @o: ocoords.tr,
( I4 F' Q" R t+ ^5 U9 E* Pd: ocoords.br / b: d/ y' U" O S2 J
},0 \& f/ H. J$ Q* M! R
bottomline: {
9 m# ?1 B* |+ r/ o! o3 uo: ocoords.br,0 a' m0 f* S& b s
d: ocoords.bl
3 ^, {9 S5 O( q8 V0 q}, m2 Z- v5 ^7 k. O
leftline: {
- f8 R4 x/ e" D; |o: ocoords.bl, m% W2 r' V0 P8 N, G4 S5 g4 ]
d: ocoords.tl
4 b) v' Z4 J7 H4 T4 o}
$ _' I: O3 k3 G+ M};
4 ?& Y9 L" _( u% x$ `; ]};* ^" Z. W7 t6 [% k
canvas.element.prototype.findtargetimage = function(mp, hovering) {
. b* g5 }% W2 v0 v0 |for (var i = this._aimages.length-1; i >= 0; i -= 1) {$ k$ X1 d1 q3 R! z
var ilines = this._getimagelines(this._aimages.ocoords); P4 J9 j4 b" W h5 p) B
var xpoints = this._findcrosspoints(mp, ilines);
4 G5 b: x/ ?' c8 N2 C- Q; t- I5 X0 r" \if (xpoints % 2 == 1 && xpoints != 0) {
/ Q+ ~/ E$ Y( h% l+ E& L& |) Dvar target = this._aimages;6 j) j8 N9 |/ @0 v* u
if (!hovering) {! c/ H: ^ P: P* S7 J
this._aimages.splice(i, 1);, T' ]1 A1 F/ C* u6 [: {- L
this._aimages.push(target);
2 v% C0 ^* f; I- Z8 g}& |2 n4 X( w E6 m' w0 `7 @
return target;
( _0 y" H7 \: R4 L3 ]3 D}
' ~; {% D- I) m" t9 F7 T( B}' ^0 t& P; t& Y* z! m0 h% j+ b
return false;
$ y- a6 X; \( C. t, o};
4 h! c4 l3 O8 d$ a$ h x5 E( dcanvas.element.prototype._findcrosspoints = function(mp, ocoords) {4 E4 A. T7 D8 `" T( y0 N
var b1, b2, a1, a2, xi, yi;
2 z0 \9 o( y Tvar xcount = 0;
( Z" Q9 Q) O; evar iline = null;
% T3 L4 z& E, x9 t4 |6 }9 {; i) Ffor (linekey in ocoords) {
5 f' e/ M9 I# j* Diline = ocoords[linekey];. k' r% _4 k7 i. J, _; _
if ((iline.o.y < mp.ey) && (iline.d.y < mp.ey)) {
% W) N$ ~$ x [! l; R) `& g- Vcontinue;
. Y( s7 k2 W* f' E" O9 Y2 @}5 w) W/ N' x" f" x$ T9 n
if ((iline.o.y >= mp.ey) && (iline.d.y >= mp.ey)) {
- l+ v* H$ [. V5 t% A; Icontinue;
! O% T) X2 M4 G, ]5 Q9 s5 \8 w}
' k# j W9 V# l& |6 `6 Aif ((iline.o.x == iline.d.x) && (iline.o.x >= mp.ex)) {
+ F b& [' l \* vxi = iline.o.x;6 c: H7 Z9 {3 T5 f& C4 b7 M5 |- e
yi = mp.ey;
8 G. q# `4 z9 T4 P8 E; Y4 E}# m8 A/ {8 N7 b5 U+ P
else {0 I3 T9 x0 R8 F# ]
b1 = 0;
! K) [: H9 P5 l% V6 ^2 o4 r8 _b2 = (iline.d.y-iline.o.y)/(iline.d.x-iline.o.x); " M, n8 q8 c, a1 d6 ~
a1 = mp.ey-b1*mp.ex;+ n3 W$ f1 M* {5 R5 h
a2 = iline.o.y-b2*iline.o.x;) N5 H6 F4 q' O* r/ ^
xi = - (a1-a2)/(b1-b2);
. G: Z; a2 `7 \, J, Y* ^! myi = a1+b1*xi;
/ b3 {! G0 M& j h2 `}2 M8 }& W! W0 F1 z: Q4 }
if (xi >= mp.ex) { " N$ ?& `: I& \2 q! t% M( |
xcount += 1;
3 S u1 S+ i, ~, L5 }& H$ J& b}/ L% e* V5 R2 J$ T4 Q f$ w0 x
if (xcount == 2) {
6 H; X3 |7 d% W' p+ d- B( M$ b. @4 bbreak;
% M3 e8 W9 ?7 G+ g4 m}
. X$ q: o5 i* C0 X" A! t}
* @* Y' o3 h- R# B; _% q$ x/ [* |# zreturn xcount;
2 h7 ^9 M* g2 Z4 L};
5 L1 R5 I& s3 ?1 vcanvas.element.prototype.findtargetcorner = function(mp, oimg) {
7 y/ }: X0 b0 X: s% s7 {var xpoints = null;6 r/ L3 {% r4 Q2 w5 r$ i
var corners = ['tl','tr','br','bl'];! v8 F3 X, o' W3 X+ p( d7 k
for (var i in oimg.ocoords) {2 M1 z. ]# f) g" Z S9 Q, B6 n
xpoints = this._findcrosspoints(mp, this._getimagelines(oimg.ocoords.corner));! y' V9 U" g( }# ]" A: ]
if (xpoints % 2 == 1 && xpoints != 0) {
. E2 F# q4 w( ?. V5 Wreturn i;9 T$ {" p/ {5 P3 {9 a
} , ]5 f) [" }6 f; W, I4 {6 E
}
6 X, U" R) J' V6 m) y7 areturn false;) z3 a) I- o0 T/ z0 s4 F R) h
};/ u7 L/ D) n% b- o
canvas.element.prototype.findmouseposition = function(e) {! x/ Z$ Q5 l* j8 h. N& }% v
var parentnode = (e.srcelement) ? e.srcelement.parentnode : e.target.parentnode;) a9 n5 I; U4 X! i# B- ?, q
var issafari2 = !s.support.ie&&!s.support.firefox;- \, F7 X' r& P Y/ N
var scrollleft = document.documentelement.scrollleft || document.body.scrollleft;4 O' X9 y- ?5 H# n
var scrolltop = document.documentelement.scrolltop || document.body.scrolltop;
: e, ^$ y1 U9 P" X5 h, Xvar safarioffsetleft = (issafari2) ? e.target.ownerdocument.body.offsetleft + scrollleft : 0;# G) @* n6 }, W I% W0 l
var safarioffsettop = (issafari2) ? e.target.ownerdocument.body.offsettop + scrolltop : 0;
: X) h- ?9 R0 K; Preturn {
2 {8 u" m# }; u( k+ Zex: e.clientx + scrollleft - parentnode.offsetleft - safarioffsetleft,3 V4 n/ G a9 e! O/ E
ey: e.clienty + scrolltop - parentnode.offsettop - safarioffsettop,6 g- O! r- Q% E' p
screenx: e.screenx,
) e! m, E% [ q7 P. {. n# `7 Z/ pscreeny: e.screeny
. J5 L* ]9 E m! E};7 t4 q2 `* J% o0 G+ }! i9 r
};7 P6 e+ {1 y. I+ A3 A/ K$ ?
canvas.element.prototype.drawborder = function(context, oimg, offsetx, offsety) {
H/ H6 B' G" O) H4 e" Kvar outlinewidth = 2;4 ]- q7 a; R9 W* x0 i G
context.fillstyle = 'rgba(0, 0, 0, .3)';* q: r) y& l& X1 |' S( Y+ u5 M( _2 w+ N
context.fillrect(-2 - offsetx, -2 - offsety, oimg.width + (2 * outlinewidth), oimg.height + (2 * outlinewidth));
* u- j, R q+ o. k8 E. F( Y1 jcontext.fillstyle = '#fff';
4 W, V" H' C [1 M! ~context.fillrect(-offsetx, -offsety, oimg.width, oimg.height);
9 L+ ]1 B" L7 J! e};
; u$ q0 d2 q6 A$ [canvas.element.prototype.drawcorners = function(context, oimg, offsetx, offsety) {6 B6 M5 s9 @, ?
context.fillstyle = rgba(0, 200, 50, 0.5);
0 L" ^) E, c$ v( o/ Wcontext.fillrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);% s l. [0 B [5 Z+ d; a
context.fillrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);
) r9 M& N+ w, g+ g0 E( j1 rcontext.fillrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
" f& t7 q1 V9 V: T# dcontext.fillrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
2 b4 ^# i4 g4 ]( H# g};* M* c+ n# g O3 A: k$ J. B& P$ t5 D
canvas.element.prototype.clearcorners = function(context, oimg, offsetx, offsety) {) Z* m3 t S6 X. H+ ] M
context.clearrect(-offsetx, -offsety, oimg.cornersize, oimg.cornersize);
* k2 N& ~8 I" j8 X( ^, k3 Rcontext.clearrect(oimg.width - offsetx - oimg.cornersize, -offsety, oimg.cornersize, oimg.cornersize);1 e ]* T! X$ c6 o, Z
context.clearrect(-offsetx, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);- z* S. r; G! D( f/ \
context.clearrect(oimg.width - offsetx - oimg.cornersize, oimg.height - offsety - oimg.cornersize, oimg.cornersize, oimg.cornersize);
$ c$ q; a. O) O! r3 z1 Jcontext.restore();8 t. _9 T" J% H4 k+ S5 _
};" n0 ?6 ?5 I m# t( M
canvas.element.prototype.canvasto = function(format) {4 Z" x# C; e* |* u
this.renderall(true,false);
. ]$ K, v6 l& T5 Y ^5 N8 Zvar containercanvas =this._ocontexttop;
. x* u8 ~9 c! F/ D! Mfor (var i = 0, l = this._aimages.length; i < l; i += 1) {6 \1 C) r( o' f
var offsety = this._aimages.height / 2;
5 e5 c( W+ @; T! }var offsetx = this._aimages.width / 2;
0 @+ o3 d/ u. Y. [7 \this.clearcorners(containercanvas, this._aimages, offsetx, offsety);( y8 T. R% r/ w7 u9 e6 W% L; w5 @* _
}/ W. A p# B) Y ?5 W
if (format == 'jpeg' || format == 'png') {, S. t& s' g1 R1 o: x
return this._oelement.todataurl('image/'+format);
8 F" H# y1 f5 S1 m* d) u1 S) S}
2 T4 [% M5 u' U1 A2 _! I; L3 s}; 9 x* z3 Q: I, P$ W
canvas.customevent = function(type) {
( c+ l4 k; g# T0 o5 h- k2 f: X5 Pthis.type = type;1 k0 w; A* K. f4 s
this.scope = null;# F' N* f' x- R# e, X* e" N2 x# F
this.handler = null;
. Z4 o& |9 m& F& W- Jvar self = this;3 e& \/ E9 t, X# g% b
this.fire = function(e) {( }6 X' r8 v" }% ^
if(this.handler != null) {
4 T" A& [8 p5 ]8 nself.handler.call(self.scope, e);: l3 e% Z2 a8 e% h g
}0 N" D" u" `" H/ M4 T/ x
};: x8 p0 X% T4 U! p8 G$ U
}; + H$ k, S0 y- d* O$ S1 ^
}());/ t# m0 l! E1 Z8 R, p/ V7 l* ?
return canvas;
Y; d5 m8 p+ K0 W/ h( P* G0 P3 V7 Y8 Q});
* h1 y, U4 P1 r! p9 mcanvasimg.js代码如下:2 W$ t$ {7 O9 w3 D/ K
复制代码代码如下:define('canvasimg', [ '../multi_upload/core' ], function(s) {
& V' [2 l9 G/ L! Z k- Y9 Qvar canvas = window.canvas || {};9 M, G/ b, c* {
(function () {
0 b0 }% j1 y# I$ y% t9 N0 icanvas.img = function(el, oconfig) {
N. A' ~) l6 }" ^* z% k4 Kthis._initelement(el);
! p5 G9 D1 v+ s. Q) S l5 o9 x# Dthis._initconfig(oconfig);9 t7 ^" G3 x6 w, t4 L! C, t
this.setimagecoords();) r2 X- \8 R7 o3 Y" o5 y( T
};0 Y% n9 t7 g$ j+ n, v& a
canvas.img.css_canvas = canvas-img;
3 B8 D$ h/ [) }9 M( Jvar default_config = { & O0 B) I; x; Z- \6 B
top: { ! b' ~; o! d8 B, `0 N4 w; A) ~
key: top,
8 L5 ]7 Z$ {: {' n, @value: 10
9 B& k/ [. F- b. v, b},/ o+ B) Y1 l0 F: Z) T6 k, Q6 }
left: {
! l. o( X6 k P) u+ Fkey: left,
( k0 G, G) n$ k- ]1 mvalue: 104 m# Y" N/ \+ K1 e4 C- P' Q0 K
},
" }! X2 Y. [; R* Tangle: {
* z% K$ M& D& I+ @; Okey: angle, ( N, S1 S( j1 N# I1 o" J
value: 0 7 y5 u7 ?# g$ M/ G0 o- R
},; G! a! d# ^5 y3 C) R2 A
theta: { ( j- v" j8 ?4 n' P
key: theta, 2 n9 E) f6 x# I% w9 K
value: 0
/ s' z* O& p& P9 z2 i' q- t' v* b }},. ~% U. P/ D/ G3 V- W
scale-x: {
) [/ z& D- Q; ^ b hkey: scalex,
9 X. G( w# C! l5 z9 A% ]" Nvalue: 1' n, A- F% {" n8 t; g* {' X
},' E, Z. m: v# M5 v
scale-y: { / i# ?" ^) b5 r' u( p1 s
key: scaley, " G6 `. Z4 S" d' z$ }# X1 x9 S
value: 1: S; I5 ^! q3 ~! h* b
},. C& N+ o& S# y0 k9 G2 Q+ e
cornersize: {
; K' i8 z; {) @key: cornersize, + k8 d2 J) X$ t& z+ |/ s
value:106 J, j; c9 _1 ^) W
},$ x, ^. `$ {; w. A$ P
borderwidth: {
7 v( N/ i, ` J; k5 dkey: borderwidth, % c$ R* H+ b6 t8 o) ^
value: 10
3 l& A5 ], [" a+ L},$ i9 y6 f6 a: }
polaroidheight: {. l, h4 v" L$ r- A
key: polaroidheight,# v' U+ u I! ]( {
value: 400 R* a/ w5 C U7 |* B" ]2 f* ~) A3 e
},4 V- e# M' v7 I0 u2 B4 z
randomposition: {
* j3 ?% s5 \$ f2 ?7 fkey: randomposition,
( t/ E0 l; N$ y5 K: E& u6 Ovalue: true. g( W0 \7 u# E% P0 \
}
( v8 X7 y% Y" h% h/ K# w};
0 e( }' `* {+ _ E0 W2 Qcanvas.img.prototype._oelement = null;
* C. ~+ ~) n: v3 g9 Q* S- \( v3 vcanvas.img.prototype.top = null;& Y# U& z; G4 |7 Q0 [1 N% z
canvas.img.prototype.left = null;' x- m& e% l0 ]- ]; ^8 `
canvas.img.prototype.maxwidth = null;5 Z7 h" T% Z% m
canvas.img.prototype.maxheight = null;
- y% S' N$ C) S$ F. e5 T! ocanvas.img.prototype.ocoords = null;3 T% X' a8 p3 c8 ^) u3 t
canvas.img.prototype.angle = null;
# O, m3 Y* L* O* ?8 q; p7 e1 kcanvas.img.prototype.theta = null; u1 ?; Z+ \( s+ V6 r
canvas.img.prototype.scalex = null;
& w( ]) T- D9 Qcanvas.img.prototype.scaley = null;1 J% n/ q$ S4 I$ u
canvas.img.prototype.cornersize = null;/ C0 E7 B- y% {6 B
canvas.img.prototype.polaroidheight = null;$ z( E9 U G7 ^1 {6 O' m
canvas.img.prototype.randomposition = null;
. V, i$ j2 H. t/ \+ s. t1 Gcanvas.img.prototype.selected = false;
, ?, `' F/ T& _4 Vcanvas.img.prototype.bordervisibility = false;
4 Z& `/ v" j1 ~! t) q' Wcanvas.img.prototype.cornervisibility = false;
$ `, J& }) i& K" ]9 h4 R A1 ^canvas.img.prototype._initelement = function(el) {% K$ L4 K# F9 q% e
this._oelement = el;
/ o. r ?" d& |& S};
0 u+ d) A& u! K- s4 u, ?( k% ^canvas.img.prototype._initconfig = function(oconfig) {1 n. t+ m3 T0 `3 O: f! W
var skey;
7 y9 s6 ?7 b: @% Ffor (skey in default_config) {1 e' M& z {9 O: t
var defaultkey = default_config[skey].key;
/ P [) n- P: G6 |if (!oconfig.hasownproperty(defaultkey)) { // = !(defaultkey in oconfig)
; q, H; u* {# }' S0 l& \$ j* `" ~this[defaultkey] = default_config[skey].value;0 c& q, a5 J; p
}) b$ j, ]! x; m2 F3 G
else {* m4 g5 q& [" D8 E* J5 q/ P3 k
this[defaultkey] = oconfig[defaultkey];0 `: j0 Y4 \; ?4 M) S; A
}
3 [# ]" W- N: I' L}( f H/ I$ B. b
if (this.bordervisibility) {
* c" }& _' r$ |this.currentborder = this.borderwidth;. ^, B+ \9 e& A# F6 v4 R
}3 X2 Q5 B/ u3 m) q( o$ _- i
else {* ?1 r7 P+ ~, n1 t& k
this.currentborder = 0;2 B* e' L2 E0 b B2 d' }7 t3 A
}: x8 @+ r3 l% C6 ]2 _& X/ I' L0 t. {
var normalizedsize = this.getnormalizedsize(this._oelement, parseint(oconfig.maxwidth), parseint(oconfig.maxheight));
8 j* z' x! q4 H: p1 K# H$ f; @9 qthis._oelement.width = normalizedsize.width;* ~8 e3 b3 ?" [& H
this._oelement.height = normalizedsize.height;( r T9 i5 c3 H B- B
this.width = normalizedsize.width + (2 * this.currentborder);$ K0 j/ \6 ?4 F+ u7 L! k3 f6 Q& o& ^
this.height = normalizedsize.height + (2 * this.currentborder);
4 M4 X6 X, a, w) V1 [/ oif (this.randomposition) {
* {9 N$ P- s; @3 H, nthis._setrandomproperties(oconfig);& _7 `0 i: v+ o& \
}
7 r1 `- u. h/ z* y* Ethis.theta = this.angle * (math.pi/180);
3 t' ?# B* J3 t8 W: C};5 d/ C: K+ f6 x1 v' k/ V
canvas.img.prototype.getnormalizedsize = function(oimg, maxwidth, maxheight) {$ _7 z. u! @' r `. k @
if (maxheight && maxwidth && (oimg.width > oimg.height && (oimg.width / oimg.height) < (maxwidth / maxheight))) {
7 Y, }% D/ p$ t9 h c$ Qnormalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);
% L: n) Z2 y! M4 gnormalizedheight = maxheight;
- F2 `8 S2 a% A( ]7 k+ P}; \/ a$ a* E3 Q5 D O
else if (maxheight && ((oimg.height == oimg.width) || (oimg.height > oimg.width) || (oimg.height > maxheight))) {
% i" F& f0 C6 v1 \normalizedwidth = math.floor((oimg.width * maxheight) / oimg.height);
/ P x+ H) Q* A" U/ j2 O9 _/ lnormalizedheight = maxheight;6 q6 @' }/ y, [, v4 `
} p7 r; S. `9 b R9 m8 z: J
else if (maxwidth && (maxwidth < oimg.width)){
- b% d, l- ~! ]# x0 }6 ~1 Dnormalizedheight = math.floor((oimg.height * maxwidth) / oimg.width);
$ o3 t0 i- Z% [! j3 x$ |4 Fnormalizedwidth = maxwidth;
7 X1 W0 \$ P( S# u( c0 l2 `}
. i4 O/ b/ O& P9 K8 P1 Z" Pelse {. v7 p/ o8 t, K4 I
normalizedwidth = oimg.width;; Y2 c4 y# T7 d2 |/ G" v
normalizedheight = oimg.height; . _# c( v5 \ Z8 o" K
}
* y9 O1 _5 i; d: Mreturn { width: normalizedwidth, height: normalizedheight }6 ]* y3 ~8 j9 [4 v5 _
},* Z4 m7 X; k6 | n+ z) e
canvas.img.prototype.getoriginalsize = function() {
, N# m1 u% y$ Jreturn { width: this._oelement.width, height: this._oelement.height }7 P* v: @) J0 Z6 S; x! [6 D' G
};
5 T: ]3 p: Z6 m: x$ @canvas.img.prototype._setrandomproperties = function(oconfig) {
/ ?1 S0 O/ H8 q: a; l3 l/ a% ]if (oconfig.angle == null) {
8 c3 T" T& Q/ J0 {% o1 n3 Athis.angle = (math.random() * 90);
, Y3 m5 T( S7 X+ @}
" i+ v# a" F. Z8 X, Hif (oconfig.top == null) {; E4 }9 Y1 ~$ z
this.top = this.height / 2 + math.random() * 450;
2 C+ ]+ G: @- F/ l" }0 A. S- w}
; x6 K+ ?% ?( D ~. uif (oconfig.left == null) {
* W" p. {3 A! c$ k" d, ?this.left = this.width / 2 + math.random() * 600;
0 A3 p+ ^ `. i1 u: f; v' Z$ r}
4 [4 |7 n; t! ~! V};; i# t' b' e* y5 W+ V/ {
canvas.img.prototype.setcornersvisibility = function(visible) {
* X" _& g9 V. k: M; wthis.cornervisibility = visible;
# ]& L+ D2 C3 _: L# | _" e};/ N. Y/ W0 w: S4 o
canvas.img.prototype.setimagecoords = function() {
; c8 w" Q# N% M4 I& L8 Y# W: D! rthis.left = parseint(this.left);/ k) _/ v3 K) z$ V9 m r) Y6 a
this.top = parseint(this.top);& e9 t5 v5 c; P% L4 o8 g
this.currentwidth = parseint(this.width) * this.scalex;
* d# n: L: G, r; G2 x' Lthis.currentheight = parseint(this.height) * this.scalex;
0 {9 H9 C" N# cthis._hypotenuse = math.sqrt(math.pow(this.currentwidth / 2, 2) + math.pow(this.currentheight / 2, 2));
* T0 g2 u7 H- A. w0 ?" D: ~this._angle = math.atan(this.currentheight / this.currentwidth);) H/ S( E1 a. G, q
var offsetx = math.cos(this._angle + this.theta) * this._hypotenuse;0 L1 Y+ A5 h2 }
var offsety = math.sin(this._angle + this.theta) * this._hypotenuse;
6 n* s, v4 L" `var theta = this.theta;# X$ X) h/ I, w4 u
var sinth = math.sin(theta);4 C+ R+ A5 _% s9 O9 f; t( N( F
var costh = math.cos(theta);
( U7 P2 h4 q( o4 Z6 {. Ivar tl = {7 E5 V; d* V& G9 o% {1 x# f
x: this.left - offsetx,
$ K) s4 E+ [3 @, G' S. X8 `y: this.top - offsety: g, v m' _" ^, E7 @) y
};2 b9 l1 o7 z q% Z1 ^4 a
var tr = {
. z. K9 l# A3 ~+ U# \% h, V: f% nx: tl.x + (this.currentwidth * costh),
- {% J$ x' n# n0 w& Fy: tl.y + (this.currentwidth * sinth)4 e+ \$ V5 K! @% I) |1 U
};
1 H& O `! v/ ] h1 I) Dvar br = {1 f- Y% V, E3 D" y, Q1 w o4 ~, l
x: tr.x - (this.currentheight * sinth),
6 z$ \! _7 a* A. ^y: tr.y + (this.currentheight * costh)8 v( ]6 k- f) j3 L; w8 r
};
o7 @9 j2 |8 }6 o* cvar bl = {
4 i6 t7 ^& ]8 T$ U' Rx: tl.x - (this.currentheight * sinth),6 W( p' l6 o& G7 n3 M
y: tl.y + (this.currentheight * costh)
" Q4 a# a% {% V7 Z3 }; T7 ^};
7 f$ Q6 u( R& B: Vthis.ocoords = { tl: tl, tr: tr, br: br, bl: bl };, b! A' W1 b4 @7 E
this.setcornercoords();
6 }! t: M" ~% w4 ?$ {+ b( M6 H};
" ?! J4 y1 z8 l( Vcanvas.img.prototype.setcornercoords = function() {
0 E2 ~! T/ f/ ]7 n @' v9 ]var coords = this.ocoords;
. T( F, `( C W* p7 y9 F" n1 nvar theta = this.theta;
& S0 D" a% }' r5 `4 q8 vvar cosoffset = this.cornersize * this.scalex * math.cos(theta);
, h. A9 `" M; H+ b) u4 |' z* M' ]var sinoffset = this.cornersize * this.scalex * math.sin(theta);$ G9 p4 r6 C3 P n! H# L
coords.tl.corner = {
1 L* `+ i; @/ |7 X" d3 Q$ n3 ftl: {* Y2 C. d8 c" C, \ l. P8 U
x: coords.tl.x,% w2 x7 s# x; d6 h" z
y: coords.tl.y0 K- w: Q1 |7 v5 Q, N! t: P
},) @' R# ~5 y& _3 X$ c7 f0 S
tr: {( _4 f+ S. d& D, l4 @
x: coords.tl.x + cosoffset,0 _! ?' f1 x$ z0 {8 J
y: coords.tl.y + sinoffset3 N7 [ C0 x( O8 N# b. s4 H. y
},
! G' y0 p; d$ fbl: {
6 u+ }& w; q. @ Bx: coords.tl.x - sinoffset,
7 I! }( E7 y. cy: coords.tl.y + cosoffset D c+ t/ C3 w* ?* e& m' Z
}
1 J$ E+ |1 l4 l( N6 B& K};% e" Z3 h$ c( l6 \5 U
coords.tl.corner.br = {
- C! m; j" h. j. f/ H4 @x: coords.tl.corner.tr.x - sinoffset,9 U- e: E3 d7 t2 J# w
y: coords.tl.corner.tr.y + cosoffset
4 p+ u5 V( G. j9 A% q9 C5 f; l};- ?& |+ D+ W& I5 y# P
coords.tr.corner = {
5 f, Z4 \) N& \1 \0 m1 W6 Dtl: {% O3 {4 H: W# ?- c1 [# E. u
x: coords.tr.x - cosoffset,. T# ]# {9 Y) w) {2 k
y: coords.tr.y - sinoffset
2 ]* a, g. O. m* ?},2 N8 |, Z6 v; T" \/ n) Z
tr: {$ L# Z5 P* ^9 A% v9 i" s; R4 n
x: coords.tr.x,
9 Q3 j9 U, d! J- ]6 t2 Py: coords.tr.y
' {6 z$ F. a% r) ^3 ^},( f5 X! ?, h& K+ ~5 |7 ?
br: {1 q [5 M+ k* i8 w, L$ P9 k
x: coords.tr.x - sinoffset," g& j7 B' y. E0 n) g+ A
y: coords.tr.y + cosoffset& P4 p; H- g# I1 J+ N$ h
}8 ?% u: V5 d2 K6 w9 e# v6 D
};
, m* v3 W( v& i: s# W: [/ k4 wcoords.tr.corner.bl = {
1 Q3 c- E0 z I1 r1 X0 f+ Vx: coords.tr.corner.tl.x - sinoffset,
9 G7 x5 \2 F+ w- a+ Cy: coords.tr.corner.tl.y + cosoffset1 S7 q4 @( t5 C1 Z
};& k! _7 @. T; V* x2 v
coords.bl.corner = {
2 {% i& S! d. k: Q; K1 V8 p2 }tl: {# ?8 f H8 F4 N
x: coords.bl.x + sinoffset,
2 Y$ ?1 Z- q/ Dy: coords.bl.y - cosoffset# G( Y9 e0 ], c& H& q
},8 a+ ^/ O k) [; J4 T. R2 C2 s
bl: {! a6 ?: @. I5 J( A) @5 s9 x! d. i
x: coords.bl.x,3 ?1 V% q* i2 g; L, z+ K; u
y: coords.bl.y% `' N" C' c( j! v3 f2 a
},
8 r& H5 S) s( I6 H5 k# v0 Nbr: {+ m: k; {! S3 F: O2 j4 u1 G, [, B
x: coords.bl.x + cosoffset,
/ H3 ~, D* K( @5 t! b& \' H6 ^# ey: coords.bl.y + sinoffset* ~! t( F3 D* {* ~! s' l
}
+ J( q# M5 J4 s8 N" E/ T7 W};: @, E* E4 k) X' C1 B- J) y
coords.bl.corner.tr = {
" `2 n# u! ~" t2 L1 v& f0 q4 T2 `) Vx: coords.bl.corner.br.x + sinoffset,
2 m% V/ w3 w1 _4 P( f# z+ h9 yy: coords.bl.corner.br.y - cosoffset1 g" } e9 [8 f" k
};
! ~; y1 M6 h% w: H0 ], e7 acoords.br.corner = {1 C! C1 _( T% t& o( H- T5 v2 Y8 t
tr: {4 P( I' a) f7 k6 p
x: coords.br.x + sinoffset,
3 S, _8 U" S( @5 |1 i! }+ X" `y: coords.br.y - cosoffset
; ^8 ^6 K6 j1 ~9 Q, [9 V9 ]* i},
: A6 K$ `/ N! H" [- Abl: { y' h& m2 |3 i: y3 V e
x: coords.br.x - cosoffset,
1 A& S/ o4 Z; h3 by: coords.br.y - sinoffset! c; ?; F. I/ t$ \+ ^0 s! S" O: A/ r
},1 n9 k; ]1 h% N! ~
br: {/ ]1 p/ z* d) z" R/ Q
x: coords.br.x,
2 C0 Q& m; S5 A! W/ W/ X5 M. D: O& v$ [y: coords.br.y
7 Y9 O* ^- J, y W/ R}
: x3 j* ^% C8 J f. }};
* D# Y' u! W8 mcoords.br.corner.tl = {
( j+ {' j# _# jx: coords.br.corner.bl.x + sinoffset,( @; L1 L! m4 Z& z, t
y: coords.br.corner.bl.y - cosoffset6 C# B, y2 P7 I' h4 U2 v$ z) c
};1 V$ j% Q5 ~+ d, _
};
, B0 L7 J* y5 t$ r: a/ B}());
8 P* D( y# C. M/ N2 L* Xreturn canvas;
! r8 T' S7 Q4 r, F, J9 O% ]6 _});; s! u5 Y. Z1 g; E0 ~+ Y
puzzle.html代码如下:
, ^9 [/ t2 A' t" o* _1 l* ^2 E复制代码代码如下:<!doctype html>+ u$ J4 v: d- m5 N
<html>
! W/ c. ~$ X0 m<head>" a& R- H% z/ q% a
<meta http-equiv=content-type content=text/html; charset=utf-8>3 A: k Y+ k2 ^* _2 w
<title>insert title here</title>1 \; |' ^" E6 t
<link type=text/css href=html5_puzzle.css rel=stylesheet />" c6 c9 L3 d+ E/ l# A
<script type=text/javascript src=../multi_upload/seed.js></script>& T! W! ?: B$ V& t# ~2 t& r
<script type=text/javascript src='html5_puzzle.js'></script>3 ?1 G1 J0 H$ n1 D. U
</head>
4 P) _8 o! F4 E" y8 t4 a1 s% ~! f<body>
- b5 a, r+ R S B" @7 z<div id='html5_puzzle'>
' D$ d$ L" y. x ~! D- d( }1 V<div id='puzzle_left'>
8 ]2 e) z5 q, V" T6 H1 ]/ p5 z' c<div class='puzzle_column'>" n+ C- P; m9 d: W9 z7 W$ l) {4 f Z
<ul>$ @/ y1 @+ N q
<li><img src='small_img/1.jpg' data-index='1' /></li>
6 c/ _" i6 e# Q3 R, o<li><img src='small_img/2.jpg' data-index='2' /></li>
( c! [! H6 _ ]' S<li><img src='small_img/3.jpg' data-index='3' /></li>
; c+ B( M& V) H<li><img src='small_img/4.jpg' data-index='4' /></li>4 R) h0 K( j+ q; }' C
<li><img src='small_img/5.jpg' data-index='5' /></li>6 U I" z6 x" d; ^
</ul>
" m; p3 Q$ f" M, N( P4 y</div>
# {$ L1 ~4 `/ M6 U& j( Z4 Z' j<div class='puzzle_column'>. u5 z" t6 s" o1 m3 D
<ul>6 h8 [, O4 x8 C# M9 H
<li><img src='small_img/6.jpg' data-index='6' /></li>: S( P& I8 G/ q: E# Z \" |
<li><img src='small_img/7.jpg' data-index='7' /></li>2 U0 F @0 o, w m2 A
<li><img src='small_img/8.jpg' data-index='8' /></li>* U: ^1 A l( |) v
<li><img src='small_img/9.jpg' data-index='9' /></li>
% l% q5 I. V$ ]1 w* f3 N& f<li><img src='small_img/10.jpg' data-index='10' /></li>2 @8 u" T" _0 i% ^ y ]7 v& S
</ul>
- V. C, J% M/ a4 r</div>
* [6 O0 v, H, S7 Z</div>! U0 k0 Z: g2 g3 _. [- Q% z% `
<div id='puzzle_right'>' r9 N5 \1 m& |8 W
<div id='puzzle_canvas'>2 P1 k3 \; z) {+ l4 P* L2 p
<canvas id=canvid1></canvas>
! u; k+ k8 ]- i8 ^4 ~! I<div id='canvas_menu'>2 O/ P3 R# L- ?. R; r q0 r" |8 Z/ k
<a href='javascript:void(0)' id='photo_delete'>删除</a> <a$ Q& ~( q! H6 y H9 b$ ^
href='javascript:void(0)' id='photo_update'>更改图片</a>- T' f/ y. C+ R
</div>( C5 w$ R0 W* M& Q) y3 k7 O
</div>
7 }3 d+ a" N& K, d* L# X<img id=bg src=big_img/1.jpg width='600' height='450' />$ b2 o+ U5 \7 Q$ b% Q
</div>
. ]/ O! E) d! K6 R: f# w% V' N# u<div id='puzzle_bottom'>( g! c5 o5 n0 O+ n8 b2 ?. ?* x! `: H
<a href='javascript:void(0)' id='add_img'><span>添加图片</span><input* u/ o8 q/ u* H8 ~8 {! Q3 e
type=file multiple= id='fileimage'> </a> <a8 u$ k3 f, N1 J- E. G) S
href='javascript:void(0)' id='upload_btn'>上传</a> <a>点击图片可以旋转,拖拽,
/ ^& ?. I& P/ @缩放哦!</a>/ g7 S/ K5 t2 S) G# Y
</div>( A, P; M7 @0 H& U4 V
</div>( K* A) x4 F7 G' G+ p8 ]+ ^* y
<input type=file id='test'>
% J7 a ]" _" u# ~" ~9 _* }<canvas id='test_canvas'></canvas>& T4 R. j( e4 X/ K1 K
</body>. x0 D: Q4 `) E
</html>" ?+ t5 l6 w" P! ]+ K
html5_puzzle.css代码如下:3 O1 A* Q1 [6 m! A; T
复制代码代码如下:@charset utf-8;</p> <p>#html5_puzzle {( D! ^! g( A+ l$ ?
font-size: 0;. v5 q4 V; \8 x/ x' n w
}</p> <p>canvas {- r" k# b" A) H7 g; P* T
background-color: transparent;
3 p. U1 L( f: Cleft: 0;
8 n4 _4 \. e& \. vposition: absolute;& z( X) b6 F8 d
top: 0;
7 E; ^: D; q# d2 [9 t! C: g( E' W}</p> <p>.puzzle_column,#puzzle_left,#puzzle_right,#add_img {- K; n! N$ [" _& |1 P9 r
display: inline-block;
! B4 ]/ O- i8 d}</p> <p>.puzzle_column li {5 f& }" q2 e+ }! F# ?
display: block;
& F7 i A3 h" {( vmargin: 5px;
4 g8 u. N6 j, w6 t( A- J8 }border: 1px solid #ffffff;
4 G5 H3 T. w6 N8 B# _}</p> <p>.puzzle_column li:hover {
9 x' j- V# ^* ~* W- B& _3 ]border: 1px solid #3b5998;
E" ?- g, e% F; _+ Gcursor: pointer; J( i9 m ~9 e
}</p> <p>.puzzle_column {# U4 L% ^. i: p, G
font-size: 0;
) m8 t' E6 K8 _/ t3 K- x( H: P9 }}</p> <p>#puzzle_left,#puzzle_right {
! g0 l' s0 D8 o9 J2 q, F yborder: 1px solid #3b5998;. ]8 c0 k- |; [1 m e) [( I
}</p> <p>#puzzle_right,#puzzle_bottom a {% S$ y. A! p9 w+ k p% B+ `
font-size: 14px;
! @! Q. N6 p! L/ |& mmargin: 10px 0 0 10px;
. ~! R; \5 u* N o}</p> <p>#puzzle_bottom {4 v. x c. r! e: z0 J! K
margin: 5px 0;4 o) |- t/ h7 b- | ?: D6 Z
}</p> <p>#puzzle_canvas img {6 ]: h. [" _1 V9 T: I2 R" N
}</p> <p>#puzzle_canvas {
% M2 m! n: E$ @+ b, j# O7 J3 `( q$ j- woverflow: hidden;- q0 V1 T; I( c7 m, |
width: 600px;
0 u9 ^# \9 V4 T5 F5 Y! Theight: 450px;
" b/ I/ O' `" T7 w- K& [position: relative;
4 z+ @- [8 l6 F, h G}</p> <p>#add_img input {
- H/ m: E+ l: O6 r2 [' }3 C. iposition: absolute;2 h. L( [4 m/ h7 a+ S' ^7 [
font-size: 100px;
$ T4 z1 ~! ^7 V6 m; a0 tright: 0;
, d5 b6 _4 Z* Z% Stop: 0;
/ m+ p. }- j0 b3 Y- s* s& b$ `) b* Topacity: 0;9 C8 b, H' S: O1 Q& Y* Q
}</p> <p>#add_img {
! v, |2 Y" t1 y6 X0 B+ J8 k3 nposition: relative;3 Z! O, y2 K' E+ ~" ~0 S
display: inline-block;6 b0 v. }+ ^/ {( O4 |* b
background: #3b5998;
. o7 G4 t7 H# h: B- }border-radius: 4px;
$ W0 e* v8 O( S2 w& a C/ O/ Xpadding: 4px 12px;
# t2 G- y: h7 O# loverflow: hidden; j' V" ]% J$ U7 i$ s; x
color: #ffffff;
3 T$ n7 V- s6 h8 H/ T' d}</p> <p>#bg,#show_list {8 o6 h+ T; ?7 [/ N
display: none;
7 W, y7 ^: `5 i7 C0 u' }4 P j}</p> <p>#canvas_menu {
3 I* J* x& o O% q. h/ j6 G% O Vborder: 1px solid red;* u, K) x/ y" p' d' C% T; v
position: absolute;# P+ W( i+ l+ T. A& G( [
z-index: 5;
+ u4 C+ X- S: W8 V" @/ p* _7 a3 Ltop: 0;
, @& v! S* F6 c+ d2 l* Bleft: 0;$ [+ d& x5 ^8 H! v3 ?
display: none;
9 J2 y/ n7 |: U; I6 j}</p> <p>#canvas_menu a {+ K. R/ d) W' P# u$ P" {0 e+ ]
display: inline-block;
, j1 P" D/ ]7 s+ ~" [1 Y' Z9 j6 Q}</p> <p>#test_canvas {* s( z: \! T6 I' B" Z; n% u
top: 700px;
0 v5 F9 a7 l7 v/ f: I}5 [4 C0 \: t4 `0 X
html5_puzzle.js代码如下:
# K) K. ~1 U1 M4 [2 ]复制代码代码如下:require([ 'img_upload', '../puzzle/canvasimg', '../puzzle/canvaselement' ], function(
; K; V& t$ A3 e2 _/ o1 ss, canvasimg, canvaselement) {
1 t* c% t0 a7 l3 t4 L* ivar img=[];4 o( G( ^7 ]0 `2 m% |
var canvas = new canvaselement.element();
" W6 Y* G* z' ^canvas.init('canvid1', {7 |2 m7 n, ~$ C9 G5 M
width : 600,, `- M" z% O! |! g
height : 450& w- K. c( C0 b1 t I
});8 I- F& }) H2 L5 b
s('.puzzle_column img').on('click',function(e){
6 K7 z5 O. `- q& \- ivar index=this.getattribute('data-index');# h( o! D& B I1 U
$('bg').onload = function() {
- C# x& t' X5 {( h" Fvar ctx=$('canvid1-canvas-background').getcontext('2d');4 y' ~& [, o/ W- F/ K1 b3 f' ~5 H* P
ctx.clearrect(0, 0,600,450);
/ W2 M: f- \' x' Y, q0 V6 himg[0]=new canvasimg.img($('bg'), {});% w* N8 Y! G9 I8 ]
canvas.setcanvasbackground(img[0]);# `+ n$ \# ^! D' ^
};2 v, N2 G6 J/ \3 c& U- ~
$('bg').setattribute('src','medium_img/'+index+'.jpg');( }4 A+ F% T8 d0 V% L; s) ?6 B
e.stoppropagation();
* H8 U; q# @/ ~3 O});# e5 P1 |, Q- h5 k; L9 J
var canvasdemo = function() {2 r! q" W8 w8 h" K
return {# ?+ ^* B+ u/ B$ _' d& b
init : function() {
; s; Y+ m3 g# U0 [1 xvar img_list=dom.query('#puzzle_canvas img');
, p$ }! i& T: ^5 h% K7 z9 dimg[0]=new canvasimg.img($('bg'), {});
: {' V$ N: o% E/ qs.each(img_list,function(i,el){
( H% Z& t8 r8 g( E g4 _el.setattribute('data-index',i);( K: Y% K+ R/ }: x+ c; q; [
img.push(new canvasimg.img(el, {}));
9 D! T0 c: L* _" I4 Vcanvas.addimage(img[i+1]);$ Y9 \& M3 \) Q, q- i7 N* r _
});1 m$ v; ?3 X0 L" s
canvas.setcanvasbackground(img[0]);
; m' n/ [8 z! b8 d& Pthis.cornersvisible = (this.cornersvisible) ? false : true;+ U! R: q6 w2 |/ I6 S% c
this.modifyimages(function(image) {& y( O |6 f$ b3 N' H& Q
image.setcornersvisibility(this.cornersvisible);
5 r2 y3 ]1 I( S});
* ^( o, k# ], v' k1 E},/ Q7 s1 [5 U3 r0 W% ]
modifyimages : function(fn) {
: S- P# m! S( D* B" }$ _) g% Ifor ( var i =0, l = canvas._aimages.length; i < l; i += 1) {
" }8 n) d6 h! xfn.call(this, canvas._aimages);' ~' j: X! _0 H/ k
}1 B' S L2 U/ u/ V$ i
canvas.renderall(false,false);1 `; C: _0 i7 M1 e& D: u% n
s('#puzzle_canvas img').remove();4 w/ J5 j K" B+ \) a
img = [];2 ?( T7 _ q) ~) @- p
}
% \+ t6 h/ M( |9 P7 ]8 p2 P8 i1 ?+ c}; r* R1 u' _# I' ?' Z: U: h
}();+ H0 y) q" w G$ [
function getcurimg(){
3 I& U3 c$ Y- w3 Wvar oimg=canvas._prevtransform.target;
( B3 a& d/ M k6 ^* D: Kfor(var i=0;i<canvas._aimages.length;i++){/ c6 t8 Z; }+ [: x+ N
if(canvas._aimages._oelement.src==oimg._oelement.src){$ T1 l5 ^( P+ N) y0 E
return i;
% \5 ^2 V8 x: T2 h}
( m7 o: |- {" G}" t2 |6 ]$ {7 _4 o
}, A( j! T/ t! y6 H- i% Q1 `
s('#photo_delete').on('click',function(e){
7 }5 Z, b* d: p" H- rvar i=getcurimg();: {0 u4 ~4 z& r: [$ N8 y# c0 Z
canvas._aimages.splice(i,1);' i+ \' H' m- P4 z4 f
canvas.renderall(true,true);
9 y) o% d6 S# E4 I$('canvas_menu').style.display=none;
G4 X5 Q- Y" f" k});
% [" k) R2 i" U1 A& _s('#photo_update').on('click',function(e){
. Z$ U$ S0 M( ~( N# T7 F I$('test').click();
& N2 |) p1 U* A4 @5 d});
7 o2 y2 ^: O1 e: T: w6 i1 ns('#test').on('change',function(e){
+ ~+ ^, S9 G; v; t2 I5 d4 g2 Wvar files = e.target.files || e.datatransfer.files;6 _" y, W* R) ~( B0 j0 w; D5 _; Q
var reader = new filereader();+ R6 C2 n2 ^! `
reader.onload = (function() {
8 B' o+ ]! Y7 p7 _- N& L* t w6 x( `- Areturn function(e) {# O" p% W; d" O* d/ z
var dataurl = e.target.result, canvas1 = document.queryselector('#test_canvas'), ctx = canvas1.getcontext('2d'), img = new image();1 G7 E, {2 o! j- L7 D# a
img.onload = function(e) {
" ^4 d7 ~1 |& e# \! [# ]! T) N) q( oif(img.width>200||img.height>200){
, p- G" R5 Z3 {1 a, b7 D! C: v Zvar prop=math.min(200/img.width,200/img.height);# o6 m, |# {; T6 X- x) G
img.width=img.width*prop;
( M# W* Q' J# A n- timg.height=img.height*prop;
% u$ g! K# c, M) [}) r. @) D0 K$ S0 _" t
canvas1.width=img.width;
8 x- x C1 p. m6 H3 }- Pcanvas1.height=img.height;
4 ]+ R! J8 W' Z1 ?, ?9 ^1 O Actx.drawimage(img, 0, 0, img.width, img.height);* J* i" I% r+ Q3 J2 }% d" X! V
s('#canvid1').html(s('#canvid1').html()+<img src='+canvas1.todataurl(image/jpeg)+'/>);2 k8 H; m Q9 h* j( [8 Q/ R0 N
var t = window.settimeout(function() {
8 r) L$ a# P# W( }# H7 q8 Pvar i=getcurimg(),target=canvas._prevtransform.target;$ \8 q/ h6 y- ^
console.log(target);
- V; j) K7 f' C- Zcanvas._aimages=new canvasimg.img(dom.query('#canvid1 img')[0], {, S4 m) l+ I8 U; K4 e, N; L4 j
top:target.top,
. m( v% Y, s% s+ q% Tleft:target.left,
; ? k3 \6 p# R4 Y/ I) U* sscalex:target.scalex,3 J! u8 {) z3 Y: ~) Y G7 w) q
scaley:target.scaley,
) }0 v& n9 d7 ~7 hangle:canvas.curangle
7 F/ g2 t2 f9 y% c$ I* h});
. [: \1 T) @4 t+ s0 ]canvas.rendertop();
: I/ j4 A! F' R' y, |6 Kcleartimeout(t);
2 c" p1 ^* J C& @; O" {( _s('#canvid1 img').remove();# {+ ]( i, p( v& h
},1000);+ O n; x1 d1 n' `, h) H, y' g9 b1 ^
};; s( _! | J+ i2 V( v0 g# M
img.src = dataurl;
5 Z& X- ]8 k4 r4 F; R" Z: a};: c9 i8 e; n: U/ k
})();
- s$ l" M6 P1 [1 [( `: V" f' h# zreader.readasdataurl(files[0]);. _' E9 V" q7 p0 ^- n1 \
});1 [; |" k. [; @0 H
s('#upload_btn').on('click',function(){
1 w0 I, m/ |( ^* j; K* K- Mvar imgdata = canvas.canvasto('jpeg');
6 }( \1 X4 ^0 l Y. Dvar imgvalue = imgdata.substr(22);
; u+ @; w' ], O2 a- fs.ajax({
: N$ F+ n& ?" }4 w Yurl : 'http://localhost/html5/upload1.php',
- C9 u0 ]9 Q; N5 E1 k1 {0 ^% y, V6 rtype : 'post',7 N8 ?8 s" v N$ k
data : {
. c/ c& \# C5 S A0 T: r# D, t" R3 Bimgdata : imgvalue,8 l$ L8 k' w) l# ^# M2 ]2 C1 t
file_name :'mix_img.jpeg'3 u( W* [' E. y% p c. m8 q
},* J S% R) p0 }1 U& |
datatype : 'text',* s Q% m0 l; a* H0 C* n
success : function(data) {4 a4 q [' g9 d( o' D
alert(s);( T3 \/ b, e$ b8 z: I }- F+ N3 d
}
: A% m& ^' @$ g$ v});+ s( y8 b' ~" L" U* m6 N
});, V' J2 r9 {( ]0 P) z& R
});
{5 e& P1 k. @# D至于用html5 input读取图片,这很简单就不贴出来了。
9 C1 ^, j1 N$ \6 a3 v$ f
2 V6 @* `2 |4 Z, W更多网页制作信息请查看: 网页制作 |