JavaScript头像上传插件源码分享

本文实例为大家分享了JavaScript头像上传插件源码,供大家参考,具体内容如下

效果图:

源码:
cxc.js

/* cxc.js 频繁操作公共接口 */
var $ = function (id) {
 return document.getElementById(id);
}; //通过id获取dom对象
var A = function (msg) {
 alert(msg);
}; //alert的简写
var EmptyFun = function () {
}; // 空方法
var setL = function (dom, L) {
 dom.style.left = L + "px";
}; // 设置 dom 的 left
var setT = function (dom, T) {
 dom.style.top = T + "px";
}; // 设置 dom 的 top
var setLT = function (dom, L, T) {
 dom.style.left = L + "px";
 dom.style.top = T + "px";
}; //同时设置 dom 的 left top
var getLT = function (dom) {
 return [parseInt(dom.style.left), parseInt(dom.style.top)];
}; // 返回dom的left和top值,类型为整型数组[int,int]
var setW = function (W) {
 dom.style.width = W + "px";
};  // 设置 dom 的 width
var setH = function (H) {
 dom.style.height = H + "px";
};  // 设置 dom 的 height
var setWH = function (dom, W, H) {
 dom.style.width = W + "px";
 dom.style.height = H + "px";
};  //同时设置 dom 的 width height
var getWH = function (dom) {
 return [parseInt(dom.style.width), parseInt(dom.style.height)];
}; // 返回dom的 width 和 height 值,类型为整型数组[int,int]
var setLTWH = function (dom, L, T, W, H) {
 dom.style.left = L + "px";
 dom.style.top = T + "px";
 dom.style.width = W + "px";
 dom.style.height = H + "px";
}; //同时设置 dom 的 left top width height
var getLTWH = function (dom) {
 return [parseInt(dom.style.left), parseInt(dom.style.top), parseInt(dom.style.width), parseInt(dom.style.height)]
};  // 返回dom的 left top width height 值,类型为整型数组[int,int,int,int]
var setcursor = function (dom,shape) {
 dom.style.cursor = shape;
}; //设置鼠标经过dom的指针形状
var EventsType = ["click", "mousedown", "mouseup", "mouseover", "mouseleave", "mousemove"];//事件类型
var AddEvent = function (dom, type, fun) {
 dom.addEventListener(type, fun, false);
};  //添加dom对象的事件监听方法
var AddEvent2 = function (dom, type1, fun1, type2, fun2) {
 dom.addEventListener(type1, fun1, false);
 dom.addEventListener(type2, fun2, false);
};  //一次添加dom的两个事件监听方法
var AddEvent3 = function (dom, type1, fun1, type2, fun2, type3, fun3) {
 dom.addEventListener(type1, fun1, false);
 dom.addEventListener(type2, fun2, false);
 dom.addEventListener(type3, fun3, false);
}; //一次添加dom的三个事件监听方法
var DelEvent = function (dom, type, fun) {
 dom.removeEventListener(type, fun, false);
}; // 删除dom对象的事件监听方法
var DelEvent2 = function (dom, type1, fun1, type2, fun2) {
 dom.removeEventListener(type1, fun1, false);
 dom.removeEventListener(type2, fun2, false);
}; //一次删除dom对象的两个事件监听方法
var DelEvent3 = function (dom, type1, fun1, type2, fun2, type3, fun3) {
 dom.removeEventListener(type1, fun1, false);
 dom.removeEventListener(type2, fun2, false);
 dom.removeEventListener(type3, fun3, false);
}; //一次删除dom对象的三个事件监听方法
var inArray = function (str, arr) {
 for (var i = 0; i < arr.length; i++) {
  if (str == arr[i]) {
   return true;
  }
 }
 return false;
}; // 判断字符串str是否存在于数组arr
var cannotselect = function () {
 window.getSelection().removeAllRanges();
}; //页面元素(文字、图片等)不能被选中
var setStyle = function (dom, styleName, styleValue) {
 dom.setAttribute("style", styleName + ":" + styleValue + ";");
}; //设置dom的 一个style 属性值
var setStyle2 = function (dom, styleName1, styleValue1, styleName2, styleValue2) {
 dom.setAttribute("style", styleName1 + ":" + styleValue1 + ";" + styleName2 + ":" + styleValue2 + ";");
};//一次设置dom的 两个style 属性值
var delStyle = function (dom, styleName) {
 dom.removeAttribute("style", styleName);
};//删除dom的 一个style 属性值
var delStyle2 = function (dom, styleName1, styleName2) {
 dom.removeAttribute("style", styleName1);
 dom.removeAttribute("style", styleName2);
};//一次删除dom的 两个style 属性值
var setAttr = function (dom, attrName, attrValue) {
 dom.setAttribute(attrName, attrValue);
};// 设置dom的 一个属性值
var setAttr2 = function (dom, attrName1, attrValue1, attrName2, attrValue2) {
 dom.setAttribute(attrName1, attrValue1);
 dom.setAttribute(attrName2, attrValue2);
};//一次设置dom的 两个属性值
var delAttr = function (dom, attrName) {
 dom.removeAttribute(attrName);
};//删除dom的 一个属性值
var delAttr2 = function (dom, attrName1, attrName2) {
 dom.removeAttribute(attrName1);
 dom.removeAttribute(attrName2);
};//删除dom 的两个属性值
var Click = function (dom) {
 dom.click();
};// 点击dom
var Hide = function (dom) {
 dom.style.display = "none";
};// 隐藏dom
var Show = function (dom) {
 dom.style.display = "inline";
}; // 显示dom
/* cxc.js 频繁操作公共接口 */ 

