vue移动端裁剪图片结合插件Cropper的使用实例代码


Posted in Javascript onJuly 10, 2017

之前写了一个上传头像的功能模块,以下的内容是描述上传头像过程中裁剪图片插件结合vue的一个使用。

当然,使用就安装 npm install cropperjs

接着再引入 import Cropper from 'cropperjs'

下面是源码

<template> 
 <div id="demo"> 
 <!-- 遮罩层 --> 
 <div class="container" v-show="panel"> 
  <div> 
  <img id="image" :src="url" alt="Picture"> 
  </div> 
 
  <button type="button" id="button" @click="crop">确定</button> 
   
 </div> 
 
 <div style="padding:20px;"> 
  <div class="show"> 
   <div class="picture" :style="'backgroundImage:url('+headerImage+')'"> 
   </div> 
  </div> 
  <div style="margin-top:20px;"> 
   <input type="file" id="change" accept="image" @change="change"> 
   <label for="change"></label> 
  </div> 
   
 </div> 
 </div> 
</template> 
 
<script> 
import Cropper from 'cropperjs' 
export default { 
 components: { 
  
 }, 
 data () { 
 return { 
  headerImage:'', 
  picValue:'', 
  cropper:'', 
  croppable:false, 
  panel:false, 
  url:'' 
 } 
 }, 
 mounted () { 
 //初始化这个裁剪框 
 var self = this; 
 var image = document.getElementById('image'); 
 this.cropper = new Cropper(image, { 
  aspectRatio: 1, 
  viewMode: 1, 
  background:false, 
  zoomable:false, 
  ready: function () { 
  self.croppable = true; 
  } 
 }); 
 }, 
 methods: { 
 getObjectURL (file) { 
  var url = null ; 
  if (window.createObjectURL!=undefined) { // basic 
  url = window.createObjectURL(file) ; 
  } else if (window.URL!=undefined) { // mozilla(firefox) 
  url = window.URL.createObjectURL(file) ; 
  } else if (window.webkitURL!=undefined) { // webkit or chrome 
  url = window.webkitURL.createObjectURL(file) ; 
  } 
  return url ; 
 }, 
 change (e) { 
  let files = e.target.files || e.dataTransfer.files; 
  if (!files.length) return; 
  this.panel = true; 
  this.picValue = files[0]; 
  
  this.url = this.getObjectURL(this.picValue); 
  //每次替换图片要重新得到新的url 
  if(this.cropper){ 
  this.cropper.replace(this.url); 
  } 
  this.panel = true; 
 
 }, 
 crop () { 
  this.panel = false; 
  var croppedCanvas; 
  var roundedCanvas; 
 
  if (!this.croppable) { 
   return; 
  } 
  // Crop 
  croppedCanvas = this.cropper.getCroppedCanvas(); 
  console.log(this.cropper) 
  // Round 
  roundedCanvas = this.getRoundedCanvas(croppedCanvas); 
 
  this.headerImage = roundedCanvas.toDataURL(); 
  this.postImg() 
   
 }, 
 getRoundedCanvas (sourceCanvas) { 
  
  var canvas = document.createElement('canvas'); 
  var context = canvas.getContext('2d'); 
  var width = sourceCanvas.width; 
  var height = sourceCanvas.height; 
  
  canvas.width = width; 
  canvas.height = height; 
 
  context.imageSmoothingEnabled = true; 
  context.drawImage(sourceCanvas, 0, 0, width, height); 
  context.globalCompositeOperation = 'destination-in'; 
  context.beginPath(); 
  context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true); 
  context.fill(); 
 
  return canvas; 
 }, 
 postImg () { 
  //这边写图片的上传 
 } 
 } 
} 
</script> 
 
