Vue实现用户自定义上传头像裁剪
使用技术:
vue.js2.0、cropperjs、canvas
<template> <div id= "app" > <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> </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 * * 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;
|
Vue实现用户自定义上传头像裁剪的更多相关文章
- vue 剪切图片上传头像,使用 cropperjs 实现
我使用的是vue,移动端的项目. 官网地址:cropperjs GitHub地址:https://github.com/fengyuanchen/cropperjs/blob/master/READM ...
- html5 上传头像的裁剪
本示例使用HTML5 canvas,简单的编写了上传头像的裁剪效果,移动端支持拖拽后裁剪, 虽然样式不好看,但是功能还算全: 下图为裁剪后的效果: html部分: <!DOCTYPE html& ...
- Jcrop+uploadify+php实现上传头像预览裁剪
最近由于项目需要,所以做了一个上传头像预览并且可以预览裁剪的功能,大概思路是上传的图片先保存到服务器,然后通过ajax从服务器获取到图片信息,再利用Jcrop插件进行裁剪,之后通过PHP获取到的四个裁 ...
- 上传头像,界面无跳转,php+js
上传头像,界面无跳转的方式很多,我用的是加个iframe那种.下面直接上代码. html: //route 为后端接口//upload/avatar 为上传的头像的保存地址//imgurl=/uplo ...
- Android基础之——startActivityForResult启动界面并返回数据,上传头像
在android应用的开发过程中,常常会出现启动一个界面后填写部分内容后带着数据返回启动前的界面,最典型的应用就是登录过程.在非常多应用程序的模块中,都有"我的"这个模块,在未登录 ...
- day105:Mofang:设置页面初始化&更新头像/上传头像&设置页面显示用户基本信息
目录 1.设置页面初始化 2.更新头像 1.点击头像进入更新头像界面 2.更新头像页面初始化 3.更新头像页面CSS样式 4.头像上传来源选择:相册/相机 5.调用api提供的本地接口从相册/相机提取 ...
- swift上传头像
很久没有写博客了,今天特地写了这个,也是一边仿照别人写的demo,注释部分都是需要的.需要的同学可以参考一下. @IBAction func headImageBtnPage(){ //上传头像 / ...
- 完美实现类似QQ的自拍头像、上传头像功能!(Demo 源码)
现在很多下载客户端程序都需要设定自己头像的功能,而设定头像一般有两种方式:使用摄像头自拍头像,或者选择一个图片的某部分区域作为自己的头像. 一.相关技术 若要实现上述的自拍头像和上传头像的功能,会碰到 ...
- asp.net mvc上传头像加剪裁功能介绍
正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jquery.Jcrop 后台是asp.net mvc ...
随机推荐
- 《剑指offer》从上往下打印二叉树
本题来自<剑指offer> 从上往下打印二叉树 题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 队列的思想. 先将根节点加入,当取该节点时候,依次将左右子树加入,直 ...
- Ubuntu 安装google 拼音
一.安装fcitx apt-get install fcitx 二.安装google pinyin sudo apt install fcitx-googlepinyin 三. 安装 fcitx-co ...
- 编辑方法分享之如何编辑PDF文件内容
我们现在在工作中会经常使用到PDF文件,还会有遇到需要编辑PDF文件的时候,PDF文件的编辑问题一直是个大难题.很多朋友在面对PDF文件的时候束手无策,不知道该怎么对它进行编辑.下面小编就教给大家一个 ...
- python接口自动化测试三十三:获取时间戳(10位和13位)
很多时候,在调用接口时,需要对请求进行签名.需要用到unix时间戳. 在python里,在网上介绍的很多方法,得到的时间戳是10位.而java里默认是13位(milliseconds,毫秒级的). 下 ...
- 启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
三种因素引起: 1.jvm(jdk)的内存引起. 2. eclipse的内存引起. 3.tomcat的内存引起. 1.解决方法: 2.解决方法: 解决问题的方式就是:修改了安装目录eclipse.in ...
- spring cloud 路由网关zuul的高可用
Zuul的高可用非常关键,因为外部请求到后端微服务的流量都会经过Zuul.故而在生产环境中,我们一般都需要部署高可用的Zuul以避免单点故障. 笔者分两种场景讨论Zuul的高可用. Zuul客户端也注 ...
- python:字符串转换成字节的三种方式及字符转码问题
str='zifuchuang' 第一种 b'zifuchuang'第二种bytes('zifuchuang',encoding='utf-8')第三种('zifuchuang').encode('u ...
- webpack学习笔记--配置devServer
devServer 1-6 使用DevServer 介绍过用来提高开发效率的 DevServer ,它提供了一些配置项可以改变 DevServer 的默认行为. 要配置 DevServer ,除了在配 ...
- Angularjs 学习笔记-2017-02-06-双向数据绑定
NG: ng-bind: 标签属性 ng-bind=" obj.xxx " ,不会出现 用于区别{{ }} 标签,当页面未加载完毕时可以看到{{}}标签,非常不雅观,ng-bi ...
- Google搜索
https://www.google.com/intl/br/insidesearch/tipstricks/all.html 如何用好谷歌等搜索引擎?