123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <!-- 草稿卡片组件 -->
- <template>
- <view class="autograph-box">
- <canvas class="autograph" :canvas-id="canvasId" :id="canvasId" @touchstart="canvasStart($event)" @touchmove="canvasMove($event)">
- <view v-if="history.length==0" :class="['default-text',horizontalScreen?'rote-text':'']">草稿区域</view>
- </canvas>
- <view :class="['action-box',horizontalScreen?'horizontalScreen':'']">
- <view class="action-bar">
- <view :class="[actionShow?'action-open':'action-close']">
- <!-- <image src="../../static/th-autograph/pencli.svg" @click="openAction('thLine')" v-if="judge('pencli')"></image> -->
- <image src="../../static/th-autograph/color.svg" @click="openAction('thColor')" v-if="judge('color')"></image>
- <image src="../../static/th-autograph/back.svg" @click="goBack" v-if="judge('back')"></image>
- <image src="../../static/th-autograph/clear.svg" @click="clear" v-if="judge('clear')"></image>
- </view>
- <image src="../../static/th-autograph/checkRow.png" @click="checkAction"
- v-if="actionBar.length!=0"
- :class="[actionShow?'roteRight':'roteLeft']"></image>
- </view>
- <!-- <view class="th-submit" @click="saveCanvas">确定</view> -->
- </view>
- <th-color ref="thColor" @setColor="setColor"></th-color>
- <th-line ref="thLine" @setLine="setLine"></th-line>
- </view>
- </template>
- <script>
- /**
- * @property {Array} actionBar 操作按钮配置 pencli(线条) color(颜色) back(返回) clear(清空)
- * @property {String} canvasId CanvasId
- * @property {Boolean} isDownload 是否下载签名
- * @property {Boolean} horizontalScreen 是否横屏
- * @property {String} fileName 文件名称
- * @property {String} delineColor 线颜色
- * @property {Number} delineWidth 线宽度
- **/
- import thColor from "./th-color.vue"
- import thLine from "./th-line.vue"
- export default {
- props: {
- //canvasId
- canvasId: {
- type: String,
- default: `th-${Date.now()}`
- },
- //配置栏
- actionBar:{
- type:Array,
- default:()=>{
- return [
- 'pencli',
- 'color',
- 'back',
- 'clear'
- ]
- }
- },
- //是否下载签名
- isDownload: {
- type: Boolean,
- default: true
- },
- //是否横屏
- horizontalScreen: {
- type: Boolean,
- default: false
- },
- //文件名称
- fileName: {
- type: String,
- default: '签名'
- },
- //线颜色
- delineColor: {
- type: String,
- default: '#000'
- },
- //线宽度
- delineWidth: {
- type: Number,
- default: 4
- }
- },
- data() {
- return {
- context:"",
- actionShow:true,
- history:[],
- lineColor:"#000",
- lineWidth:4
- }
- },
- components:{
- thColor,thLine
- },
- mounted () {
- this.lineColor = this.delineColor
- this.lineWidth = this.delineWidth
- const ctx = uni.createCanvasContext(this.canvasId,this)
- this.context = ctx;
- },
- methods: {
- //操作栏显示控制
- judge(key) {
- if(this.actionBar.includes(key)){
- return true
- }else{
- return false;
- }
- },
- //打开选择器
- openAction(ref) {
- this.$refs[ref].checkModel()
- },
- //设置颜色
- setColor(color) {
- this.lineColor = color;
- },
- //设置线条
- setLine(width) {
- this.lineWidth = width;
- },
- //切换控制栏
- checkAction() {
- this.actionShow = !this.actionShow
- },
- //保存
- async saveCanvas() {
- const tempFilePath = await this.canvasToFilPath()
- if(!this.isDownload) {
- this.$emit('submit',tempFilePath)
- return false;
- }
- return new Promise((resolve, reject) => {
- // #ifdef H5
- try {
- const a = document.createElement('a')
- a.href = tempFilePath
- a.download = this.fileName
- document.body.appendChild(a)
- a.click()
- a.remove()
- resolve()
- } catch (e) {
- reject(e)
- }
- // #endif
- // #ifndef H5
- uni.saveImageToPhotosAlbum({
- filePath: tempFilePath,
- success(resObj) {
- this.$emit('submit',tempFilePath)
- resolve(resObj)
- },
- fail(err) {
- this.$emit('dowmloadErr',err)
- reject(err)
- }
- })
- // #endif
- })
- },
- // 保存临时路径
- canvasToFilPath(conf = {}) {
- return new Promise((resolve, reject) => {
- uni.canvasToTempFilePath(
- {
- canvasId: this.canvasId,
- success: res => {
- resolve(res.tempFilePath)
- },
- fail: err => {
- reject(err)
- }
- }
- )
- })
- },
- //撤回
- goBack() {
- this.context.draw()
- this.history.pop();
- this.history.forEach((item, index) => {
- let {color,width} = item.style
- this.context.beginPath()
- this.context.setLineCap('round')
- this.context.setStrokeStyle(color)
- this.context.setLineWidth(width)
- if (item.coordinates.length >= 2) {
- item.coordinates.forEach(it => {
- if (it.type == 'touchstart') {
- this.context.moveTo(it.x, it.y)
- } else {
- this.context.lineTo(it.x, it.y)
- }
- })
- } else {
- const point = item.coordinates[0]
- this.context.moveTo(point.x, point.y)
- this.context.lineTo(point.x + 1, point.y)
- }
- this.context.stroke()
- })
- this.context.draw(true)
- },
- //清空画布
- clear() {
- this.history = [];
- this.context.draw()
- },
- canvasStart(event) {
- let {x,y} = event.touches[0]
- this.history.push({
- style: {
- color: this.lineColor,
- width: this.lineWidth
- },
- coordinates: [
- {
- type: event.type,
- x: x,
- y: y
- }
- ]
- })
- this.drawGraphics()
- },
- canvasMove(e) {
- // e.preventDefault()
- let {x,y} = e.touches[0]
- this.history[this.history.length - 1].coordinates.push({
- type: e.type,
- x: x,
- y: y
- })
- this.drawGraphics()
- },
- //绘制
- drawGraphics() {
- let historyLen = this.history.length
- if (!historyLen) return
- let currentData = this.history[historyLen - 1]
- let coordinates = currentData.coordinates
- if (!coordinates.length) return
- let startPoint,endPoint;
- if (coordinates.length < 2) {
- startPoint = coordinates[coordinates.length - 1]
- endPoint = {
- x: startPoint.x + 1,
- y: startPoint.y
- }
- } else {
- startPoint = coordinates[coordinates.length - 2]
- endPoint = coordinates[coordinates.length - 1]
- }
- let style = currentData.style
- this.context.beginPath()
- this.context.setLineCap('round')
- this.context.setStrokeStyle(style.color)
- this.context.setLineWidth(style.width)
- this.context.moveTo(startPoint.x, startPoint.y)
- this.context.lineTo(endPoint.x, endPoint.y)
- this.context.stroke()
- this.context.draw(true)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .autograph{
- width: 100%;
- height:100%;
- box-sizing: border-box;
- // border:1px dashed #ccc;
- }
- .horizontalScreen{
- left: -150upx !important;
- bottom: 0 !important;
- right: auto !important;
- transform: rotate(90deg);
- transform-origin:bottom right;
- }
- .rote-text{
- transform:rotate(90deg);
- }
- .action-box{
- position: absolute;
- bottom: 0;
- right: 0;
- z-index: 50;
- display: flex;
- flex-direction: column;
- align-items: flex-end;
- }
- .th-submit{
- width: 150upx;
- height: 100upx;
- background-color: #4169E1;
- border-radius: 70upx 0 0 0;
- display: flex;
- align-items: center;
- justify-content: center;
- color: #FFFFFF;
- font-size: 30upx;
- }
- .action-bar{
- margin-bottom: 30upx;
- margin-right: 35upx;
- image{
- width: 40upx;
- height: 40upx;
- }
- >image{
- transition: all 0.3s;
- }
- >view{
- display: flex;
- flex-direction: column;
- image{
- width: 40upx;
- height: 40upx;
- margin-bottom: 52upx;
- }
- }
- }
- .autograph-box{
- width: 100%;
- height: 100%;
- position: relative;
- .default-text{
- width: 100%;
- height: 100%;
- position: absolute;
- top: 0;
- left: 0;
- display: flex;
- align-items: center;
- z-index: -1;
- justify-content: center;
- font-size: 38upx;
- color: #C0C0C0;
- letter-spacing: 5upx;
- }
- }
- .roteRight{
- transform: rotate(136deg);
- }
- .roteLeft{
- transform: rotate(0deg);
- }
- .action-open{
- animation:bounceIn 1s;
- }
- .action-close{
- animation:bounceOut 0.5s forwards;
- }
- @keyframes bounceIn {
-
- 0%,
- 20%,
- 40%,
- 60%,
- 80%,
- to {
- -webkit-animation-timing-function: cubic-bezier(.215, .61, .355, 1);
- animation-timing-function: cubic-bezier(.215, .61, .355, 1)
- }
-
- 0% {
- opacity: 0;
- -webkit-transform: scale3d(.3, .3, .3);
- transform: scale3d(.3, .3, .3)
- }
-
- 20% {
- -webkit-transform: scale3d(1.1, 1.1, 1.1);
- transform: scale3d(1.1, 1.1, 1.1)
- }
-
- 40% {
- -webkit-transform: scale3d(.9, .9, .9);
- transform: scale3d(.9, .9, .9)
- }
-
- 60% {
- opacity: 1;
- -webkit-transform: scale3d(1.03, 1.03, 1.03);
- transform: scale3d(1.03, 1.03, 1.03)
- }
-
- 80% {
- -webkit-transform: scale3d(.97, .97, .97);
- transform: scale3d(.97, .97, .97)
- }
-
- to {
- opacity: 1;
- -webkit-transform: scaleX(1);
- transform: scaleX(1)
- }
- }
- @keyframes bounceOut {
- 20% {
- -webkit-transform: scale3d(.9, .9, .9);
- transform: scale3d(.9, .9, .9)
- }
-
- 50%,
- 55% {
- opacity: 1;
- -webkit-transform: scale3d(1.1, 1.1, 1.1);
- transform: scale3d(1.1, 1.1, 1.1)
- }
-
- to {
- opacity: 0;
- -webkit-transform: scale3d(.3, .3, .3);
- transform: scale3d(.3, .3, .3);
- display: none;
- }
- }
- </style>
|