/* AJAX 接口 */
var url, method, msg;
var xmlReq = new XMLHttpRequest();
var AJAX = function (url, method, msg, callback) {
 xmlReq.open(method, url, true);
 xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlReq.onreadystatechange = function () {
  if (xmlReq.readyState == 4) {
   if (xmlReq.status == 200) {
    callback();
   }
   else {
    A("bad status is " + xmlReq.status);
   }
  }
 };
 xmlReq.send(msg);
};
/* AJAX 接口 */

one.js

/* one.js */
/* my website philosophy */
/*
 注:一般网站,浏览器最大化时,没有横向滚动条,有纵向滚动条,页面缩放按比例只在合适的地方用到
 <html>标签 不必加css和js控制 <body>标签 作为总父标签 用它控制整个页面的宽度和高度
 <body>的宽度 一般为100%(考虑滚动条存在与否) 而高度可根据页面需求自定义
 也就是说body的宽高就是页面的宽高 页面高度如果超出 浏览器窗口高度 出现滚动条
*/
var one = {
 screenW: null, //可用浏览器窗口的宽度
 screenH: null, //可用浏览器窗口的高度
 body: null,  //document.body对象
 bodyW: null,  //body的宽度
 bodyH: null,  //body的高度
 avatar: null, //默认头像div
 avatar_img:null,
 main: null,  //处理上传图片的主要父div
 mainW: 430,  //main的宽度
 mainH:400,  //main的高度
 mainL: null,  //main 的left位置
 mainT:null,  //main 的top位置
 top: null,
 upfile:null,
 center:null,
 bigimg: null,
 movebox: null,
 moveimg: null,
 d11: null,
 d22: null,
 d33: null,
 TopLeft: null,
 TopRight: null,
 BottomRight: null,
 BottomLeft: null,
 p2: null,
 p3:null
};
var Init = function () {
 //////////////////////////////////////////////////////////////////
 one.screenW = window.innerWidth;
 one.screenH = window.innerHeight;
 one.body = document.body;
 one.bodyW = one.body.offsetWidth;
 one.bodyH = one.screenH; //定义body的高度等于可用浏览器窗口的高度
 one.body.setAttribute("style", "height:" + one.bodyH + "px;");
 //////////////////////////////////////////////////////////////////
 one.avatar = $("avatar");
 one.avatar_img = $("avatar_img");
 one.main = $("main");
 one.mainL = (one.bodyW - one.mainW) / 2;
 one.mainT = (one.screenH - one.mainH) / 2;
 ///////////////////////////////////////////////////////////
 one.top = $("top");
 one.center = $("center");
 one.bigimg = $("bigimg");
 one.movebox = $("movebox");
 one.moveimg = $("moveimg");
 one.d11 = $("d11");
 one.d22 = $("d22");
 one.d33 = $("d33");
 ///////////////////////////////////////////////////////////
 one.TopLeft = $("TopLeft");
 one.TopRight = $("TopRight");
 one.BottomRight = $("BottomRight");
 one.BottomLeft = $("BottomLeft");
 ///////////////////////////////////////////////////////////
 one.p2 = $("p2");
 one.p3 = $("p3");
 ///////////////////////////////////////////////////////////
 setLT(one.main, one.mainL, one.mainT);
 Hide(one.main);
};
var End = function () { 

};
window.onload = function () {
 Init(); //初始化,获取页面上所有需要处理的标签对象,并赋初始值
 Events(); //定义页面中的所有事件
 End(); //js文件加载完成之后的处理工作
};//dom元素全部加载完成,执行此方法

Events.js

