123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- const HOST = require('../../utils/const.js').apiUrl;
- Page({
-
- data: {
- imageUrl: '',
- cropperW: '',
- cropperH: '',
- img_ratio: '',
- IMG_W: '',
- IMG_H: '',
- clipImage: '',
- left: '',
- top: '',
- clipW: 200
- },
-
- cancel: function () {
- wx.navigateBack();
- },
-
- move: function ({ detail }) {
- this.setData({
- left: detail.x * 2,
- top: detail.y * 2
- })
- },
-
- scale: function ({detail}) {
- console.log(detail.scale)
- this.setData({
- clipW: 200 * detail.scale
- })
- },
-
- getImageInfo: function () {
- wx.showLoading({
- title: '图片生成中...',
- })
- const img_ratio = this.data.img_ratio;
- const canvasW = (this.data.clipW / this.data.cropperW) * this.data.IMG_W
- const canvasH = (this.data.clipW / this.data.cropperH) * this.data.IMG_H
- const canvasL = (this.data.left / this.data.cropperW) * this.data.IMG_W
- const canvasT = (this.data.top / this.data.cropperH) * this.data.IMG_H
-
- const ctx = wx.createCanvasContext('myCanvas');
-
- ctx.save();
- ctx.beginPath();
- ctx.clearRect(0, 0, 1000, 1000)
-
- ctx.arc(this.data.clipW / 2, this.data.clipW / 2, this.data.clipW / 2, 0, 2 * Math.PI, false)
- ctx.clip();
-
-
- ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, 0, 0, this.data.clipW, this.data.clipW);
- ctx.restore();
- ctx.draw(true, () => {
-
- wx.canvasToTempFilePath({
- x: 0,
- y: 0,
- width: this.data.clipW,
- height: this.data.clipW,
- destWidth: this.data.clipW,
- destHeight: this.data.clipW,
- quality: 0.5,
- canvasId: 'myCanvas',
- success: (res) => {
- wx.hideLoading()
- this.setData({
- clipImage: res.tempFilePath
- })
-
- wx.uploadFile({
- url: HOST + 'wx/file/upload',
- filePath: res.tempFilePath,
- name: 'uploadfile_ant',
- header: {
- "Content-Type": "multipart/form-data"
- },
- success: (res) => {
- console.log(res);
- const data = JSON.parse(res.data);
- if (data.success) {
- wx.navigateTo({
- url: '/pages/setName/setName?imageUrl=' + data.data
- })
- }
- },
- fail: function (res) {
- wx.hideToast();
- wx.showModal({
- title: '错误提示',
- content: '上传图片失败'
- })
- }
- });
-
-
-
-
-
- }
- })
- })
- },
-
- onLoad: function (options) {
- console.log(options.imageUrl);
- const imageUrl = options.imageUrl;
- this.setData({
- imageUrl
- })
- wx.getImageInfo({
- src: imageUrl,
- success: (res) => {
- console.log(res);
-
- const width = res.width;
- const height = res.height;
-
- const img_ratio = width / height
- this.setData({
- img_ratio,
- IMG_W: width,
- IMG_H: height,
- })
- if (img_ratio >= 1) {
-
- this.setData({
- cropperW: 750,
- cropperH: 750 / img_ratio,
- })
- } else {
-
- this.setData({
- cropperW: 750 * img_ratio,
- cropperH: 750
- })
- }
- }
- })
- },
-
- onReady: function () {
- },
-
- onShow: function () {
- },
-
- onHide: function () {
- },
-
- onUnload: function () {
- },
-
- onPullDownRefresh: function () {
- },
-
- onReachBottom: function () {
- },
-
- onShareAppMessage: function () {
- }
- })
|