用canvas裁剪图片

  1 var selectObj = null;
  2 function ImageCrop(canvasId, imageSource, x, y, width, height) {
  3     var canvas = $("#" + canvasId);
  4     if (canvas.length == 0 && imageSource) {
  5         return;
  6     }
  7
  8     function canvasMouseDown(e) {
  9         StopSelect(e);
 10         canvas.css("cursor", "default");
 11     }
 12
 13     function canvasMouseMove(e) {
 14         var canvasOffset = canvas.offset();
 15         var pageX = e.pageX || event.targetTouches[0].pageX;
 16         var pageY = e.pageY || event.targetTouches[0].pageY;
 17         iMouseX = Math.floor(pageX - canvasOffset.left);
 18         iMouseY = Math.floor(pageY - canvasOffset.top);
 19
 20         canvas.css("cursor", "default");
 21         if (selectObj.bDragAll) {
 22             canvas.css("cursor", "move");
 23             canvas.data("drag", true);
 24             var cx = iMouseX - selectObj.px;
 25             cx = cx < 0 ? 0 : cx;
 26             mx = ctx.canvas.width - selectObj.w;
 27             cx = cx > mx ? mx : cx;
 28             selectObj.x = cx;
 29             var cy = iMouseY - selectObj.py;
 30             cy = cy < 0 ? 0 : cy;
 31             my = ctx.canvas.height - selectObj.h;
 32             cy = cy > my ? my : cy;
 33             selectObj.y = cy;
 34         }
 35         for (var i = 0; i < 4; i++) {
 36             selectObj.bHow[i] = false;
 37             selectObj.iCSize[i] = selectObj.csize;
 38         }
 39
 40         // hovering over resize cubes
 41         if (iMouseX > selectObj.x - selectObj.csizeh && iMouseX < selectObj.x + selectObj.csizeh &&
 42             iMouseY > selectObj.y - selectObj.csizeh && iMouseY < selectObj.y + selectObj.csizeh) {
 43             canvas.css("cursor", "pointer");
 44             selectObj.bHow[0] = true;
 45             selectObj.iCSize[0] = selectObj.csizeh;
 46         }
 47         if (iMouseX > selectObj.x + selectObj.w - selectObj.csizeh && iMouseX < selectObj.x + selectObj.w + selectObj.csizeh &&
 48             iMouseY > selectObj.y - selectObj.csizeh && iMouseY < selectObj.y + selectObj.csizeh) {
 49             canvas.css("cursor", "pointer");
 50
 51             selectObj.bHow[1] = true;
 52             selectObj.iCSize[1] = selectObj.csizeh;
 53         }
 54         if (iMouseX > selectObj.x + selectObj.w - selectObj.csizeh && iMouseX < selectObj.x + selectObj.w + selectObj.csizeh &&
 55             iMouseY > selectObj.y + selectObj.h - selectObj.csizeh && iMouseY < selectObj.y + selectObj.h + selectObj.csizeh) {
 56             canvas.css("cursor", "pointer");
 57
 58             selectObj.bHow[2] = true;
 59             selectObj.iCSize[2] = selectObj.csizeh;
 60         }
 61         if (iMouseX > selectObj.x - selectObj.csizeh && iMouseX < selectObj.x + selectObj.csizeh &&
 62             iMouseY > selectObj.y + selectObj.h - selectObj.csizeh && iMouseY < selectObj.y + selectObj.h + selectObj.csizeh) {
 63             canvas.css("cursor", "pointer");
 64
 65             selectObj.bHow[3] = true;
 66             selectObj.iCSize[3] = selectObj.csizeh;
 67         }
 68
 69         if (iMouseX > selectObj.x && iMouseX < selectObj.x + selectObj.w && iMouseY > selectObj.y && iMouseY < selectObj.y + selectObj.h) {
 70             canvas.css("cursor", "move");
 71         }
 72
 73         // in case of dragging of resize cubes
 74         var iFW, iFH, iFX, iFY, mx, my;
 75         if (selectObj.bDrag[0]) {
 76             iFX = iMouseX - selectObj.px;
 77             iFY = iMouseY - selectObj.py;
 78             iFW = selectObj.w + selectObj.x - iFX;
 79             iFH = selectObj.h + selectObj.y - iFY;
 80             canvas.data("drag", true);
 81         }
 82         if (selectObj.bDrag[1]) {
 83             iFX = selectObj.x;
 84             iFY = iMouseY - selectObj.py;
 85             iFW = iMouseX - selectObj.px - iFX;
 86             iFH = selectObj.h + selectObj.y - iFY;
 87             canvas.data("drag", true);
 88         }
 89         if (selectObj.bDrag[2]) {
 90             iFX = selectObj.x;
 91             iFY = selectObj.y;
 92             iFW = iMouseX - selectObj.px - iFX;
 93             iFH = iMouseY - selectObj.py - iFY;
 94             canvas.data("drag", true);
 95         }
 96         if (selectObj.bDrag[3]) {
 97             iFX = iMouseX - selectObj.px;
 98             iFY = selectObj.y;
 99             iFW = selectObj.w + selectObj.x - iFX;
100             iFH = iMouseY - selectObj.py - iFY;
101             canvas.data("drag", true);
102         }
103
104         if (iFW > selectObj.csizeh * 2 && iFH > selectObj.csizeh * 2) {
105             selectObj.w = iFW;
106             selectObj.h = iFH;
107             selectObj.x = iFX;
108             selectObj.y = iFY;
109         }
110         drawScene();
111     }
112
113     function canvasMouseOut() {
114         $(canvas).trigger("mouseup");
115     }
116
117     function canvasMouseUp() {
118         selectObj.bDragAll = false;
119         for (var i = 0; i < 4; i++) {
120             selectObj.bDrag[i] = false;
121         }
122         canvas.css("cursor", "default");
123         canvas.data("select", {
124             x: selectObj.x,
125             y: selectObj.y,
126             w: selectObj.w,
127             h: selectObj.h
128         });
129         selectObj.px = 0;
130         selectObj.py = 0;
131     }
132
133     function Selection(x, y, w, h) {
134         this.x = x; // initial positions
135         this.y = y;
136         this.w = w; // and size
137         this.h = h;
138
139         this.px = x; // extra variables to dragging calculations
140         this.py = y;
141
142         this.csize = 4; // resize cubes size
143         this.csizeh = 6; // resize cubes size (on hover)
144
145         this.bHow = [false, false, false, false]; // hover statuses
146         this.iCSize = [this.csize, this.csize, this.csize, this.csize]; // resize cubes sizes
147         this.bDrag = [false, false, false, false]; // drag statuses
148         this.bDragAll = false; // drag whole selection
149     }
150
151     Selection.prototype.draw = function () {
152         ctx.strokeStyle = ‘#666‘;
153         ctx.lineWidth = 2;
154         ctx.strokeRect(this.x, this.y, this.w, this.h);
155
156         // draw part of original image
157         if (this.w > 0 && this.h > 0) {
158             ctx.drawImage(image, this.x, this.y, this.w, this.h, this.x, this.y, this.w, this.h);
159         }
160
161         // draw resize cubes
162         ctx.fillStyle = ‘#999‘;
163         ctx.fillRect(this.x - this.iCSize[0], this.y - this.iCSize[0], this.iCSize[0] * 2, this.iCSize[0] * 2);
164         ctx.fillRect(this.x + this.w - this.iCSize[1], this.y - this.iCSize[1], this.iCSize[1] * 2, this.iCSize[1] * 2);
165         ctx.fillRect(this.x + this.w - this.iCSize[2], this.y + this.h - this.iCSize[2], this.iCSize[2] * 2, this.iCSize[2] * 2);
166         ctx.fillRect(this.x - this.iCSize[3], this.y + this.h - this.iCSize[3], this.iCSize[3] * 2, this.iCSize[3] * 2);
167     };
168
169     var drawScene = function () {
170         ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); // clear canvas
171
172         // draw source image
173         ctx.drawImage(image, 0, 0, ctx.canvas.width, ctx.canvas.height);
174
175         // and make it darker
176         ctx.fillStyle = ‘rgba(0, 0, 0, 0.5)‘;
177         ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
178
179         // draw selection
180         selectObj.draw();
181         canvas.mousedown(canvasMouseDown);
182         canvas.on("touchstart", canvasMouseDown);
183     };
184
185     var createSelection = function (x, y, width, height) {
186         var content = $("#imagePreview");
187         x = x || Math.ceil((content.width() - width) / 2);
188         y = y || Math.ceil((content.height() - height) / 2);
189         return new Selection(x, y, width, height);
190     };
191
192     var ctx = canvas[0].getContext("2d");
193     var iMouseX = 1;
194     var iMouseY = 1;
195     var image = new Image();
196     image.onload = function () {
197         selectObj = createSelection(x, y, width, height);
198         canvas.data("select", {
199             x: selectObj.x,
200             y: selectObj.y,
201             w: selectObj.w,
202             h: selectObj.h
203         });
204         drawScene();
205     };
206     image.src = imageSource;
207
208     canvas.mousemove(canvasMouseMove);
209     canvas.on("touchmove", canvasMouseMove);
210
211     var StopSelect = function (e) {
212         var canvasOffset = $(canvas).offset();
213         var pageX = e.pageX || event.targetTouches[0].pageX;
214         var pageY = e.pageY || event.targetTouches[0].pageY;
215         iMouseX = Math.floor(pageX - canvasOffset.left);
216         iMouseY = Math.floor(pageY - canvasOffset.top);
217
218         selectObj.px = iMouseX - selectObj.x;
219         selectObj.py = iMouseY - selectObj.y;
220
221         if (selectObj.bHow[0]) {
222             selectObj.px = iMouseX - selectObj.x;
223             selectObj.py = iMouseY - selectObj.y;
224         }
225         if (selectObj.bHow[1]) {
226             selectObj.px = iMouseX - selectObj.x - selectObj.w;
227             selectObj.py = iMouseY - selectObj.y;
228         }
229         if (selectObj.bHow[2]) {
230             selectObj.px = iMouseX - selectObj.x - selectObj.w;
231             selectObj.py = iMouseY - selectObj.y - selectObj.h;
232         }
233         if (selectObj.bHow[3]) {
234             selectObj.px = iMouseX - selectObj.x;
235             selectObj.py = iMouseY - selectObj.y - selectObj.h;
236         }
237
238         if (iMouseX > selectObj.x + selectObj.csizeh &&
239             iMouseX < selectObj.x + selectObj.w - selectObj.csizeh &&
240             iMouseY > selectObj.y + selectObj.csizeh &&
241             iMouseY < selectObj.y + selectObj.h - selectObj.csizeh) {
242             selectObj.bDragAll = true;
243         }
244
245         for (var i = 0; i < 4; i++) {
246             if (selectObj.bHow[i]) {
247                 selectObj.bDrag[i] = true;
248             }
249         }
250     };
251     canvas.mouseout(canvasMouseOut);
252     canvas.mouseup(canvasMouseUp);
253     canvas.on("touchend", canvasMouseUp);
254
255     this.getImageData = function (previewID) {
256         var tmpCanvas = $("<canvas></canvas>")[0];
257         var tmpCtx = tmpCanvas.getContext("2d");
258         if (tmpCanvas && selectObj) {
259             tmpCanvas.width = selectObj.w;
260             tmpCanvas.height = selectObj.h;
261             tmpCtx.drawImage(image, selectObj.x, selectObj.y, selectObj.w, selectObj.h, 0, 0, selectObj.w, selectObj.h);
262             if (document.getElementById(previewID)) {
263                 document.getElementById(previewID).src = tmpCanvas.toDataURL();
264                 document.getElementById(previewID).style.border = "1px solid #ccc";
265             }
266             return tmpCanvas.toDataURL();
267         }
268     };
269 }
270
271 function autoResizeImage(maxWidth, maxHeight, objImg) {
272     var img = new Image();
273     img.src = objImg.src;
274     var hRatio;
275     var wRatio;
276     var ratio = 1;
277     var w = objImg.width;
278     var h = objImg.height;
279     wRatio = maxWidth / w;
280     hRatio = maxHeight / h;
281     if (w < maxWidth && h < maxHeight) {
282         return;
283     }
284     if (maxWidth == 0 && maxHeight == 0) {
285         ratio = 1;
286     } else if (maxWidth == 0) {
287         if (hRatio < 1) {
288             ratio = hRatio;
289         }
290     } else if (maxHeight == 0) {
291         if (wRatio < 1) {
292             ratio = wRatio;
293         }
294     } else if (wRatio < 1 || hRatio < 1) {
295         ratio = (wRatio <= hRatio ? wRatio : hRatio);
296     } else {
297         ratio = (wRatio <= hRatio ? wRatio : hRatio) - Math.floor(wRatio <= hRatio ? wRatio : hRatio);
298     }
299     if (ratio < 1) {
300         if (ratio < 0.5 && w < maxWidth && h < maxHeight) {
301             ratio = 1 - ratio;
302         }
303         w = w * ratio;
304         h = h * ratio;
305     }
306     objImg.height = h;
307     objImg.width = w;
308 }
时间: 2024-12-11 11:29:56