var downX, downY, oldL, oldT, tempWH, tempL, tempT, dxMax,tempMaxL,tempMaxT;
var file, imgtype, imgsize, imgW, imgH, imgP, imgURL;
var bigimgL, bigimgT;
var moveboxWH, moveboxL, moveboxT, moveboxMinL, moveboxMinT, moveboxMaxL, moveboxMaxT;
var moveimgL, moveimgT;
var topL, topT;
var gen = {
 _moveboxWH:null,
 _moveboxL: null,
 _moveboxT: null,
};
/* one.avatar Events start */
var avatar_click = function () {
 one.upfile = document.createElement("input");
 setAttr2(one.upfile, "type", "file", "id", "upfile");
 this.parentNode.appendChild(one.upfile);
 Click(one.upfile);
 one.upfile.onchange = function () {
  file = this.files[0];
  imgtype = file.type;
  if (!fun.check_imgtype()) {
   return;
  } //检查文件类型
  imgsize = file.size;
  if (!fun.check_imgsize()) {
   return;
  }; //检查图片大小
  var reader = new FileReader();
  reader.onload = function () {
   fun.setImgWH(this.result, imgtype);
   delete (reader);
  };
  reader.readAsDataURL(file);
  ///////////////////////////
  this.parentNode.removeChild(one.upfile);
 };
};
var avatar_mouseover = function () {
 setStyle2(one.avatar, "border", "2px solid #46AFDC", "box-shadow", "0 0 5px #46AFDC");
};
var avatar_mouseleave = function () {
 delStyle2(one.avatar, "border", "box-shadow");
};
/* one.avatar Events end */ 

/* one.top Events start */
var topLimit = function () {
 if (topL < 0)
  topL = 1;
 else if (topL > one.bodyW - 432)
  topL = one.bodyW - 432 - 1;
 if (topT < 0)
  topT = 1;
 else if (topT > one.screenH - 402)
  topT = one.screenH - 402 - 1;
};
var top_mousedown = function (e) {
 if (e.button > 0) {
  top_mouseup();
  return false;
 }
 downX = e.clientX;
 downY = e.clientY;
 oldL = one.main.offsetLeft;
 oldT = one.main.offsetTop;
 AddEvent2(document, EventsType[2], top_mouseup, EventsType[5], doc_top_mousemove);
};
var doc_top_mousemove = function (e) {
 topL = oldL + e.clientX - downX;
 topT = oldT + e.clientY - downY;
 topLimit();
 setLT(one.main, topL, topT);
};
var top_mouseup = function () {
 DelEvent2(document, EventsType[2], top_mouseup, EventsType[5], doc_top_mousemove);
};
/* one.top Events end */ 

/* one.movebox Events start */
var moveboxLimit = function () {
 if (moveboxL <= moveboxMinL)
  moveboxL = moveboxMinL;
 else if (moveboxL >= moveboxMaxL)
  moveboxL = moveboxMaxL;
 if (moveboxT <= moveboxMinT)
  moveboxT = moveboxMinT;
 else if (moveboxT > moveboxMaxT)
  moveboxT = moveboxMaxT;
};
var movebox_mousedown = function (e) {
 if (e.button > 0) {
  movebox_mouseup();
  return false;
 }
 e.preventDefault && e.preventDefault();
 downX = e.clientX;
 downY = e.clientY;
 oldL = moveboxL;
 oldT = moveboxT;
 AddEvent2(document, EventsType[2], movebox_mouseup, EventsType[5], doc_movebox_mousemove);
};
var doc_movebox_mousemove = function (e) {
 moveboxL = oldL + e.clientX - downX;
 moveboxT = oldT + e.clientY - downY;
 moveboxLimit();
 setLT(one.movebox, moveboxL, moveboxT);
 fun.setimg();
 fun.set_dxx();
};
var movebox_mouseup = function () {
 DelEvent2(document, EventsType[2], movebox_mouseup, EventsType[5], doc_movebox_mousemove);
};
/* one.movebox Events end */ 

/* 拉伸事件开始 */
var TopLeft_mousedown = function (e) {
 if (e.button > 0) {
  TopLeft_mouseup();
  return false;
 }
 e.preventDefault && e.preventDefault();
 downX = e.clientX;
 downY = e.clientY;
 oldL = moveboxL;
 oldT = moveboxL;
 tempWH = moveboxWH;
 tempL = moveboxL - bigimgL;
 tempT = moveboxT - bigimgT;
 tempMaxL = moveboxMaxL;
 tempMaxT = moveboxMaxT;
 dxMax = tempL >= tempT ? tempT : tempL;
 AddEvent2(document, EventsType[2], TopLeft_mouseup, EventsType[5], doc_TopLeft_mousemove);
};
var doc_TopLeft_mousemove = function (e) {
 movebox_mouseup();//移动事件屏蔽,非常重要
 var dx = e.clientY - downY;
 if (dx < 0 && Math.abs(dx) > dxMax) {
  dx = -dxMax;
 }
 else if (dx > 0 && dx > tempWH - pic.pwh_min) {
  dx = tempWH - pic.pwh_min;
 }
 moveboxMaxL = tempMaxL + dx;
 moveboxMaxT = tempMaxT + dx;
 moveboxL = oldL + dx;
 moveboxT = oldT + dx;
 moveboxWH = tempWH - dx;
 setLT(one.movebox, moveboxL, moveboxT);
 setWH(one.movebox, moveboxWH , moveboxWH);
 fun.setimg();
 fun.set_dxx();
};
var TopLeft_mouseup = function () {
 DelEvent2(document, EventsType[2], TopLeft_mouseup, EventsType[5], doc_TopLeft_mousemove);
}; 

