天看到一款前端图片剪裁开源项目- vue-picture-cut,基于vue开发,功能太逆天,特来分来,建议收藏;
先来个预览看看:
功能在线演示站点:
https://htmlpreview.github.io/?https://raw.githubusercontent.com/987153776/vue-picture-cut/master/dist/index.html
这是一款基于vue和typescript开发的一款图片剪裁处理工具。优点:原生、轻量、使用简单、功能全面、扩展性强。目前功能:缩放、翻折、旋转、边缘校验、矩形剪裁、任意(椭)圆剪裁。
码云开源地址:
https://gitee.com/light-year/vue-picture-cut
github开源地址:
https://github.com/987153776/vue-picture-cut
English | 中文
基于vue和typescript开发的一款图片剪裁处理工具
优点:原生、轻量、使用简单、功能全面、扩展性强
目前功能:缩放、翻折、旋转、边缘校验、矩形剪裁、任意(椭)圆剪裁
关于缩放:鼠标(鼠标滚轮缩放)、触屏(双指缩放)
github主页
码云主页
demo演示(github)
demo演示(码云)
0.x版本点这里【此版本bug太多,不再维护】
1、在main.js中全局引用
import Vue from 'vue';
import 'vue-picture-cut/lib/vue-picture-cut.css';
import VuePictureCut from 'vue-picture-cut';
Vue.use(VuePictureCut);
2、或者在.vue文件中单独引用
<template>
<div>
<input type="file" accept="image/*" @change="inputChange"/>
<vue-picture-cut :src="src" @on-change="cutChange"/>
</div>
</template>
<script>
import { VuePictureCut } from 'vue-picture-cut';
export default {
// ...
components: {
VuePictureCut
},
data () {
return {
src: null,
blob: null,
base64: null
}
},
methods: {
inputChange(e) {
const file=e.target.files[0];
this.src=URL.createObjectURL(file);
},
/**
* @param blob BLOB对象
* @param base64 base64字符串
*/
cutChange({ blob, base64 }) {
this.blob=blob;
this.base64=base64;
}
}
// ...
}
</script>
<style>
@import "~vue-picture-cut/lib/vue-picture-cut.css";
</style>
3、注意
组件在使用时,宽高跟随父级标签,所以需要设置父级标签的宽高。
全局引入时
import VuePictureCut from 'vue-picture-cut';
Vue.use(VuePictureCut);
此时会注册:VuePictureCut、VuePictureCutMask、VuePictureCutMenu三个组件。
独立引用
import {
VuePictureCut,
VuePictureCutMask,
VuePictureCutMenu,
Bezier,
createAnimation,
Tool,
createUtils
} from 'vue-picture-cut';
组件:VuePictureCut、VuePictureCutMask、VuePictureCutMenu。工具类:Bezier、createAnimation、Tool、createUtils。
slot插槽: default、menu
使用:
/demo/demo1.html
/demo/demo2.html
<template>
<vue-picture-cut
ref="pictureCut"
:src="src"
:magnification="magnification"
:init-angle="initAngle"
:msk-option="mskOption"
:max-pixel="maxPixel"
:encoder-options="encoderOptions"
:format="format"
:rotate-control="rotateControl"
:menu-position="menuPosition"
:menu-thickness="menuThickness"
:background-color="backgroundColor"
@on-change="onChange"
/>
</template>
属性:
事件:
方法:
VuePictureCutMask是VuePictureCut默认slot插槽组件,是控制遮罩裁剪框相关的组件,使用它与不使用它效果一样。
使用:
/demo/demo3.html
<template>
<vue-picture-cut
:src="src"
:magnification="magnification"
:init-angle="initAngle"
:rotate-control="rotateControl"
:max-pixel="maxPixel"
:encoder-options="encoderOptions"
:format="format"
:background-color="backgroundColor"
@on-change="onChange"
>
<vue-picture-mask
:width="width"
:height="height"
:is-round="isRound"
:resize="resize"
:color="color"
:border-color="borderColor"
/>
</vue-picture-cut>
</template>
属性:
菜单栏组件,效果参见Demo。
使用:
/demo/demo4.html
/demo/demo5.html
<template>
<vue-picture-cut
:src="src"
:magnification="magnification"
:init-angle="initAngle"
:rotate-control="rotateControl"
:msk-option="mskOption"
@on-change="cutChange"
>
<vue-picture-menu
slot="menu"
:cancel="false"
:max-pixel="maxPixel"
:encoder-options="encoderOptions"
:format="format"
:theme="theme"
confirm-name="Ok"
cancel-name="Cancel"
size-auto-name="auto"
size-raw-name="raw"
menu-rotate-name="Rotate"
@on-change="onChange"
@on-cancel="onCancel"
/>
</vue-picture-cut>
</template>
属性:
事件:
import { Bezier } from 'vue-picture-cut';
const bezier=Bezier();
bezier.setOpt(Bezier.BEZIER['ease-in-out']);
const y=bezier.getPoint(0.5);
console.log(y);
键
值
说明
linear | [0.0, 0.0, 1.0, 1.0] | 线性过渡 |
ease | [0.25, 0.1, 0.25, 1.0] | 平滑过渡 |
ease-in | [0.42, 0, 1.0, 1.0] | 由慢到快 |
ease-out | [0, 0, 0.58, 1.0] | 由快到慢 |
ease-in-out | [0.42, 0, 0.58, 1.0] | 由慢到快再到慢 |
一个无参的构造函数,内部调用了setOptByString('ease')方法
方法名
说明
参数
返回值
setOpt | 设置贝塞尔曲线类型 | (arg: string , ParamsInterface='ease') | Bezier对象本身 |
setOptByString | 设置贝塞尔曲线类型 | BEZIER预设值(arg='ease') | Bezier对象本身 |
setOptByArr | 设置贝塞尔曲线类型 | (x1: number, y1: number, x2: number, y2: number) | Bezier对象本身 |
getPoint | 返回x坐标对应的y坐标值 | (x: number) 0~1之间 | 对应y坐标,0~1之间 |
ParamsInterface为包含4个number类型的数组。
import { createAnimation } from 'vue-picture-cut';
const animation=createAnimation(option);
createAnimation会返回一个Animation对象
参数
说明
类型
可选值
必需
默认值
duration | 动画持续时间,单位毫秒 | number | —— | false | 1000 |
timing | 动画的过渡类型 | string、number[] | Bezier.BEZIER中的值,或者ParamsInterface类型 | false | ease |
delay | 动画的延迟时间,单位毫秒 | number | —— | false | 0 |
iteration | 动画循环次数,infinite为无限循环 | number、string | 'infinite'或正整数 | false | 0 |
direction | 动画在循环中是否反向运动 | string | normal(默认,正向运动);reverse(反向运行);alternate(先正向,后反向,并交替);alternate-reverse(先反向,后正向,并交替)。 | false | normal |
change | 回调函数,接收参数x,x在0~1之间,动画在这里处理 | Function | —— | false | —— |
end | 回调函数,动画结束时执行 | Function | —— | false | —— |
方法名
说明
参数
返回值
start | 开始动画 | —— | Animation对象本身 |
abort | 中止动画 | —— | —— |
import { Tool } from 'vue-picture-cut';
Tool.loadImg('http://xxx.xxx.xxx/xxx.jpg')
.then(image=> {
// 加载图片成功
// image为Image对象
}, ()=> {
// 加载图片失败
});
<template>
<div>
<input type="file" accept="image/*" @change="inputChange"/>
<vue-picture-cut ref="pictureCut" :src="src"/>
<button @click="doCut">裁剪</button>
</div>
</template>
<script>
import { VuePictureCut, createUtils } from 'vue-picture-cut';
export default {
// ...
components: {
VuePictureCut
},
data () {
return {
utils: null,
src: null,
blob: null,
base64: null
}
},
mounted() {
this.utils=createUtils(this.$refs['pictureCut']);
},
methods: {
inputChange(e) {
const file=e.target.files[0];
this.src=URL.createObjectURL(file);
},
doCut() {
const res=this.utils.cut();
if (res) {
this.blob=res.file; // BLOB对象
this.base64=res.src; // base64字符串
}
}
}
// ...
}
</script>
<style>
@import "~vue-picture-cut/lib/vue-picture-cut.css";
</style>
...待编辑
暂时可以参考src/App.vue和src/lib/views/vue-picture-cut-menu.vue
...文档待编辑
<img src="./post-img.jpg" />
注:HTML有多种标签中使用的属性,属性的作用是描述元素,src就是属性,为了描述图片。图片的路径可以是绝对路径也可以是相对路径。
<img src="./post-img.jpg" alt="HTML 代码图片" />
若我们图片无法显示,将会如下图显示:
<img src="./post-img.jpg" alt="HTML 代码图片" width="500" height="200" />
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
</head>
注:meta元素代表元数据,【charset="UTF-8"】,表示字符集编码格式为UTF-8
uniapp开发时,富文本里的图片路径因为是相对路径而无法显示,这时候就需要用正则方式给图片路径加上绝对地址(http://域名),让uniapp能正常显示。
另外,因为富文本里的图片因为宽度太宽,导致显示超出界面,同样,可以用正则方式加以修正(但推荐采用在uniapp模板里加CSS样式控制)
1、把uniapp里富文本的图片相对路径改为绝对路径(加上https(s)://域名)的形式,代码如下:
this.content1 = content1.replace(/\<img src="/gi, '<img src="http://localhost:2000');//content1为富文本里的内容
2、修改富文本里的图片样式,代码如下:
A、推荐方式,即非正则方式:
<view class="detail">
<rich-text :nodes="content1"></rich-text>
</view>
<style>
.detail img{max-width:100%;}
</style>
B、正则方式:
*请认真填写需求信息,我们会在24小时内与您取得联系。