修复bug 和 拆开操作和图片生成

This commit is contained in:
CPPAlien 2019-11-15 18:03:41 +08:00
parent febb9ba276
commit 100ebfe82f
2 changed files with 91 additions and 52 deletions

View File

@ -17,7 +17,6 @@ Component({
frontContext: {}, frontContext: {},
bottomContext: {}, bottomContext: {},
topContext: {}, topContext: {},
hasIdViews: [],
/** /**
* 组件的属性列表 * 组件的属性列表
*/ */
@ -34,6 +33,14 @@ Component({
} }
}, },
}, },
dancePalette: {
type: Object,
observer: function (newVal, oldVal) {
if (!this.isEmpty(newVal)) {
this.initDancePalette(newVal);
}
},
},
widthPixels: { widthPixels: {
type: Number, type: Number,
value: 0 value: 0
@ -58,6 +65,7 @@ Component({
}, },
methods: { methods: {
/** /**
* 判断一个 object 是否为 * 判断一个 object 是否为
* @param {object} object * @param {object} object
@ -79,9 +87,9 @@ Component({
doAction(newVal) { doAction(newVal) {
if (newVal && newVal.css) { if (newVal && newVal.css) {
if (Array.isArray(this.touchedView.css)) { if (Array.isArray(this.touchedView.css)) {
this.touchedView.css = [...this.touchedView.css, newVal.css] this.touchedView.css = Object.assign({}, ...this.touchedView.css, newVal.css)
} else { } else {
this.touchedView.css = [this.touchedView.css, newVal.css] this.touchedView.css = Object.assign({}, this.touchedView.css, newVal.css)
} }
} }
const draw = { const draw = {
@ -91,7 +99,9 @@ Component({
} }
const pen = new Pen(this.globalContext, draw); const pen = new Pen(this.globalContext, draw);
pen.paint(); pen.paint();
const { rect } = this.touchedView const {
rect
} = this.touchedView
const block = { const block = {
width: this.currentPalette.width, width: this.currentPalette.width,
height: this.currentPalette.height, height: this.currentPalette.height,
@ -151,7 +161,9 @@ Component({
let canBeTouched = [] let canBeTouched = []
for (let i = totalLayerCount - 1; i >= 0; i--) { for (let i = totalLayerCount - 1; i >= 0; i--) {
const view = this.currentPalette.views[i] const view = this.currentPalette.views[i]
const { rect } = view const {
rect
} = view
if (this.inArea(x, y, rect, hasTouchedView)) { if (this.inArea(x, y, rect, hasTouchedView)) {
canBeTouched.push({ canBeTouched.push({
view, view,
@ -179,7 +191,11 @@ Component({
} }
this.touchedView = touchAble[i].view this.touchedView = touchAble[i].view
this.findedIndex = touchAble[i].index this.findedIndex = touchAble[i].index
const { rect, id, css } = this.touchedView const {
rect,
id,
css
} = this.touchedView
this.triggerEvent('touchStart', { this.triggerEvent('touchStart', {
id: id, id: id,
css: css, css: css,
@ -242,8 +258,10 @@ Component({
this.startX = x this.startX = x
this.startY = y this.startY = y
this.startTimeStamp = new Date().getTime() this.startTimeStamp = new Date().getTime()
if (this.touchedView && JSON.stringify(this.touchedView) !== JSON.stringify({})) { if (this.touchedView && !this.isEmpty(this.touchedView)) {
const { rect } = this.touchedView const {
rect
} = this.touchedView
if (rect.right - ACTION_POINT_RADIUS < x && x < rect.right + ACTION_POINT_RADIUS && rect.bottom - ACTION_POINT_RADIUS < y && y < rect.bottom + ACTION_POINT_RADIUS) { if (rect.right - ACTION_POINT_RADIUS < x && x < rect.right + ACTION_POINT_RADIUS && rect.bottom - ACTION_POINT_RADIUS < y && y < rect.bottom + ACTION_POINT_RADIUS) {
this.isScale = true this.isScale = true
this.startH = rect.bottom - rect.top this.startH = rect.bottom - rect.top
@ -256,15 +274,17 @@ Component({
onTouchEnd(e) { onTouchEnd(e) {
const current = new Date().getTime() const current = new Date().getTime()
if ((current - this.startTimeStamp) <= 100 && !this.hasMove) { if ((current - this.startTimeStamp) <= 500 && !this.hasMove) {
this.onClick(e) this.onClick(e)
} else if (this.touchedView && !this.isEmpty(this.touchedView)) {
// 传出镜像
this.triggerEvent('touchEnd', this.currentPalette)
} }
this.hasMove = false this.hasMove = false
this.triggerEvent('touchEnd')
}, },
onTouchCancel() { onTouchCancel(e) {
this.triggerEvent('touchCancel') this.onTouchEnd(e)
}, },
hasMove: false, hasMove: false,
@ -279,7 +299,10 @@ Component({
} = event.touches[0] } = event.touches[0]
const offsetX = x - this.startX const offsetX = x - this.startX
const offsetY = y - this.startY const offsetY = y - this.startY
const { rect, type } = this.touchedView const {
rect,
type
} = this.touchedView
let css = {} let css = {}
if (this.isScale) { if (this.isScale) {
const newW = this.startW + offsetX > 1 ? this.startW + offsetX : 1 const newW = this.startW + offsetX > 1 ? this.startW + offsetX : 1
@ -314,34 +337,27 @@ Component({
}) })
}, },
startPaint() { initScreenK() {
if (this.isEmpty(this.properties.palette)) { if (!(getApp() && getApp().systemInfo && getApp().systemInfo.screenWidth)) {
return;
}
if (!(getApp().systemInfo && getApp().systemInfo.screenWidth)) {
try { try {
getApp().systemInfo = wx.getSystemInfoSync(); getApp().systemInfo = wx.getSystemInfoSync();
} catch (e) { } catch (e) {
const error = `Painter get system info failed, ${JSON.stringify(e)}`; console.error(`Painter get system info failed, ${JSON.stringify(e)}`);
this.triggerEvent('imgErr', {
error: error
});
console.error(error);
return; return;
} }
} }
let screenK = getApp().systemInfo.screenWidth / 750; this.screenK = 0.5;
setStringPrototype(screenK, 1); if (getApp() && getApp().systemInfo && getApp().systemInfo.screenWidth) {
this.screenK = getApp().systemInfo.screenWidth / 750;
this.downloadImages().then((palette) => {
this.currentPalette = palette
this.hasIdViews = []
palette.views && palette.views.map(view => {
if (view.id) {
this.hasIdViews.push(view)
} }
}) setStringPrototype(this.screenK, 1);
},
initDancePalette() {
this.initScreenK();
this.downloadImages(this.properties.dancePalette).then((palette) => {
this.currentPalette = palette
const { const {
width, width,
height height
@ -351,33 +367,55 @@ Component({
console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`); console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`);
return; return;
} }
this.setData({
painterStyle: `width:${width.toPx()}px;height:${height.toPx()}px;`,
});
this.frontContext = wx.createCanvasContext('front', this);
this.bottomContext = wx.createCanvasContext('bottom', this);
this.topContext = wx.createCanvasContext('top', this);
this.globalContext = wx.createCanvasContext('k-canvas', this);
new Pen(this.globalContext, palette).paint();
});
},
startPaint() {
this.initScreenK();
this.downloadImages(this.properties.palette).then((palette) => {
const {
width,
height
} = palette;
if (!width || !height) {
console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`);
return;
}
// 生成图片时,根据设置的像素值重新绘制
this.canvasWidthInPx = width.toPx(); this.canvasWidthInPx = width.toPx();
if (this.properties.widthPixels) { if (this.properties.widthPixels) {
// 如果重新设置过像素宽度,则重新设置比例 setStringPrototype(this.screenK, this.properties.widthPixels / this.canvasWidthInPx)
setStringPrototype(screenK, this.properties.widthPixels / this.canvasWidthInPx)
this.canvasWidthInPx = this.properties.widthPixels this.canvasWidthInPx = this.properties.widthPixels
} }
this.canvasHeightInPx = height.toPx(); this.canvasHeightInPx = height.toPx();
this.setData({ this.setData({
painterStyle: `width:${this.canvasWidthInPx}px;height:${this.canvasHeightInPx}px;`, photoStyle: `width:${this.canvasWidthInPx}px;height:${this.canvasHeightInPx}px;`,
}); });
this.globalContext = wx.createCanvasContext('k-canvas', this); const photoContext = wx.createCanvasContext('photo', this);
const pen = new Pen(this.globalContext, palette);
pen.paint(() => { new Pen(photoContext, palette).paint(() => {
this.saveImgToLocal(); this.saveImgToLocal();
}); });
this.frontContext = wx.createCanvasContext('front', this);
this.bottomContext = wx.createCanvasContext('bottom', this);
this.topContext = wx.createCanvasContext('top', this)
}); });
}, },
downloadImages() { downloadImages(palette) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let preCount = 0; let preCount = 0;
let completeCount = 0; let completeCount = 0;
const paletteCopy = JSON.parse(JSON.stringify(this.properties.palette)); const paletteCopy = JSON.parse(JSON.stringify(palette));
if (paletteCopy.background) { if (paletteCopy.background) {
preCount++; preCount++;
downloader.download(paletteCopy.background).then((path) => { downloader.download(paletteCopy.background).then((path) => {
@ -438,7 +476,7 @@ Component({
const that = this; const that = this;
setTimeout(() => { setTimeout(() => {
wx.canvasToTempFilePath({ wx.canvasToTempFilePath({
canvasId: 'k-canvas', canvasId: 'photo',
success: function (res) { success: function (res) {
that.getImageInfo(res.tempFilePath); that.getImageInfo(res.tempFilePath);
}, },
@ -510,7 +548,7 @@ function setStringPrototype(screenK, scale) {
let res = 0; let res = 0;
if (unit === 'rpx') { if (unit === 'rpx') {
res = Math.round(value * screenK * (scale || 1)); res = Math.round(value * (screenK || 0.5) * (scale || 1));
} else if (unit === 'px') { } else if (unit === 'px') {
res = Math.round(value * (scale || 1)); res = Math.round(value * (scale || 1));
} }

View File

@ -1,4 +1,5 @@
<view style='position: relative;{{painterStyle}}{{customStyle}}'> <view style='position: relative;{{customStyle}}'>
<canvas canvas-id="photo" style="{{photoStyle}};position: absolute;top: -9999rpx;" />
<canvas canvas-id="bottom" style="{{painterStyle}};position: absolute;" /> <canvas canvas-id="bottom" style="{{painterStyle}};position: absolute;" />
<canvas canvas-id="k-canvas" style="{{painterStyle}};position: absolute;" /> <canvas canvas-id="k-canvas" style="{{painterStyle}};position: absolute;" />
<canvas canvas-id="top" style="{{painterStyle}};position: absolute;" /> <canvas canvas-id="top" style="{{painterStyle}};position: absolute;" />