var TopRight_mousedown = function (e) {
 if (e.button > 0) {
  TopRight_mouseup();
  return false;
 }
 e.preventDefault && e.preventDefault();
 downX = e.clientX;
 downY = e.clientY;
 oldL = moveboxL;
 oldT = moveboxL;
 tempWH = moveboxWH;
 tempL = imgW - (moveboxL - bigimgL) - moveboxWH;
 tempT = moveboxT - bigimgT;
 tempMaxL = moveboxMaxL;
 tempMaxT = moveboxMaxT;
 dxMax = tempL >= tempT ? tempT : tempL;
 AddEvent2(document, EventsType[2], TopRight_mouseup, EventsType[5], doc_TopRight_mousemove);
};
var doc_TopRight_mousemove = function (e) {
 movebox_mouseup();//移动事件屏蔽,非常重要
 var dx = e.clientY - downY;
 if (dx < 0 && Math.abs(dx) > dxMax) {
  dx = -dxMax;
 }
 else if (dx > 0 && dx > tempWH - pic.pwh_min) {
  dx = tempWH - pic.pwh_min;
 }
 moveboxMaxL = tempMaxL + dx;
 moveboxMaxT = tempMaxT + dx;
 moveboxL = oldL;
 moveboxT = oldT + dx;
 moveboxWH = tempWH - dx;
 setLT(one.movebox, moveboxL, moveboxT);
 setWH(one.movebox, moveboxWH, moveboxWH);
 fun.setimg();
 fun.set_dxx();
};
var TopRight_mouseup = function () {
 DelEvent2(document, EventsType[2], TopRight_mouseup, EventsType[5], doc_TopRight_mousemove);
}; 

var BottomRight_mousedown = function (e) {
 if (e.button > 0) {
  BottomRight_mouseup();
  return false;
 }
 e.preventDefault && e.preventDefault();
 downX = e.clientX;
 downY = e.clientY;
 oldL = moveboxL;
 oldT = moveboxL;
 tempWH = moveboxWH;
 tempL = imgW - (moveboxL - bigimgL) - moveboxWH;
 tempT = imgH - (moveboxT - bigimgT) - moveboxWH;
 tempMaxL = moveboxMaxL;
 tempMaxT = moveboxMaxT;
 dxMax = tempL >= tempT ? tempT : tempL;
 AddEvent2(document, EventsType[2], BottomRight_mouseup, EventsType[5], doc_BottomRight_mousemove);
};
var doc_BottomRight_mousemove = function (e) {
 movebox_mouseup();//移动事件屏蔽,非常重要
 var dx = e.clientY - downY;
 if (dx > 0 && dx > dxMax) {
  dx = dxMax;
 }
 else if (dx < 0 && Math.abs(dx) > tempWH - pic.pwh_min) {
  dx = -(tempWH - pic.pwh_min);
 }
 moveboxMaxL = tempMaxL - dx;
 moveboxMaxT = tempMaxT - dx;
 moveboxL = oldL;
 moveboxT = oldT;
 moveboxWH = tempWH + dx;
 setLT(one.movebox, moveboxL, moveboxT);
 setWH(one.movebox, moveboxWH, moveboxWH);
 fun.setimg();
 fun.set_dxx();
};
var BottomRight_mouseup = function () {
 DelEvent2(document, EventsType[2], BottomRight_mouseup, EventsType[5], doc_BottomRight_mousemove);
}; 

var BottomLeft_mousedown = function (e) {
 if (e.button > 0) {
  BottomLeft_mouseup();
  return false;
 }
 e.preventDefault && e.preventDefault();
 downX = e.clientX;
 downY = e.clientY;
 oldL = moveboxL;
 oldT = moveboxL;
 tempWH = moveboxWH;
 tempL = moveboxL - bigimgL;
 tempT = imgH - (moveboxT - bigimgT) - moveboxWH;
 tempMaxL = moveboxMaxL;
 tempMaxT = moveboxMaxT;
 dxMax = tempL >= tempT ? tempT : tempL;
 AddEvent2(document, EventsType[2], BottomLeft_mouseup, EventsType[5], doc_BottomLeft_mousemove);
};
var doc_BottomLeft_mousemove = function (e) {
 movebox_mouseup();//移动事件屏蔽,非常重要
 var dx = e.clientY - downY;
 if (dx > 0 && dx > dxMax) {
  dx = dxMax;
 }
 else if (dx < 0 && Math.abs(dx) > tempWH - pic.pwh_min) {
  dx = -(tempWH - pic.pwh_min);
 }
 moveboxMaxL = tempMaxL - dx;
 moveboxMaxT = tempMaxT - dx;
 moveboxL = oldL - dx;
 moveboxT = oldT;
 moveboxWH = tempWH + dx;
 setLT(one.movebox, moveboxL, moveboxT);
 setWH(one.movebox, moveboxWH, moveboxWH);
 fun.setimg();
 fun.set_dxx();
};
var BottomLeft_mouseup = function () {
 DelEvent2(document, EventsType[2], BottomLeft_mouseup, EventsType[5], doc_BottomLeft_mousemove);
};
/* 拉伸事件结束 */ 

