HTML5canvas achieves the effect of dragging and dropping the uploaded Avatar on the Mobile End _ html5 tutorial tips-

Source: Internet
Author: User
This article describes how to use HTML5canvas to drag and drop a portrait on a mobile terminal and crop it. It is of reference value. If you are interested, you can refer to this example to use HTML5 canvas, you have simply compiled the cropping effect of the uploaded avatar. You can drag and drop the Avatar to crop it. Although the style is not easy to see, the function is still complete:

For the effect after cropping:

Html section:

Copy XML/HTML Code to clipboard

  1. Upload Avatar
  2. Save
  3. The following is the cut image:

JavaScript section:

Copy the content to the clipboard using JavaScript Code

  1. Var $ imgCrop = $ ("# imgCrop ");
  2. Var $ img = $ imgCrop. find ("img ");
  3. Var img = $ img [0];
  4. Var width = parseInt($imgCrop.css ("width "));
  5. Var height = parseInt($imgCrop.css ("height "));
  6. Var startX, startY, scale = 1;
  7. Var x = 0, y = 0;
  8. $ ("Input"). on ("change", function (){
  9. Var fr = new FileReader ();
  10. Var file = this. files [0]
  11. // Console. log (file );
  12. If (! /Image \/\ w +/. test (file. type )){
  13. Alert (file. name + "is not an image file! ");
  14. Return;
  15. }
  16. Console. log (file );
  17. $ Img. removeAttr ("height width ");
  18. Fr. readAsDataURL (file );
  19. Fr. onload = function (){
  20. Img. src = fr. result;
  21. Var widthInit = img. width;
  22. If (img. width> img. height ){
  23. Img. height = height;
  24. X = (width-img. width)/2;
  25. Y = 0;
  26. } Else {
  27. Img. width = width;
  28. X = 0;
  29. Y = (height-img. height)/2;
  30. }
  31. Scale = widthInit/img. width;
  32. Move ($ img, x, y );
  33. };
  34. });
  35. Img. addEventListener ("touchstart", function (e ){
  36. StartX = e.tar getTouches [0]. pageX;
  37. StartY = e.tar getTouches [0]. pageY;
  38. Return;
  39. });
  40. Img. addEventListener ("touchmove", function (e ){
  41. E. preventDefault ();
  42. E. stopPropagation ();
  43. Var changeX = e. changedTouches [0]. pageX-startX + x;
  44. Var changeY = e. changedTouches [0]. pageY-startY + y;
  45. Move ($ (this), changeX, changeY );
  46. Return;
  47. });
  48. Img. addEventListener ("touchend", function (e ){
  49. Var changeX = e. changedTouches [0]. pageX-startX + x;
  50. Var changeY = e. changedTouches [0]. pageY-startY + y;
  51. X = x + e. changedTouches [0]. pageX-startX;
  52. Y = y + e. changedTouches [0]. pageY-startY;
  53. Move ($ (this), changeX, changeY );
  54. Return;
  55. });
  56. // Determine the target image Style
  57. Function move (ele, x, y ){
  58. Ele.css ({
  59. '-Webkit-transform': 'translate3d ('+ x + 'px,' + y + 'px, 0 )',
  60. 'Transform': 'translate3d ('+ x + 'px,' + y + 'px, 0 )'
  61. });
  62. }
  63. $ ("# Save"). on ("click", function (){
  64. Var url = imageData ($ img );
  65. Console. log (url );
  66. $ ("# ImgShow" ).html ("+ url +"/> ");;
  67. });
  68. // Crop an image
  69. Function imageData ($ img ){
  70. Var canvas = document. createElement ('canvas ');
  71. Var ctx = canvas. getContext ('2d ');
  72. Canvas. width = width;
  73. Canvas. height = height;
  74. Ctx. drawImage (img,-x * scale,-y * scale, width * scale, height * scale, 0, 0, width, height );
  75. Return canvas. toDataURL ();
  76. }

The above is all the content of this article, hoping to help you learn.

Original article: http://www.cnblogs.com/yifengBlog/p/5265598.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.