<style> 
*{ 
 margin: 0; 
 padding: 0; 
} 
#demo #button { 
 position: absolute; 
 right: 10px; 
 top: 10px; 
 width: 80px; 
 height: 40px; 
 border:none; 
 border-radius: 5px; 
 background:white; 
} 
#demo .show { 
 width: 100px; 
 height: 100px; 
 overflow: hidden; 
 position: relative; 
 border-radius: 50%; 
 border: 1px solid #d5d5d5; 
} 
#demo .picture { 
 width: 100%; 
 height: 100%; 
 overflow: hidden; 
 background-position: center center; 
 background-repeat: no-repeat; 
 background-size: cover; 
} 
#demo .container { 
 z-index: 99; 
 position: fixed; 
 padding-top: 60px; 
 left: 0; 
 top: 0; 
 right: 0; 
 bottom: 0; 
 background:rgba(0,0,0,1); 
} 
 
#demo #image { 
 max-width: 100%; 
} 
 
.cropper-view-box,.cropper-face { 
 border-radius: 50%; 
} 
/*! 
 * Cropper.js v1.0.0-rc 
 * https://github.com/fengyuanchen/cropperjs 
 * 
 * Copyright (c) 2017 Fengyuan Chen 
 * Released under the MIT license 
 * 
 * Date: 2017-03-25T12:02:21.062Z 
 */ 
 
.cropper-container { 
 font-size: 0; 
 line-height: 0; 
 
 position: relative; 
 
 -webkit-user-select: none; 
 
  -moz-user-select: none; 
 
  -ms-user-select: none; 
 
   user-select: none; 
 
 direction: ltr; 
 -ms-touch-action: none; 
  touch-action: none 
} 
 
.cropper-container img { 
 /* Avoid margin top issue (Occur only when margin-top <= -height) */ 
 display: block; 
 min-width: 0 !important; 
 max-width: none !important; 
 min-height: 0 !important; 
 max-height: none !important; 
 width: 100%; 
 height: 100%; 
 image-orientation: 0deg 
} 
 
.cropper-wrap-box, 
.cropper-canvas, 
.cropper-drag-box, 
.cropper-crop-box, 
.cropper-modal { 
 position: absolute; 
 top: 0; 
 right: 0; 
 bottom: 0; 
 left: 0; 
} 
 
.cropper-wrap-box { 
 overflow: hidden; 
} 
 
.cropper-drag-box { 
 opacity: 0; 
 background-color: #fff; 
} 
 
.cropper-modal { 
 opacity: .5; 
 background-color: #000; 
} 
 
.cropper-view-box { 
 display: block; 
 overflow: hidden; 
 
 width: 100%; 
 height: 100%; 
 
 outline: 1px solid #39f; 
 outline-color: rgba(51, 153, 255, 0.75); 
} 
 
.cropper-dashed { 
 position: absolute; 
 
 display: block; 
 
 opacity: .5; 
 border: 0 dashed #eee 
} 
 
.cropper-dashed.dashed-h { 
 top: 33.33333%; 
 left: 0; 
 width: 100%; 
 height: 33.33333%; 
 border-top-width: 1px; 
 border-bottom-width: 1px 
} 
 
.cropper-dashed.dashed-v { 
 top: 0; 
 left: 33.33333%; 
 width: 33.33333%; 
 height: 100%; 
 border-right-width: 1px; 
 border-left-width: 1px 
} 
 
.cropper-center { 
 position: absolute; 
 top: 50%; 
 left: 50%; 
 
 display: block; 
 
 width: 0; 
 height: 0; 
 
 opacity: .75 
} 
 
.cropper-center:before, 
 .cropper-center:after { 
 position: absolute; 
 display: block; 
 content: ' '; 
 background-color: #eee 
} 
 
.cropper-center:before { 
 top: 0; 
 left: -3px; 
 width: 7px; 
 height: 1px 
} 
 
.cropper-center:after { 
 top: -3px; 
 left: 0; 
 width: 1px; 
 height: 7px 
} 
 
.cropper-face, 
.cropper-line, 
.cropper-point { 
 position: absolute; 
 
 display: block; 
 
 width: 100%; 
 height: 100%; 
 
 opacity: .1; 
} 
 
.cropper-face { 
 top: 0; 
 left: 0; 
 
 background-color: #fff; 
} 
 