/* 两个按钮事件开始 */
var callback = function () {
 var txt = xmlReq.responseText;
 one.avatar_img.src = "../saveimg/"+txt;
 Hide(one.main);
 Show(one.avatar);
};
var create_msg = function () {
 var msg = "moveboxL=" + (moveboxL - bigimgL) + "&moveboxT=" + (moveboxT - bigimgT) + "&moveboxWH=" + moveboxWH;
 msg += "&imgURL=" + imgURL;
 return msg;
};
var p2_click = function () {
 url="../Avatar/AJAX_saveimg";
 method = "post";
 msg = create_msg();
 AJAX(url, method, msg, callback);
};
var p3_click = function () {
 Hide(one.main);
 Show(one.avatar);
};
/* 两个按钮事件结束 */
var Events = function () {
 AddEvent3(one.avatar, EventsType[0], avatar_click, EventsType[3], avatar_mouseover, EventsType[4], avatar_mouseleave);//avatar
 AddEvent(one.top, EventsType[1], top_mousedown);//top
 AddEvent(one.movebox, EventsType[1], movebox_mousedown);//movebox
 AddEvent(one.TopLeft, EventsType[1], TopLeft_mousedown);//TopLeft
 AddEvent(one.TopRight, EventsType[1], TopRight_mousedown);//TopRight
 AddEvent(one.BottomRight, EventsType[1], BottomRight_mousedown);//BottomRight
 AddEvent(one.BottomLeft, EventsType[1], BottomLeft_mousedown);//BottomLeft
 AddEvent(one.p2, EventsType[0], p2_click);//p2
 AddEvent(one.p3, EventsType[0], p3_click);//p3
 /* =========================================== END =========================================== */
 AddEvent(document, EventsType[5], cannotselect);//最后添加整个页面无法选中事件
};

def.js

