微信小程序截圖工具
發表時(shí)間:2021-4-22
發布人(rén):融晨科技
浏覽次數:101
welCropper 微信小程序截圖工具
文件目錄結構,要(yào / yāo)在(zài)測試機上(shàng)運行,工程目錄選擇文件夾project
./
├── documents
│ ├── hierarchy.png
│ ├── result.gif
│ └── screenshot.jpeg
├── project
│ ├── app.js
│ ├── app.json
│ ├── app.wxss
│ ├── pages
│ │ ├── index
│ │ │ ├── index.js
│ │ │ ├── index.json
│ │ │ ├── index.wxml
│ │ │ └── index.wxss
│ │ └── test
│ │ ├── test.js
│ │ ├── test.json
│ │ ├── test.wxml
│ │ └── test.wxss
│ ├── utils
│ │ └── util.js
│ └── welCropper
│ ├── welCropper.js
│ ├── welCropper.wxml
│ └── welCropper.wxss
└── readme.md
- 保證圖片質量,也(yě)可以(yǐ)選擇壓縮圖
Documents
因爲(wéi / wèi)cropper
的(de)數據和(hé / huò)事件是(shì)直接綁定到(dào)Page
上(shàng)的(de),所以(yǐ)數據和(hé / huò)事件命名應該避免一下名字(之(zhī)後會想辦法避免這(zhè)種情況)及其相關解釋:
data中的(de)名字:
- cropperData
- cropperMovableItems
函數名:
- showCropper
- hideCropper
- originalChange
- cropImage
- loadImage
- clearCanvas
- drawImage
- drawOriginalImage
- drawLines
- setupMoveItem
- moveEvent
- endEvent
外部隻用到(dào)showCropper
和(hé / huò)hideCropper
/**
inputPath:輸入圖片地(dì / de)址
callback(resPath):點擊“完成”按鈕後毀掉函數,毀掉函數中會有截圖地(dì / de)址
*/
showCropper(inputPath, callback)
使用
将welCropper
複制到(dào)自己的(de)工程當中(以(yǐ)/pages/index/index
爲(wéi / wèi)例)
wxml
引入并調用:
<!-- 引入組件 -->
<import src="https://www.wxapp-union.com/welCropper/welCropper" />
<!-- 調用組件 -->
<template is="welCropper" data="{{data:cropperData, cropperMovableItems:cropperMovableItems}}"></template>
<!-- 用于(yú)選擇圖片,傳入cropper中 -->
<button bindtap='selectTap'>select image</button>
wxss
引入:
@import "/welCropper/welCropper.wxss";
js
引入和(hé / huò)使用:
// 獲取顯示區域長寬
const device = wx.getSystemInfoSync()
const W = device.windowWidth
const H = device.windowHeight - 50
let cropper = require('../../welCropper/welCropper.js');
console.log(device)
Page({
data: {
},
onLoad: function () {
var that = this
// 初始化組件數據和(hé / huò)綁定事件
cropper.init.apply(that, [W, H]);
},
selectTap() {
var that = this
wx.chooseImage({
count: 1, // 默認9
sizeType: ['original', 'compressed'], // 可以(yǐ)指定是(shì)原圖還是(shì)壓縮圖,默認二者都有
sourceType: ['album', 'camera'], // 可以(yǐ)指定來(lái)源是(shì)相冊還是(shì)相機,默認二者都有
success(res) {
const tempFilePath = res.tempFilePaths[0]
console.log(tempFilePath)
// 将選取圖片傳入cropper,并顯示cropper
that.showCropper({
src: tempFilePath,
sizeType: ['original', 'compressed'], //'original' | 'compressed'(default)
callback: (resPath) => {
console.log("crop callback:" + resPath)
wx.previewImage({
current: '',
urls: [resPath]
})
// that.hideCropper() //隐藏,我在(zài)項目裏是(shì)點擊完成就(jiù)上(shàng)傳,所以(yǐ)如果回調是(shì)上(shàng)傳,那麽隐藏掉就(jiù)行了(le/liǎo),不(bù)用previewImage
}
})
}
})
}