// pages/edit/edit.js
const app = getApp() //全局对象
let cfg = {
img: {},
scale: 1 //默认不缩放
} //全局配置对象
let SCALE = { //常量
MAX: 2,
MIN: 0.3
}
Page({
/**
* 页面的初始数据 素材图片
*/
data: {
images: [{
img: '../../img/clothes0.png'
},{
img: '../../img/clothes1.png'
},{
img: '../../img/hair0.png'
},{
img: '../../img/hair1.png'
},{
img: '../../img/shoe1.png'
},{
img: '../../img/shoe2.png'
}],
canvasWidth: 0,
canvasHeight: 0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setCanvasSize();
},
//图片自适应画布
setCanvasSize: function(){
var imgFile = app.globalData.imgFile;
console.log("edit::",imgFile)
var self = this;
//取到画布信息
wx.createSelectorQuery().select("#edit-editor").boundingClientRect(function(wrapper){
var _canvasWidth = 0, _canvasHeight = 0;
wx.getImageInfo({
src: imgFile.tempFilePaths[0],
success(res) {
var originalWidth = res.width;
var originalHeight = res.height;
//图片比例 画布比例
if (originalHeight/originalWidth > wrapper.height/wrapper.width) {
_canvasHeight = wrapper.height;
_canvasWidth = originalWidth * _canvasHeight/originalHeight;
} else {
_canvasWidth = wrapper.width;
_canvasHeight = originalHeight * _canvasWidth/originalWidth;
}
//保存缩放后的宽高
self.setData({
canvasHeight: _canvasHeight,
canvasWidth: _canvasWidth
})
//绘制图片
var ctx = wx.createCanvasContext("editor");
ctx.drawImage(imgFile.tempFilePaths[0], 0, 0, _canvasWidth, _canvasHeight);
ctx.draw();
}
})
}).exec();
},
// 点击图片素材
onTapImgMaterial: function(event){
console.log(event.currentTarget.dataset.index);
this.drawImgMaterial(event.currentTarget.dataset.index);
},
// 绘制图片素材
drawImgMaterial: function(index){
var images = this.data.images;
var imgFile = app.globalData.imgFile;
var self = this;
var ctx = wx.createCanvasContext("editor");
wx.getImageInfo({
src: images[index].img,
success(res) {
var width = cfg.img.originalWidth = res.width;
var height = cfg.img.originalHeight = res.height;
//将素材保存
cfg.img.origin = images[index].img;
//初始化坐标
cfg.img.x = 0;
cfg.img.y = 0;
//绘制上传的图片
ctx.drawImage(imgFile.tempFilePaths[0], 0, 0,
self.data.canvasWidth, self.data.canvasHeight);
//绘制选中图片素材
ctx.drawImage(images[index].img, 0, 0, 100, 100 * height/width);
ctx.draw();
}
})
},
startMove: function(event) {
var touchPoint = event.touches[0];
var x = cfg.img.x;
var y = cfg.img.y;
cfg.offsetX = touchPoint.clientX - x; //解决图片跳问题
cfg.offsetY = touchPoint.clientY - y;
},
startZoom: function(event){
var xLen = event.touches[1].clientX - event.touches[0].clientX;
var yLen = event.touches[1].clientY - event.touches[0].clientY;
//2指距离
cfg.initDistance = Math.sqrt(xLen*xLen + yLen*yLen);
},
//触摸开始
onTouchStart: function(event) {
if (event.touches.length == 1) {
this.startMove(event);
}
if (event.touches.length > 1) {
this.startZoom(event);
}
},
//触摸移动
onTouchMove: function(event){
if (event.touches.length == 1) {
this.move(event);
}
if (event.touches.length > 1) {
this.zoom(event);
}
},
//释放
onTouchEnd: function(){
},
//缩放
zoom: function(event) {
var xLen = event.touches[1].clientX - event.touches[0].clientX;
var yLen = event.touches[1].clientY - event.touches[0].clientY;
//2指距离
cfg.curDistance = Math.sqrt(xLen*xLen + yLen*yLen);
//计算缩放比例
cfg.scale = cfg.scale + 0.001 * (cfg.curDistance - cfg.initDistance);
cfg.scale = Math.min(cfg.scale, SCALE.MAX);
cfg.scale = Math.max(cfg.scale, SCALE.MIN);
var imgWidth = 100 * cfg.scale;
var imgHeight = imgWidth * cfg.img.originalHeight/cfg.img.originalWidth;
var ctx = wx.createCanvasContext("editor");
var imgFile = app.globalData.imgFile;
ctx.drawImage(imgFile.tempFilePaths[0], 0, 0,
this.data.canvasWidth, this.data.canvasHeight);
//绘制选中图片素材
ctx.drawImage(cfg.img.origin, cfg.img.x, cfg.img.y, imgWidth, imgHeight);
ctx.draw();
},
move: function(event) {
var touchPoint = event.touches[0];
var x = touchPoint.clientX - cfg.offsetX;
var y = touchPoint.clientY - cfg.offsetY;
cfg.img.x = x;
cfg.img.y = y;
var imgWidth = 100 * cfg.scale;
var imgHeight = imgWidth * cfg.img.originalHeight/cfg.img.originalWidth;
var ctx = wx.createCanvasContext("editor");
var imgFile = app.globalData.imgFile;
ctx.drawImage(imgFile.tempFilePaths[0], 0, 0,
this.data.canvasWidth, this.data.canvasHeight);
//绘制选中图片素材
ctx.drawImage(cfg.img.origin, x, y, imgWidth, imgHeight);
ctx.draw();
},
//保存编辑后的图片
savePic: function(){
wx.canvasToTempFilePath({
width: 300,
height: 300,
destWidth: 600,
destHeight: 600,
canvasId: "editor",
success: function(res){
console.log(res.tempFilePath)
//将临时图片保存到相册
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function(){
wx.showToast({
title: '保存成功',
})
}
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
没有合适的资源?快使用搜索试试~ 我知道了~
微信小程序完整代码微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码
共120个文件
png:39个
json:29个
js:22个
需积分: 3 0 下载量 192 浏览量
2023-01-24
21:52:30
上传
评论
收藏 3.72MB ZIP 举报
温馨提示
微信小程序完整微信小程序微微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码信微微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码信小程序完整微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码代码小程序完微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码整代码完整代码微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码代码微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码
资源推荐
资源详情
资源评论
收起资源包目录
微信小程序完整代码微信小程序完整微信小程序微信微信小程序完整代码小程序完整代码完整代码代码 (120个子文件)
bg.jpg 46KB
cover.jpg 6KB
edit.js 6KB
index.js 1KB
index.js 1KB
app.js 1KB
app.js 1KB
second.js 897B
direc.js 847B
edit.js 841B
index.js 664B
util.js 472B
util.js 472B
util.js 472B
util.js 472B
util.js 472B
index.js 384B
index.js 384B
index.js 284B
logs.js 261B
app.js 123B
app.js 123B
app.js 123B
app.js 123B
project.config.json 2KB
project.config.json 2KB
project.config.json 1KB
project.config.json 1KB
project.config.json 1KB
project.config.json 1KB
app.json 327B
app.json 300B
app.json 296B
app.json 296B
app.json 273B
app.json 272B
sitemap.json 191B
sitemap.json 191B
sitemap.json 191B
sitemap.json 191B
sitemap.json 191B
sitemap.json 191B
logs.json 77B
index.json 39B
index.json 39B
index.json 39B
index.json 39B
second.json 27B
index.json 27B
edit.json 27B
index.json 27B
direc.json 27B
edit.json 27B
bg.png 630KB
bg.png 630KB
bg.png 630KB
bg.png 630KB
baby-bg2.png 331KB
wx3faec47ecdd419d0.o6zAJsweqdg9k_xlILB1DaaBCEs8.9pm606dq6Mrree8de7646ccff0419f0a54c83c37c4a3.png 321KB
baby.png 67KB
clothes2.png 42KB
clothes4.png 34KB
clothes1.png 34KB
clothes5.png 30KB
clothes3.png 29KB
clothes0.png 29KB
hair1.png 26KB
hair4.png 21KB
hair2.png 19KB
hair3.png 18KB
clothes7.png 17KB
hair0.png 17KB
hair6.png 17KB
shoe1.png 15KB
shoe2.png 15KB
hair5.png 14KB
shoe3.png 14KB
shoe4.png 14KB
shoe9.png 13KB
shoe10.png 13KB
shoe7.png 13KB
shoe8.png 13KB
shoe6.png 12KB
shoe5.png 12KB
index-active.png 3KB
sort-active.png 3KB
head-active.png 3KB
cal-active.png 3KB
head.png 2KB
index.png 2KB
sort.png 2KB
cal.png 2KB
direc.wxml 4KB
index.wxml 2KB
edit.wxml 770B
index.wxml 512B
index.wxml 250B
index.wxml 194B
logs.wxml 173B
edit.wxml 145B
共 120 条
- 1
- 2
资源评论
故事只若初见
- 粉丝: 23
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功