var pic = {
 pwh_max: 299, //图片最大宽高
 pwh_min: 30,  //图片最小宽高
 P:10/1,   //图片宽高比
 movediv_min: 30, //截框最小宽高
 movediv_default: 100,//截框初始宽高
 W_H: false, //宽大于高?
 imgtype: ["image/jpeg", "image/png", "image/gif", "image/bmp"],//支持这4种类型图片
 imgsize: 5 * 1024 * 1024, //最大5M
 d11WH: 119,
 d22WH: 99,
 d33WH: 71,
 URL:window.URL || window.webkitURL || window.mozURL || window.msURL || false,
};
var fun = {
 FormBlob: function (dataURI) {
  var byteString, splits = false, splits1 = dataURI.replace(new RegExp("^data:.*base64,"), function () {
   splits = true;
   return "";
  });
  byteString = atob(splits1);
  var byteStringlength = byteString.length, ia = new Uint8Array(byteStringlength);
  for (var i = 0; i < byteStringlength; i++) {
   ia[i] = byteString.charCodeAt(i);
  }
  return new Blob([ia], {
   type: imgtype
  });
 },
 check_imgtype: function () {
  if (!inArray(imgtype, pic.imgtype)) {
   one.upfile.parentNode.removeChild(one.upfile);
   alert("请选择正确的图片类型");
   return false;
  } else { return true;}
 },
 check_imgsize: function () {
  if (imgsize > pic.imgsize) {
   this.parentNode.removeChild(this);
   alert("图片不能超过5M");
   return false;
  } else { return true;}
 },
 setImgWH: function (src,type) {
  var image = new Image();
  image.onload = function () {
   var newcanvas = document.createElement("canvas");
   newcanvas.style.display = "none";
   var bodys = document.body;
   bodys.appendChild(newcanvas);
   var ctx = newcanvas.getContext("2d");
   var width = this.width, height = this.height;//图片的宽高
   var w, h; //选取图片的宽高
   var cw, ch;//画布的宽高
   var P = width / height;
   imgP = P;
   pic.W_H = width > height ? true : false;
   if (pic.W_H) {
    if (P >= 10) {
     ch = pic.pwh_min;
     cw = pic.pwh_max;
     h = height;
     w = h * pic.pwh_max / pic.pwh_min;
    }
    else {
     if (height <= pic.pwh_min) {
      ch = pic.pwh_min;
      cw = Math.round(ch * P);
      h = height;
      w = width;
     }
     else if (width >= pic.pwh_max) {
      cw = pic.pwh_max;
      ch = Math.round(cw / P);
      h = height;
      w = width;
     }
     else {
      cw = width;
      ch = height;
      h = height;
      w = width;
     }
    }
   }
   else {
    if (P <= 1 / 10) {
     cw = pic.pwh_min;
     ch = pic.pwh_max;
     w = width;
     h = w * pic.pwh_max / pic.pwh_min;
    }
    else {
     if (width <= pic.pwh_min) {
      cw = pic.pwh_min;
      ch = Math.round(cw / P);
      w = width;
      h = height;
     }
     else if (height >= pic.pwh_max) {
      ch = pic.pwh_max;
      cw = Math.round(ch * P);
      w = width;
      h = height;
     }
     else {
      cw = width;
      ch = height;
      h = height;
      w = width;
     }
    }
   }
   /////////////////////////////////////////////////////
   imgW = newcanvas.width = cw;
   imgH = newcanvas.height = ch;
   ctx.fillStyle = "#FFFFFF";
   ctx.fillRect(0, 0, cw, ch);
   ctx.drawImage(image, 0, 0, w, h, 0, 0,cw, ch);
   imgURL = newcanvas.toDataURL(type, 1);
   //imgURL = pic.URL.createObjectURL(fun.FormBlob(imgURL));
   one.d11.src = one.d22.src = one.d33.src = one.bigimg.src = one.moveimg.src = imgURL;
   ctx.clearRect(0, 0, cw, ch);
   bodys.removeChild(newcanvas);
   delete DATA;
   delete image;
   fun.setStart();
  };
  image.onerror = function () {
   alert("图片已损坏,请上传正确图片");
  };
  image.src = src;
 },
 setStart: function () {
  Hide(one.avatar);
  Show(one.main);
  fun.set_bigimg();
  fun.set_movebox();
  fun.set_dxx();
 },
 set_bigimg: function () {
  bigimgL = Math.round((pic.pwh_max - imgW) / 2);
  bigimgT = Math.round((pic.pwh_max - imgH) / 2);
  setLT(one.bigimg,bigimgL,bigimgT);
 },
 set_movebox: function () {
  if (pic.W_H) {
   moveboxWH = imgH <= pic.movediv_default ? imgH : pic.movediv_default;
  }
  else {
   moveboxWH = imgW <= pic.movediv_default ? imgW : pic.movediv_default;
  }
  moveboxL = Math.round((pic.pwh_max - moveboxWH) / 2);
  moveboxT = Math.round((pic.pwh_max - moveboxWH) / 2);
  moveboxMinL = bigimgL;
  moveboxMinT = bigimgT;
  moveboxMaxL = Math.round(pic.pwh_max - moveboxWH - bigimgL);
  moveboxMaxT = Math.round(pic.pwh_max - moveboxWH - bigimgT);
  setLT(one.movebox, moveboxL, moveboxT);
  setWH(one.movebox, moveboxWH, moveboxWH);
  moveimgL = -Math.round((imgW - moveboxWH) / 2);
  moveimgT = -Math.round((imgH - moveboxWH) / 2);
  setLT(one.moveimg, moveimgL, moveimgT);
 },
 set_dxx: function () {
  var P1 = pic.d11WH / moveboxWH;
  var P2 = pic.d22WH / moveboxWH;
  var P3 = pic.d33WH / moveboxWH;
  var d11W = Math.round(imgW * P1);
  var d22W = Math.round(imgW * P2);
  var d33W = Math.round(imgW * P3);
  var d11H = Math.round(imgH * P1);
  var d22H = Math.round(imgH * P2);
  var d33H = Math.round(imgH * P3);
  setWH(one.d11, d11W, d11H);
  setWH(one.d22, d22W, d22H);
  setWH(one.d33, d33W, d33H);
  var d11L = Math.round(moveimgL * P1);
  var d22L = Math.round(moveimgL * P2);
  var d33L = Math.round(moveimgL * P3);
  var d11T = Math.round(moveimgT * P1);
  var d22T = Math.round(moveimgT * P2);
  var d33T = Math.round(moveimgT * P3);
  setLT(one.d11, d11L, d11T);
  setLT(one.d22, d22L, d22T);
  setLT(one.d33, d33L, d33T);
 },
 setimg: function () {
  moveimgL = bigimgL - one.movebox.offsetLeft;
  moveimgT = bigimgT - one.movebox.offsetTop;
  setLT(one.moveimg, moveimgL, moveimgT);
 },
};

Index.cshtml