.cropper-line { 
 background-color: #39f 
} 
 
.cropper-line.line-e { 
 top: 0; 
 right: -3px; 
 width: 5px; 
 cursor: e-resize 
} 
 
.cropper-line.line-n { 
 top: -3px; 
 left: 0; 
 height: 5px; 
 cursor: n-resize 
} 
 
.cropper-line.line-w { 
 top: 0; 
 left: -3px; 
 width: 5px; 
 cursor: w-resize 
} 
 
.cropper-line.line-s { 
 bottom: -3px; 
 left: 0; 
 height: 5px; 
 cursor: s-resize 
} 
 
.cropper-point { 
 width: 5px; 
 height: 5px; 
 
 opacity: .75; 
 background-color: #39f 
} 
 
.cropper-point.point-e { 
 top: 50%; 
 right: -3px; 
 margin-top: -3px; 
 cursor: e-resize 
} 
 
.cropper-point.point-n { 
 top: -3px; 
 left: 50%; 
 margin-left: -3px; 
 cursor: n-resize 
} 
 
.cropper-point.point-w { 
 top: 50%; 
 left: -3px; 
 margin-top: -3px; 
 cursor: w-resize 
} 
 
.cropper-point.point-s { 
 bottom: -3px; 
 left: 50%; 
 margin-left: -3px; 
 cursor: s-resize 
} 
 
.cropper-point.point-ne { 
 top: -3px; 
 right: -3px; 
 cursor: ne-resize 
} 
 
.cropper-point.point-nw { 
 top: -3px; 
 left: -3px; 
 cursor: nw-resize 
} 
 
.cropper-point.point-sw { 
 bottom: -3px; 
 left: -3px; 
 cursor: sw-resize 
} 
 
.cropper-point.point-se { 
 right: -3px; 
 bottom: -3px; 
 width: 20px; 
 height: 20px; 
 cursor: se-resize; 
 opacity: 1 
} 
 
@media (min-width: 768px) { 
 
 .cropper-point.point-se { 
 width: 15px; 
 height: 15px 
 } 
} 
 
@media (min-width: 992px) { 
 
 .cropper-point.point-se { 
 width: 10px; 
 height: 10px 
 } 
} 
 
@media (min-width: 1200px) { 
 
 .cropper-point.point-se { 
 width: 5px; 
 height: 5px; 
 opacity: .75 
 } 
} 
 
.cropper-point.point-se:before { 
 position: absolute; 
 right: -50%; 
 bottom: -50%; 
 display: block; 
 width: 200%; 
 height: 200%; 
 content: ' '; 
 opacity: 0; 
 background-color: #39f 
} 
 
.cropper-invisible { 
 opacity: 0; 
} 
 
.cropper-bg { 
 background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA3NCSVQICAjb4U/gAAAABlBMVEXMzMz////TjRV2AAAACXBIWXMAAArrAAAK6wGCiw1aAAAAHHRFWHRTb2Z0d2FyZQBBZG9iZSBGaXJld29ya3MgQ1M26LyyjAAAABFJREFUCJlj+M/AgBVhF/0PAH6/D/HkDxOGAAAAAElFTkSuQmCC'); 
} 
 
.cropper-hide { 
 position: absolute; 
 
 display: block; 
 
 width: 0; 
 height: 0; 
} 
 
.cropper-hidden { 
 display: none !important; 
} 
 
.cropper-move { 
 cursor: move; 
} 
 
.cropper-crop { 
 cursor: crosshair; 
} 
 