用canvas裁剪图片的相关文章

使用canvas进行图片裁剪简单功能

1.html部分 使用一个input[type="file"]进行图片上传: canvas进行图片的裁剪展示 <div> <input type="file" id="imgFile"> </div> <div id="demoBox" style="width: 300px;height: 300px;position: absolute;left: 300px;top:

Android拍照、相册选取、裁剪图片

来自:http://blog.csdn.net/ryantang03/article/details/8656278 package com.example.listactivity; import java.io.ByteArrayOutputStream; import java.io.File; import com.example.model.ImageTools; import android.app.Activity; import android.app.AlertDialog;

HTML5 本地裁剪图片并上传至服务器(转)

很多情况下用户上传的图片都需要经过裁剪,比如头像啊什么的.但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁剪坐标,发送给服务器,服务器裁剪完再返回给用户,来回需要 5 步.步骤繁琐不说,当很多用户上传图片的时候也很影响服务器性能. HTML5 的出现让我们可以更方便的实现这一需求.虽然这里所说的技术都貌似有点过时了(前端界的“过时”,你懂的),但还是有些许参考价值.在这里我只说一下要点,具体实现同学们慢慢研究. 下面奉上我自己写的一个demo,在输入框中选好

网页裁剪图片(FileAPI)

网页端裁剪图片(FileAPI),兼容谷歌火狐IE6/7/8.高版本浏览器用canvas 裁剪,低版本用flash过度.FileAPI 的应用实例. 效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6