<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="width=device-width" />
 <script src="~/Scripts/one.js"></script>
 <script src="~/Scripts/Events.js"></script>
 <script src="~/Scripts/def.js"></script>
 <script src="~/Scripts/cxc.js"></script>
 <link href="~/Content/Avatar_Main.css" rel="stylesheet" />
 <title>@ViewBag.Title</title>
</head>
<body>
 <div id="avatar">
  <img id="avatar_img" src="~/Images/default_avatar.jpg" />
 </div> 

 <div id="main">
  <div id="top">
   <p id="p1"> 图 片 截 取 </p>
  </div>
  <div id="center">
   <div id="movebox">
    <i id="TopLeft"></i>
    <i id="TopRight"></i>
    <i id="BottomRight"></i>
    <i id="BottomLeft"></i>
    <img id="moveimg"/>
   </div>
   <div id="black"></div>
   <img id="bigimg"/>
  </div> 

  <div id="d1">
   <img id="d11"/>
  </div>
  <div id="d2">
   <img id="d22"/>
  </div>
  <div id="d3">
   <img id="d33"/>
  </div>
  <div id="bottom">
   <p id="p2">就是它了</p>
   <p id="p3">暂且放弃</p>
  </div>
 </div>
</body>
</html>

Avatar_Main.css

body {
 margin:0px;
 padding:0px;
 background-color:#9C938F;
}
#avatar{
 width:120px;
 height:120px;
 border:2px solid #FFFFFF;
 position:absolute;
 top:30px;
 left:8%;
 border-radius:7px;
 background-color:#ffffff;
 overflow:hidden;
 cursor:pointer;
}
#avatar_img{
 width:120px;
 height:120px;
}
#upfile{
 display:none;
}
#main{
 position:absolute;
 width:430px;
 height:400px;
 background-color:#9C938F;
 border-bottom:1px solid #fff;
 border-right:1px solid #fff;
 border-left:1px solid #635E5B;
 border-top:1px solid #635E5B;
 border-radius:8px;
}
#top,#center,#d1,#d2,#d3,#bottom{
 position:absolute;
 border-bottom:1px solid #635E5B;
 border-right:1px solid #635E5B;
 border-left:1px solid #fff;
 border-top:1px solid #fff;
 background-color:#9C938F;
 border-radius:8px;
}
#top{
 width:424px;
 height:43px;
 left:2px;
 top:2px;
 text-align: center;
 cursor:move;
}
#p1{
 position:absolute;
 left:115px;
 top:-30px;
 font-size:30px;
 font-family:"微软雅黑";
 color: #9C938F;
 font-weight:normal;
 text-shadow: -1px -1px white, 1.2px 1.2px #333333;
}
#center{
 width:300px;
 height:300px;
 top:49px;
 left:2px;
 overflow:hidden;
 border-radius:0px;
}
#d1{
 overflow:hidden;
 width:120px;
 height:120px;
 top:49px;
 right:2px;
 border-radius:0px;
}
#d2{
 overflow:hidden;
 width:100px;
 height:100px;
 top:173px;
 right:2px;
 border-radius:0px;
}
#d3{
 overflow:hidden;
 width:72px;
 height:72px;
 top:277px;
 right:2px;
 border-radius:0px;
}
#bottom{
 width:424px;
 height:43px;
 left:2px;
 bottom:2px;
}
#p2,#p3{
 position:absolute;
 width:100px;
 height:30px;
 font-size:22px;
 font-family:"微软雅黑";
 color: #9C938F;
 font-weight:normal;
 text-shadow: -1px -1px white, 1.2px 1.2px #333333;
}
#p2:hover,#p3:hover{
 cursor:pointer;
 color:#bbbbbb;
}
#p2{
 top:-15px;
 left:200px;
}
#p3{
 top:-15px;
 right:10px;
}
#bigimg{
 position:absolute;
}
#black{
 position:absolute;
 z-index:99;
 width:299px;
 height:299px;
 background-color:#000;
 opacity:0.6;
}
#movebox {
 position: absolute;
 z-index: 100;
 overflow: hidden;
 cursor:move;
} 

#BottomRight,#TopRight,#TopLeft,#BottomLeft {
 background:#D6FB66;
 display:block;
 width:6px;
 height:6px;
 overflow:hidden;
 position:absolute;
 z-index:105;
 bottom:0;
 right:0;
 cursor:nw-resize;
}
#BottomLeft {
 bottom:0;
 left:0;
 cursor:ne-resize;
}
#TopRight {
 top:0;
 right:0;
 cursor:ne-resize;
}
#TopLeft {
 top:0;
 left:0;
 cursor:nw-resize;
}
#moveimg{
 position:absolute;
}
#d11,#d22,#d33{
 position:absolute;
}

以上就是本文的全部内容,希望对大家学习javascript程序设计有所帮助。

(0)