.cropper-disabled .cropper-drag-box, 
.cropper-disabled .cropper-face, 
.cropper-disabled .cropper-line, 
.cropper-disabled .cropper-point { 
 cursor: not-allowed; 
} 
 
 
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Ucren Virtual Desktop V2.0
Nov 07 Javascript
Jsonp 跨域的原理以及Jquery的解决方案
May 18 Javascript
ModelDialog JavaScript模态对话框类代码
Apr 17 Javascript
一款jquery特效编写的大度宽屏焦点图切换特效的实例代码
Aug 05 Javascript
js 实现 input type=&quot;file&quot; 文件上传示例代码
Aug 07 Javascript
浏览器页面区域大小的js获取方法
Sep 21 Javascript
js获取页面传来参数的方法
Sep 06 Javascript
javascript中函数作为参数调用的方法
Feb 09 Javascript
React利用插件和不用插件实现双向绑定的方法详解
Jul 03 Javascript
Angular 2父子组件之间共享服务通信的实现
Jul 04 Javascript
vue左右侧联动滚动的实现代码
Jun 06 Javascript
微信小程序自定义tabBar的踩坑实践记录
Nov 06 Javascript
vue轮播图插件vue-awesome-swiper的使用代码实例
Jul 10 #Javascript
Javascript调试之console对象——你不知道的一些小技巧
Jul 10 #Javascript
vue实现表格数据的增删改查
Jul 10 #Javascript
激动人心的 Angular HttpClient的源码解析
Jul 10 #Javascript
基于JS实现仿京东搜索栏随滑动透明度渐变效果
Jul 10 #Javascript
jQuery实现QQ空间汉字转拼音功能示例
Jul 10 #jQuery
Underscore之Array_动力节点Java学院整理
Jul 10 #Javascript
You might like
php下连接mssql2005的代码
2011/01/17 PHP
php图片的裁剪与缩放生成符合需求的缩略图
2013/01/11 PHP
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
2014/06/23 PHP
摘自织梦CMS中的图片处理类
2015/08/08 PHP
PHP is_array() 检测变量是否是数组的实现方法
2016/06/13 PHP
详解PHP字符串替换str_replace()函数四种用法
2017/10/13 PHP
PHP使用Http Post请求发送Json对象数据代码解析
2020/07/16 PHP
JavaScript入门教程(5) js Screen屏幕对象
2009/01/31 Javascript
JSP中使用JavaScript动态插入删除输入框实现代码
2014/06/13 Javascript
javascript实现模拟时钟的方法
2015/05/13 Javascript
jQuery判断一个元素是否可见的方法
2015/06/05 Javascript
使用Raygun对Node.js应用进行错误处理的方法
2015/06/23 Javascript
JQuery validate插件验证用户注册信息
2016/05/11 Javascript
Bootstrap基本样式学习笔记之按钮(4)
2016/12/07 Javascript
vue-router+vuex addRoutes实现路由动态加载及菜单动态加载
2017/09/28 Javascript
详解 vue.js用法和特性
2017/10/15 Javascript
js精确的加减乘除实例
2017/11/14 Javascript
IE11下使用canvas.toDataURL报SecurityError错误的解决方法
2017/11/19 Javascript
BootStrap modal实现拖拽功能
2018/12/01 Javascript
Vue 路由间跳转和新开窗口的方式(query、params)
2019/12/25 Javascript
[59:59]EG vs IG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
浅谈Python中列表生成式和生成器的区别
2015/08/03 Python
python去除空格和换行符的实现方法(推荐)
2017/01/04 Python
Python实现删除文件中含“指定内容”的行示例
2017/06/09 Python
Python基于多线程实现抓取数据存入数据库的方法
2018/06/22 Python
python读取Excel表格文件的方法
2019/09/02 Python
pycharm第三方库安装失败的问题及解决经验分享
2020/05/09 Python
CSS3制作彩色进度条样式的代码示例分享
2016/06/23 HTML / CSS
Supersmart英国:欧洲市场首批食品补充剂供应商之一
2018/05/05 全球购物
创造美妙香氛体验:Aera扩散器和香水
2018/11/25 全球购物
加拿大时装零售商:Influence U
2018/12/22 全球购物
关于奉献的演讲稿
2014/05/21 职场文书
公开承诺书格式
2014/05/21 职场文书
班风口号
2014/06/18 职场文书
2015年公司工作总结
2015/04/25 职场文书
MySQL数据库Innodb 引擎实现mvcc锁
2022/05/06 MySQL