html:
<div class ="upload" id ="head_up" style ="display: none" >
<p class ="p-2" >
<i class ="i-1" > </i > 上传头像<i class ="i-2" id ="exitHeadImgUpload" > </i >
</p >
<div class ="left pull-left" >
<input id ="lefile" type ="file" style ="display: none" />
<button type ="button" class ="btn btn1"
onclick ="$('input[id=lefile]').click();" >
<i class ="i-3" > </i > 选择照片
</button >
<button type ="button" class ="btn btn2" id ="autodyne" >
<i class ="i-4" > </i > 自拍头像
</button >
<div class ="p-1" >
<canvas id ="headImgCanvas" height ="235px;" width ="235px"
style ="position: absolute;margin-left:10px;margin-top:7px;display:none" > your
of the browser not support canvas.</canvas >
<canvas
style ="position: absolute;margin-left:10px;margin-top:7px;display:none"
id ="headImgPhotoCanvas" height ="235px;" width ="235px" > your
of the browser not support canvas.</canvas >
<video id ="headImgVideo" height ="235px;" width ="335px"
controls ="controls" autoplay ="autoplay" style ="display:none" > </video >
</div >
<div class ="row" >
<div class ="col-sm-offset-1" >
<button type ="button" class ="btn btn3" id ="headImgPhoto" > 拍照</button >
<button type ="button" class ="btn btn3" id ="headImgClipping" > 剪裁</button >
<button type ="button" class ="btn btn4" id ="headImgSubmit" > 上传</button >
</div >
</div >
</div >
<div class ="right pull-left" >
<p class ="red" > 预览效果 :</p >
<div class ="xiaotu" >
<img id ="headImgMin" height ="60" width ="60" />
</div >
<p class ="p-4" > 60x60</p >
<div class ="datu" >
<img id ="headImgMax" height ="146" width ="146" />
</div >
<p class ="p-4" > 146x146</p >
</div >
</div >
css :
*{
margin :0 ;
padding :0 ;
box-sizing :border-box ;
list-style :none ;
}
body {
font-size :16 px ;
font-family :Arial,Verdana ;
color :#666 ;
font-family : "SimHei" , "Helvetica Neue" ;
}
.main {
background :purple ;
position :relative ;
width :1170 px ;
height :1000 px ;
}
.upload {
position :absolute ;
height :442 px ;
width :640 px ;
background-color :#f0f0f0 ;
left :50 % ;
top :50 % ;
margin-left :-320 px ;
margin-top :-211 px ;
border-radius :5 px ;
overflow :hidden ;
}
.p-2 {
background :#e5e4e4 ;
height :45 px ;
line-height :45 px ;
}
.i-1 {
display :inline-block ;
width :19 px ;
height :22 px ;
background :url(../../imgs/head_img_upload/list.png) no-repeat ;
margin :10 px ;
vertical-align :top ;
}
.i-2 {
float :right ;
height :30 px ;
width :30 px ;
background :url(../../imgs/head_img_upload/list.png) 0 -22 px no-repeat ;
margin-top :6 px ;
margin-right :6 px ;
}
.btn1 ,.btn2 {
font-size :18 px ;
line-height :30 px ;
border :1 px solid #cacaca ;
}
.btn1 {
background :#55b8ed ;
color :#fff ;
}
.btn2 {
background :#f0f0f0 ;
}
.i-3 {
display :inline-block ;
width :20 px ;
height :18 px ;
background :url(../../imgs/head_img_upload/list.png) -22 px 0 no-repeat ;
vertical-align :top ;
margin-top :5 px ;
}
.i-4 {
display :inline-block ;
width :20 px ;
height :20 px ;
background :url(../../imgs/head_img_upload/list.png) -43 px 0 no-repeat ;
vertical-align :top ;
margin-top :4 px ;
}
.p-1 {
width :257 px ;
height :250 px ;
margin-top :20 px ;
margin-bottom :16 px ;
}
.btn3 ,.btn4 {
width :99 px ;
height :36 px ;
color :#fff ;
font-size :18 px ;
}
.btn4 {
background :#fe4f4f ;
}
.left {
width :409 px ;
padding :23 px ;
}
.upload .right {
padding :13 px ;
width :231 px ;
}
.red {
color :#fe4f4f ;
}
.xiaotu {
width :60 px ;
height :60 px ;
margin :10 px auto ;
}
.p-4 {
height :40 px ;
text-align :center ;
color :#b7b7b7 ;
}
.datu {
width :146 px ;
height :146 px ;
margin :10 px auto ;
}
js:
$(function () {
$("#lefile" ).change(function (e) {
console.log($("#lefile" ).val());
var headImgPhotoCanvas = document.getElementById("headImgPhotoCanvas" );
var headImgPhotoContext = headImgPhotoCanvas.getContext("2d" );
var image = new Image();
image.src = $("#lefile" ).val();
headImgPhotoContext.drawImage(image, 0 , 0 , 235 , 235 );
});
$("#exitHeadImgUpload" ).click(function (e) {
$("#head_up" ).trigger('close' );
});
$("#autodyne" )
.click(
function (e) {
$("#headImgCanvas" ).css("display" , "block" );
$("#headImgPhotoCanvas" ).css("display" , "none" );
var canvas = document.getElementById("headImgCanvas" );
var context = canvas.getContext("2d" );
var video = document.getElementById("headImgVideo" );
var getUserMedia = (navigator.getUserMedia
|| navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia || navigator.msGetUserMedia);
getUserMedia.call(navigator, {
video : true ,
audio : false
},
function (localMediaStream) {
video.src = window.URL
.createObjectURL(localMediaStream);
video.onloadedmetadata = function (e2) {
console.log("Label: "
+ localMediaStream.label);
console.log("AudioTracks" ,
localMediaStream
.getAudioTracks());
console.log("VideoTracks" ,
localMediaStream
.getVideoTracks());
};
}, function (e1) {
console.log('Reeeejected!' , e1);
});
video.addEventListener("play" , function () {
var i = window.setInterval(function () {
context.drawImage(video, 0 , 0 , 235 , 235 )
}, 20 );
}, false );
video.addEventListener('pause' , function () {
window.clearInterval(i);
}, false );
video.addEventListener('ended' , function () {
clearInterval(i);
}, false );
});
var headImgCanvas = document.getElementById("headImgCanvas" );
var headImgContext = headImgCanvas.getContext("2d" );
var headImgPhotoCanvas = document.getElementById("headImgPhotoCanvas" );
var headImgPhotoContext = headImgPhotoCanvas.getContext("2d" );
var headImgData;
$("#headImgPhoto" ).click(function (e) {
$("#headImgCanvas" ).css("display" , "none" );
$("#headImgPhotoCanvas" ).css("display" , "block" );
headImgData = headImgContext.getImageData(0 , 0 , 235 , 235 );
headImgPhotoContext.putImageData(headImgData, 0 , 0 , 0 , 0 , 235 , 235 );
setStrokeStyleGradient(headImgPhotoContext);
headImgPhotoContext.lineWidth = 0.8 ;
headImgPhotoContext.strokeRect(45 , 45 , 146 , 146 );
});
function setStrokeStyleGradient (ctx) {
var gradient = ctx.createLinearGradient(0 , 0 , 170 , 0 );
gradient.addColorStop("0" , "magenta" );
gradient.addColorStop("0.5" , "blue" );
gradient.addColorStop("1.0" , "red" );
ctx.strokeStyle = gradient;
}
var centerPosition = null ;
headImgPhotoCanvas.addEventListener('mousedown' , function (e) {
centerPosition = getMousePos(headImgPhotoCanvas, e);
headImgPhotoCanvas.addEventListener('mousemove' , mobileRectangular,
false );
}, false );
headImgPhotoCanvas.addEventListener('mouseup' , function (e) {
if (headImgPhotoCanvas.removeEventListener) {
headImgPhotoCanvas.removeEventListener("mousemove" ,
mobileRectangular, false );
} else if (headImgPhotoCanvas.detachEvent) {
headImgPhotoCanvas.detachEvent('onmousemove' , mobileRectangular,
false );
}
}, false );
var position = null ;
var left_top_x = 45 ;
var left_top_y = 45 ;
var rect_width = 146 ;
var rect_height = 146 ;
function mobileRectangular (e) {
headImgPhotoCanvas.setAttribute("height" , 235 );
headImgPhotoCanvas.setAttribute("width" , 235 );
position = getMousePos(headImgPhotoCanvas, e);
headImgPhotoContext.putImageData(headImgData, 0 , 0 , 0 , 0 , 235 , 235 );
setStrokeStyleGradient(headImgPhotoContext);
left_top_x = (45 + (position.x - centerPosition.x));
left_top_y = (45 + (position.y - centerPosition.y));
if (left_top_x < 0 ) {
left_top_x = 1 ;
}
if (left_top_y < 0 ) {
left_top_y = 1 ;
}
if ((234 - left_top_x) < 146 ) {
left_top_x = 234 - 146 ;
}
if ((233 - left_top_y) < 146 ) {
left_top_y = 233 - 146 ;
}
headImgPhotoContext.strokeRect(left_top_x, left_top_y, 146 , 146 );
}
var imagebase = null ;
$("#headImgClipping" ).click(
function (e) {
var headImgPhotoClippingData = headImgPhotoContext
.getImageData(left_top_x + 1 , left_top_y + 1 ,
rect_width - 2 , rect_height - 2 );
headImgPhotoCanvas.setAttribute("height" , rect_height - 2 );
headImgPhotoCanvas.setAttribute("width" , rect_width - 2 );
headImgPhotoContext.putImageData(headImgPhotoContext
.createImageData(235 , 235 ), 0 , 0 , 0 , 0 , 235 , 235 );
headImgPhotoContext.putImageData(headImgPhotoClippingData, 0 ,
0 , 0 , 0 , 235 , 235 );
imagebase = headImgPhotoCanvas.toDataURL("image/png" );
$("#headImgMin" ).attr("src" , imagebase);
$("#headImgMax" ).attr("src" , imagebase);
});
$("#headImgSubmit" ).click(function (e) {
$.post("personalCenter/changeHeadImg" , {
"headImg" : imagebase
}, function (data) {
data = eval ("(" + data + ")" );
$("#head_up" ).trigger('close' );
headImgPhotoCanvas.setAttribute("height" , 235 );
headImgPhotoCanvas.setAttribute("width" , 235 );
$("#userHeadImage" ).attr("src" , data.info.substring(1 ));
});
});
function getMousePos (canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x : evt.clientX - rect.left * (canvas.width / rect.width),
y : evt.clientY - rect.top * (canvas.height / rect.height)
};
}
});