相关推荐

  • 实现Asp.net mvc上传头像加剪裁功能

    在我们使用QQ上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐? 本文主要介绍了Asp.net mvc实现上传头像加剪裁功能,分享给大家供大家参考.具体如下: 运行效果截图如下: 具体代码如下: 前台代码 <link href="~/Content/fineuploader.css" rel="stylesheet" /> <link href="~/Content/jquery.Jcrop

  • 针对多用户实现头像上传功能PHP代码 适用于登陆页面制作

    一个网站,其实说白了就是某几个特定功能的组合,而更换用户头像就在这些功能之中.今天就来做个测试,针对不同的用户,实现头像上传功能. -------------------------------------------------------------------------------- 成品图 思路  •针对不同的用户上传头像,我们要为每一个已登录的用户创建一个文件夹,文件夹的名称以当前用户的用户名为准. •用户上传成功后,跳转到用户登录成功后的页面,并刷新用户头像. 登陆页面 表单制作

  • 关于js拖拽上传 [一个拖拽上传修改头像的流程]

    如今现代的浏览器已经有很多支持拖拽文件读取操作,其优点不再复述.前端时间利用拖拽改进了一下网站的头像上传流程,对其中的要点和实践体会做一点总结. 先看一下总体视图:1. 文件拖拽接受区域要有明显的标示,并且要尽可能的大(由于版面的原因,这个界面的拖放盒子并不大).可以用虚线框盒子等样式吸引用户拖拽文件.最好有明显的文字提示和图标配合. 2. 在交互体验上当文件拖入浏览器窗口时,可以用拖放区变换背景颜色等向用户发起放置操作邀请. 实现代码: 复制代码 代码如下: doc.bind({ 'drage

  • Android实现本地上传图片并设置为圆形头像

    先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像. 因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥. 效果图: 核心代码: 复制代码 代码如下: public class MainActivity extends Activity {         private ImageView iv;         private String appKey="";                //填写你的Applicatio

  • 全屏js头像上传插件源码高清版

    本文实例为大家分享了全屏js头像上传插件源码,供大家参考,具体内容如下 index.html <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>ccp</title> <link href="Content/ccp.css" rel=&qu

  • web前端开发upload上传头像js示例代码

    这次分享一个简易的上传头像示例,其大致流程为: 一.将选择的图片转为base64字符串 function preview(file) {//预览图片得到图片base64 var prevDiv = document.getElementById('preview'); if (file.files && file.files[0]) { var reader = new FileReader(); reader.onload = function(evt){ prevDiv.innerHT

  • php视频拍照上传头像功能实现代码分享

    如果要在php中实现视频拍照我们需要借助于flash插件了,由flash拍出的确照片我们再通过php的$GLOBALS ['HTTP_RAW_POST_DATA']接受数据,然后保存成图片就可以了,下面我来介绍一下. 使用的是$GLOBALS ['HTTP_RAW_POST_DATA'],这个系统函数跟post很相似,但是$GLOBALS ['HTTP_RAW_POST_DATA']支持的数据格式更丰富些. 设计流程: $GLOBALS ['HTTP_RAW_POST_DATA']传递照片数据流

  • php+js iframe实现上传头像界面无跳转

    上传头像,界面无跳转的方式很多,我用的是加个iframe那种.下面直接上代码. html: 复制代码 代码如下: //route 为后端接口 //upload/avatar 为上传的头像的保存地址 //imgurl=/upload/avatar/<?=$uid?> 这里最后的<?=$uid?>是为了后面用js实现即时显示最新的更换后的头像用的,参照下面的js部分的代码 //头像保存名称为uid.type,如1.jpg,2.png等 //$user['avatar'] 用户如果上传过

  • JS仿flash上传头像效果实现代码

    先看效果图(在FF下): 下面是我整个的思路: 一.用ajaxFileUpload.js异步上传图片.这个不用多说了,挺简单的东西. 二.div拖拽用jquery-ui.这个也不多说了. 三.上传过图片的处理. 编辑容器是300*300,如果图片不在300*300之内,这里会生成一个maxHeight=300,maxWidth=300的缩略图, 用于设置编辑容器的背景,如果原文件为123.jpg,这里生成的文件为123.jpg.view.jpg,当然,文件123.jpg.view.jpg可能不会

  • PHP+ajaxfileupload+jcrop插件完美实现头像上传剪裁

    昨天花了点时间整合了一下头像插件 东拼西凑的成果 先来看下效果 1.先使用ajaxfileupload插件做异步上传.这个地方我本来想做个上传进度的效果,但技术有限失败了.上传按钮我还做了一个文件大小的限制,但是由于浏览器兼容性的问题,不完美在IE6--IE9之间还有很多问题需要解决 getFileSize函数是用于判断文件大小的函数 复制代码 代码如下: function getFileSize(fileName) {                var byteSize = 0;     

随机推荐