AlloyPhoto 图像处理,兼容手机端、allouimage.js - 图1
    AlloyPhoto 图像处理,兼容手机端、allouimage.js - 图2

    前两天接到个新的需求:用户在上传照片时需要进行美化下。
    实现处理效果如上图所示,图片上传部分就不贴出来了。
    主要运用alloyimage这个图像处理引擎(是由腾讯Web前端AlloyTeam推出的一个开源项目http://alloyteam.github.io/AlloyPhoto/

    使用到的js有zepto.js、iscroll.js、allouimage.js
    zepto.js:由于是手机端项目所以使用zepto代替jquery
    iscroll.js:在底部我有一个横向的滚动菜单,使用它来进行横向滚动
    allouimage.js:图像处理

    同事催的急也就没有写代码注释,本来说后面空了补起来,现在看来算是说空话了

    css

    1. <style>
    2. ::-webkit-scrollbar-track-piece {
    3. background-color:#f5f5f5;
    4. border-left:1px solid #d2d2d2;
    5. }
    6. ::-webkit-scrollbar {
    7. width:13px;
    8. height:13px;
    9. }
    10. ::-webkit-scrollbar-thumb {
    11. background-color:#c2c2c2;
    12. background-clip:padding-box;
    13. border:1px solid #979797;
    14. min-height:28px;
    15. }
    16. ::-webkit-scrollbar-thumb:hover {
    17. border:1px solid #636363;
    18. background-color:#929292;
    19. }
    20. html,body{
    21. height:100%;
    22. padding:0;
    23. margin:0;
    24. text-shadow:0 1px 3px rgba(0, 0, 0, 0.75);
    25. color:#fff;
    26. font-family:Microsoft Yahei,"宋体";
    27. overflow:hidden;
    28. }
    29. li{
    30. list-style:none;
    31. }
    32. h3{
    33. font-weight:normal;
    34. text-align:center;
    35. height:30px;
    36. line-height:30px;
    37. font-size:16px;
    38. }
    39. .header{
    40. border-bottom:1px solid #575C62;
    41. text-align:center;
    42. margin:0;
    43. height:40px;
    44. line-height:40px;
    45. background:#26292d;
    46. }
    47. .wrapper{
    48. border-top:1px solid #282B2F;
    49. height:100%;
    50. /*
    51. display:box;
    52. display:-webkit-box;
    53. display:-moz-box;
    54. box-orient:horizontal;
    55. -webkit-box-orient:horizontal;
    56. */
    57. }
    58. #infoMsg{
    59. position:absolute;
    60. top:170px;
    61. left:450px;
    62. z-index:9;
    63. width:100px;
    64. height:100px;
    65. text-align:center;
    66. line-height:100px;
    67. background:#333;
    68. border-radius:4px;
    69. opacity:0.7;
    70. display:none;
    71. color:#ccc;
    72. }
    73. .picWrapper{
    74. position:relative;
    75. width:100%;
    76. height:500px;
    77. }
    78. .pic{
    79. position:absolute;
    80. left:0;
    81. top:0;
    82. border:0px;
    83. 2box-shadow:6px 6px 3px #333;
    84. 2-webkit-box-shadow:6px 6px 3px #333;
    85. }
    86. .command{
    87. background:#27282b;
    88. background:-webkit-gradient(linear,left top,left bottom,from(#26292d),color-stop(50%,#27282b),to(#26292d));
    89. height:50px;
    90. border-top:1px solid #565555;
    91. border-bottom:1px solid #565555;
    92. position:fixed;
    93. bottom:0;
    94. width:100%;
    95. z-index:10;
    96. }
    97. .demoImg{
    98. float:left;
    99. margin:0px;
    100. padding-left: 0px;
    101. }
    102. .demoImg li{
    103. list-style:none;
    104. display:inline-block;
    105. margin-left:10px;
    106. vertical-align:middle;
    107. text-align:center;
    108. }
    109. .d_item{
    110. width:50px;
    111. height:50px;
    112. line-height: 50px;
    113. overflow:hidden;
    114. position:relative;
    115. }
    116. .d_item img{
    117. width:80px;
    118. height:auto;
    119. border:3px solid rgb(45,45,45);
    120. }
    121. .lab{
    122. position:absolute;
    123. bottom:0;
    124. width:100%;
    125. text-align:center;
    126. font-size:12px;
    127. }
    128. /*
    129. * botton'
    130. * */
    131. a.button{
    132. text-decoration:none;
    133. -moz-border-radius:5px;
    134. -webkit-border-radius:5px;
    135. border-radius:5px;
    136. background: #E0B324;
    137. background: -moz-linear-gradient(0% 45% 90deg,#E0B324, #FAD121, #FFE678 100%);
    138. background: -webkit-gradient(linear, 0% 0%, 0% 65%, from(#FFE678), to(#E0B324), color-stop(.2,#FAD121));
    139. color:#312f30;
    140. float:left;
    141. font-family:arial,helvetica,sans-serif;
    142. font-size:17px;
    143. font-weight:bold;
    144. padding:10px 20px;
    145. text-shadow:1px 1px 0 #ebd663;
    146. }
    147. a.button:hover{
    148. background: #D4A922;
    149. background: -moz-linear-gradient(40% 51% 90deg,#D4A922, #EBC41F, #EDD670 100%);
    150. background: -webkit-gradient(linear, 0% 0%, 0% 65%, from(#EDD670), to(#D4A922), color-stop(.2,#EBC41F));
    151. }
    152. #open{
    153. display:none;
    154. }
    155. .d_item{
    156. cursor:pointer;
    157. }
    158. </style>

    html

    1. <body>
    2. <div class="header">AlloyPhoto 简约版 1.0</div>
    3. <div class="wrapper">
    4. <div class="right">
    5. <div id="picWrapper" class="picWrapper">
    6. <img src="" class="pic" id="pic" alt="" draggable="false" style="left: 0px; top: 0px;">
    7. <div id="infoMsg" style="left: 326.5px; top: 417.5px; display: none;">
    8. 处理中
    9. </div>
    10. </div>
    11. </div>
    12. </div>
    13. <div class="command" id="h_scroll_demo">
    14. <ul class="demoImg" style="width: 1100px;border-radius: 0;">
    15. <li><a class="button" >打开图片</a></li>
    16. <li class="d_item"><div class="lab">原图</div></li>
    17. <li class="d_item"><div class="lab">美肤效果</div></li>
    18. <li class="d_item"><div class="lab">素描效果</div></li>
    19. <li class="d_item"><div class="lab">自然增强</div></li>
    20. <li class="d_item"><div class="lab">紫调效果</div></li>
    21. <li class="d_item"><div class="lab">柔焦效果</div></li>
    22. <li class="d_item"><div class="lab">复古效果</div></li>
    23. <li class="d_item"><div class="lab">黑白效果</div></li>
    24. <li class="d_item"><div class="lab">仿lomo</div></li>
    25. <li class="d_item"><div class="lab">亮白增强</div></li>
    26. <li class="d_item"><div class="lab">灰白效果</div></li>
    27. <li class="d_item"><div class="lab">灰色效果</div></li>
    28. <li class="d_item"><div class="lab">暖秋效果</div></li>
    29. <li class="d_item"><div class="lab">木雕效果</div></li>
    30. <li class="d_item"><div class="lab">粗糙效果</div></li>
    31. </ul>
    32. </div>
    33. <input type="file" name="open" id="open" class="open">
    34. </body>

    设置菜单栏可以横向滚动

    1. Z.Scroll('#h_scroll_demo',{hScroll:true,hScrollbar : false});
    2. ;(function($){
    3. var scrollCache = {},index = 1;
    4. Z.Scroll = function(selector,opts){
    5. var scroll,scrollId,$el = $(selector),
    6. options = {
    7. hScroll : false,
    8. bounce : false,
    9. lockDirection : true,
    10. useTransform: true,
    11. useTransition: false,
    12. checkDOMChanges: false,
    13. onBeforeScrollStart: function (e) {
    14. var target = e.target;
    15. while (target.nodeType != 1) target = target.parentNode;
    16. if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA')
    17. e.preventDefault();
    18. }
    19. };
    20. scrollId = $el.data('_jscroll_');
    21. //滚动组件使用频繁,缓存起来节省开销
    22. if(scrollId && scrollCache[scrollId]){
    23. scroll = scrollCache[scrollId];
    24. $.extend(scroll.scroller.options,opts)
    25. scroll.scroller.refresh();
    26. return scroll;
    27. }else{
    28. scrollId = '_jscroll_'+index++;
    29. $el.data('_jscroll_',scrollId);
    30. $.extend(options,opts);
    31. scroller = new iScroll($el[0],options);
    32. return scrollCache[scrollId] = {
    33. scroller : scroller,
    34. destroy : function(){
    35. scroller.destroy();
    36. delete scrollCache[scrollId];
    37. }
    38. };
    39. };
    40. }
    41. })(Z.$);

    给菜单栏中对应的菜单绑定相应的事件

    1. (function(){
    2. var Main = {
    3. img: null,
    4. addEvent: function(selector, eventType, func){
    5. var proName = "";
    6. switch(true){
    7. case /^\./.test(selector) :
    8. proName = "className";
    9. selector = selector.replace(".", "");
    10. break;
    11. case /^\#/.test(selector) :
    12. proName = "id";
    13. selector = selector.replace("#", "");
    14. break;
    15. default:
    16. proName = "tagName";
    17. }
    18. document.body.addEventListener(eventType,function(e){
    19. function check(node){
    20. if(! node.parentNode) return;
    21. if(node[proName] == selector){
    22. func.call(node, e);
    23. };
    24. check(node.parentNode);
    25. }
    26. check(e.target);
    27. }, false);
    28. },
    29. eventAtt: function(){
    30. var _this = this;
    31. var clickFlag = 0, dx, dy, left, top;
    32. this.addEvent(".pic", "mousedown", function(e){
    33. /*
    34. dx = e.offsetX ? e.offsetX : e.layerX;
    35. dy = e.offsetY ? e.offsetY : e.layerY;
    36. */
    37. dx = e.clientX;
    38. dy = e.clientY;
    39. left = parseInt(pic.style.left);
    40. top = parseInt(pic.style.top);
    41. clickFlag = 1;
    42. });
    43. this.addEvent(".picWrapper", "mouseup", function(e){
    44. clickFlag = 0;
    45. });
    46. document.getElementById("picWrapper").onmousemove = function(e){
    47. /*
    48. var x = e.offsetX ? e.offsetX : e.layerX;
    49. var y = e.offsetY ? e.offsetY : e.layerY;
    50. */
    51. var x = e.clientX;
    52. var y = e.clientY;
    53. if(clickFlag){
    54. var pic = document.getElementById("pic");
    55. /*
    56. var x = e.offsetX ? e.offsetX : e.layerX;
    57. var y = e.offsetY ? e.offsetY : e.layerY;
    58. */
    59. var x = e.clientX;
    60. var y = e.clientY;
    61. var rLeft = left + (x - dx);
    62. var rTop = top + (y - dy);
    63. if(rLeft < 0) rLeft = 0;
    64. if(rTop < 0) rTop = 0;
    65. pic.style.left = rLeft + "px";
    66. pic.style.top = rTop + "px";
    67. }
    68. };
    69. this.addEvent(".button", "click", function(e){
    70. document.getElementById("open").click();
    71. });
    72. this.addEvent(".open", "change", function(e){
    73. _this.openFile(e.target.files[0]);
    74. });
    75. this.addEvent(".lab", "click", function(e){
    76. var text = this.childNodes[0].nodeValue.replace("效果","");
    77. var img = document.getElementById("pic");
    78. var AP = _this.img.clone();
    79. if(text == "原图") AP.replace(img);
    80. else{
    81. msgEle.style.display = "block";
    82. setTimeout(function(){
    83. var t = + new Date();
    84. AP.ps(text).replace(img).complete(function(){
    85. console.log(text + ":" + (+ new Date() - t) / 1000 + "s");
    86. msgEle.style.display = "none";
    87. });
    88. }, 2);
    89. }
    90. });
    91. document.body.addEventListener("drop", function(e){
    92. e.preventDefault();
    93. var fileList = e.dataTransfer.files;
    94. _this.openFile(fileList[0]);
    95. },false);
    96. window.onresize = function(){
    97. _this.initView();
    98. };
    99. },
    100. init: function(){
    101. this.initView();
    102. this.eventAtt();
    103. var _this = this;
    104. var pic = document.getElementById("pic");
    105. pic.onload = function(){
    106. _this.img = AlloyImage(this);
    107. _this.initView();
    108. };
    109. },
    110. initView: function(){
    111. var func = function(){
    112. var w_width = parseInt(document.body.clientWidth) - 250;
    113. var w_height = document.body.clientHeight;
    114. var p_width = this.width;
    115. var p_height = this.height;
    116. var left = (parseInt(w_width) - parseInt(p_width)) / 2;
    117. var top = (parseInt(w_height) - parseInt(p_height)) / 2;
    118. top = top < 0 ? 0 : top;
    119. left = left < 0 ? 0 : left;
    120. this.style.left = left + "px";
    121. this.style.top = top + "px";
    122. msgEle.style.left = (parseInt(w_width)) + "px";
    123. msgEle.style.top = (parseInt(w_height) - 100) / 2 + "px";
    124. };
    125. func.call(document.getElementById("pic"));
    126. var height = document.body.clientHeight;
    127. //var left = document.querySelector(".left");
    128. var leftHeight = height - 143;
    129. //left.style.height = leftHeight + "px";
    130. },
    131. openFile: function(fileUrl){//打开图像文件
    132. var reader = new FileReader();
    133. var _this = this;
    134. reader.readAsDataURL(fileUrl);
    135. reader.onload = function(){
    136. var pic = document.getElementById("pic");
    137. pic.src = this.result;
    138. pic.onload = function(){
    139. _this.initView();
    140. _this.img = AlloyImage(this);
    141. };
    142. };
    143. }
    144. };
    145. var msgEle;
    146. window.addEventListener("DOMContentLoaded", function(){
    147. msgEle = document.getElementById("infoMsg");
    148. //$AI.useWorker("js/combined/alloyimage.js");
    149. Main.init();
    150. var cw = parseInt(document.body.clientWidth);
    151. document.getElementById("pic").style.width=cw+"px";
    152. }, false);
    153. })();

    源码 http://download.csdn.net/detail/u012793146/8856819