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 ...
随机推荐
- 浅谈FastJson的TypeReference用法
简单描述:看同事提交的代码,发现有一行代码 似曾相识,但却朦朦胧胧,ε=(´ο`*)))唉很明显自己没掌握呗,于是乎,就百度了一下 干货:对进行泛型的反序列化,使用TypeReference可以明确的 ...
- django----Form实时更新两种方式
在ModelForm需要知道: from app03 import models from django.forms import ModelForm class UserForm(ModelForm ...
- django----利用Form 实现两次密码输入是否一样 ( 局部钩子和全局钩子 )
from django import forms # 导入表单模块 from django.core.exceptions import ValidationError class RegisterF ...
- Python元组(tuple)
元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...
- 论文阅读笔记四:CTPN: Detecting Text in Natural Image with Connectionist Text Proposal Network(ECCV2016)
前面曾提到过CTPN,这里就学习一下,首先还是老套路,从论文学起吧.这里给出英文原文论文网址供大家阅读:https://arxiv.org/abs/1609.03605. CTPN,以前一直认为缩写一 ...
- 任务超时退出的方法 C#
超出时间方法退出.防止卡住. 方法: private static bool ImportTaskTimeout(Action method, int hours) { try { var task ...
- MongoDB 入门
1 MongoDb 简介 MongoDB是为互联网而生的数据库,是文档数据库. 1.1 优点: Schema-less,不需要预先定义表结构,同一个"表"中可以保存多个格式的数据: ...
- Idea设置行注释不显示在行首
如图:idea使用ctrl+/注释时候,//都在行首,强迫症表示受不了 解决方法如图
- python多线程之t.setDaemon(True) 和 t.join()
0.目录 1.参考2.结论 (1)通过 t.setDaemon(True) 将子线程设置为守护进程(默认False),主线程代码执行完毕后,python程序退出,无需理会守护子线程的状态. ...
- pycharm中出现的错误
错误1 在pycharm上安装TensorFlow运行import tensorflow时报错: File , in _find_and_load SystemError: PyEval_EvalFr ...