×î½ü×öÁËÒ»¸öÏîÄ¿£¬Õâ¸öÏîÄ¿ÖÐÐèҪʵÏÖµÄÒ»¸ö¹¦ÄÜÊÇ£ºÓû§×Ô¶¨ÒåÍ·Ïñ£¨Óû§ÔÚ±¾µØÑ¡ÔñÒ»ÕÅͼƬ£¬ÔÚ±¾µØ½«Í¼Æ¬²Ã¼ô³ÉÂú×ãϵͳҪÇó³ß´çµÄ´óС£©¡£Õâ¸ö¹¦ÄܵÄÐèÇóÊÇ£ºÍ·Ïñ×î³õ¼ôÇÐΪһ¸öÕý·½ÐΡ£Èç¹ûÑ¡ÔñµÄͼƬСÓڹ涨µÄÍ·ÏñÒªÇó³ß´ç£¬ÄÇôÕâÕûÕÅͼƬ¶¼»á×÷ΪͷÏñ¡£Èç¹û´óÓڹ涨µÄ³ß´ç£¬ÄÇôÓû§¿ÉÒÔÑ¡ÔñÒª²Ã¼ôµÄÇøÓò¡£Óû§µã»÷È·¶¨°´Å¥£¬¾Í½«²Ã¼ôµÃµ½µÄͼƬÊý¾Ý·¢Ë͵½·þÎñÆ÷£¬ÔÚºó¶Ë½«Í¼Æ¬Êý¾Ý±£´æ³ÉÒ»¸öÎļþ¡£
ÒªÍê³ÉÉÏÊö¹¦ÄÜ£¬Éæ¼°µ½µÄ֪ʶÓУºajax£¬canvasºÍhtml5ÖеÄfiles½Ó¿Ú¡£ÎÒ½«ÊµÏÖÕâ¸ö¹¦ÄܵĴúÂë·â×°µ½ÁË4¸öÄ£¿éÖУ¬·Ö±ðÊÇajax.js£¬preview.js£¬shear.jsºÍcustomerImg.js¡£
ajax.js:ÓÃÓÚ·¢ËÍajaxÇëÇó¡£
preview.js:ÓÃÓÚͼƬԤÀÀ
shear.js£ºÓÃÓڲüôͼƬ
customer.js:×Ô¶¨ÒåÍ·Ïñ¡£ÔÚÕâ¸öÄ£¿éÖÐÒ©ÒýÈëajax.js£¬preview.jsºÍshear.js
ÎÒʹÓÃwebpack½øÐдò°ü¡£ÎÒ»¹Ê¹ÓÃÁËjqueryºÍjquery-ui¡£
ÎÒ´ÓÕâ¸öÏîÄ¿ÖгéÀë³öÁËÕâ¸ö¹¦ÄÜ¡£ÏÂÃæÊÇÕâ¸ö¹¦ÄܵÄÏêϸ´úÂë¡£
1.HTML´úÂëÑ¡ÔñͼƬԤÀÀÈ·¶¨
2.CSS´úÂë
.clearfix:after{
content: "";
display: block;
clear: both;
height: 0;
overflow: hidden;
visibility: hidden;
}
img{
¡¡¡¡vertical-align: middle;
¡¡¡¡max-width:100%
}
.m-warp{
width: 800px;
}
.item{
margin-top: 20px;
}
.col{
float: left;
}
.col-1{
position: relative;
width: 450px;
height: 450px;
outline: 1px solid #333;
}
.preview{
display: inline-block;
}
.col-2{
width: 300px;
margin-left: 50px;
}
label{
display: block;
text-align: center;
width: 100px;
font-size: 16px;
color: #fff;
background-color: #888888;
height: 30px;
line-height: 30px;
}
.mask{
position: absolute;
z-index: 1;
top:0;
left: 0;
bottom: 0;
right: 0;
background-color: rgba(0,0,0,.4);
}
.cvsMove{
position: absolute;
z-index: 2;
outline: 2px dotted #333;
cursor: move;
display: none;
}
ÓÐÁËcssºÍhtmlµÄÔËÐнá¹ûÈçÏ£º
3.js´úÂë customerImg.jsvar $ = require('jquery'); var ajax = require('./ajax.js'); var preview = require('./preview.js'); var shear = require('./shear.js'); /** * ×Ô¶¨ÒåÍ·Ïñ * @constructor */ function CustomerImg() { this.isSupport = null; this.previewBox = null; this.warp = null; } /** * Èë¿Ú * @param warp ²Ù×÷ÇøÓò jquery½Úµã */ CustomerImg.prototype.start = function (warp) { var info,me,warpBox; me = this; this.isSupport = this.__isSupport(); if(!this.isSupport) { info = $('<p>ÄãµÄä¯ÀÀÆ÷²»Ö§³Ö×Ô¶¨ÒåÍ·Ïñ£¬¿É¸ü»»¸ß°æ±¾µÄä¯ÀÀÆ÷×Ô¶¨ÒåÍ·Ïñ</p>'); $('body').html(info); return this; } (warp && warp.length > 0){ this.warp = warp; }else{ return this; } //Ô¤ÀÀ preview.start(warp,shear.start.bind(shear,warp)); this.previewBox = warp.find('#preview'); //È·¶¨ warp .find('#submit') .unbind('click') .on('click',me.__submit.bind(me)); }; /** * Ìá½» * @private */ CustomerImg.prototype.__submit = function () { var cvsMove,data,fd; cvsMove = this.previewBox.find('#cvsMove'); data = cvsMove[0].toDataURL('image/jpg',1); fd = { 'customerImg':data }; ajax.upload(fd); }; /** * ÅжÏÊÇ·ñÖ§³Ö×Ô¶¨ÒåÍ·Ïñ * @returns {boolean} * @private */ CustomerImg.prototype.__isSupport = function () { var canvas,context; canvas= document.createElement('canvas'); if(typeof FileReader === 'function'&& canvas.getContext && canvas.toDataURL){ return true; }else{ return false; } }; var customerImg = new CustomerImg(); module.exports = customerImg;
preview.js
¡¡