HTML5 本地裁剪图片

最近有时间了解了下html5的各API,发现新浪微博的头像设置是使用canvas实现截图的,加之前段时间了解了下html5的File API使用File API 之FileReader实现文件上传更加觉得html5好玩了,想着也试试写写这功能权当学习canvas吧. 下面奉上我自己写的一个demo,代码写得比较少,很多细节不会处理.如果有不得当的地方恳请指教,谢谢啦 ^_^ ^_^ 功能实现步奏: 一:获取文件,读取文件并生成url 二:根据容器的大小使用canvas绘制图片 三:使用canva

cropper.js裁剪图片的使用

这两天难得有时间可以整理一下最近学习的东西,这两天项目中用到了头像上传裁剪的功能,这里只介绍头像的裁剪吧. 单独实现图片剪裁的功能还是挺容易的,入门级别的.看一遍官方给的文档,基本上就明白了.大家如果不想看英文版的官网介绍,可以看这篇博客,讲的特别好. 官网地址:http://fengyuanchen.github.io/cropper/ 附上网址:https://blog.csdn.net/weixin_38023551/article/details/78792400 这篇文章讲的很好. 我

微信小程序裁剪图片成圆形

代码地址如下:http://www.demodashi.com/demo/14453.html 前言 最近在开发小程序,产品经理提了一个需求,要求微信小程序换头像,用户剪裁图片必须是圆形,也在github上看了一些例子,一般剪裁图片用的都是方形,所以自己打算写一个小组件,可以把图片剪裁成圆形,主要思路就是使用canvas绘图,把剪裁的图片绘制成圆形,另外剪裁图片的窗口还可以移动放大缩小,这个功能就用了微信组件movable-view,好了,该说的也说完了,下面咱们开始撸代码. movable-v

自定义裁剪图片

判断图片大小,根据裁剪目标对图片进行缩放,然后裁剪图片(在图像不变形情况下) /// <summary> /// 指定长宽裁剪 /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸 /// </summary> /// <param name="fromFile">原图Stream对象</param> /// <param name="fileSaveUrl">保存路径</param> ///

快速解决Canvas.toDataURL 图片跨域的问题

出现Canvas.toDataURL 图片跨域问题怎么解决呢?下面小编就为大家带来一篇Canvas.toDataURL 图片跨域问题的快速解决方法.一起跟随小编过来看看吧 如题,在将页面的图片地址进行本地输出时(Html2Canvas.js),因不同源存在跨域问题,会出现toDataURL访问权限问题: [Redirect at origin 'http://sub1.xx.com' has been blocked from loading by Cross-Origin Resource S