PainterCore/painter.js

206 lines
5.7 KiB
JavaScript
Raw Normal View History

2018-07-05 15:27:12 +08:00
import Pen from './lib/pen';
import Downloader from './lib/downloader';
2018-07-10 18:24:28 +08:00
const util = require('./lib/util');
2018-07-05 15:27:12 +08:00
const downloader = new Downloader();
// 最大尝试的绘制次数
const MAX_PAINT_COUNT = 5;
Component({
canvasWidthInPx: 0,
canvasHeightInPx: 0,
paintCount: 0,
/**
* 组件的属性列表
*/
properties: {
customStyle: {
type: String,
},
palette: {
type: Object,
observer: function (newVal, oldVal) {
if (this.isNeedRefresh(newVal, oldVal)) {
this.paintCount = 0;
this.startPaint();
}
},
},
},
data: {
picURL: '',
showCanvas: true,
painterStyle: '',
},
2018-07-10 18:24:28 +08:00
attached() {
setStringPrototype();
},
2018-07-05 15:27:12 +08:00
methods: {
/**
* 判断一个 object 是否为
* @param {object} object
*/
isEmpty(object) {
for (const i in object) {
return false;
}
return true;
},
isNeedRefresh(newVal, oldVal) {
if (!newVal) {
return false;
}
return true;
},
startPaint() {
if (this.isEmpty(this.properties.palette)) {
return;
}
2018-07-10 18:24:28 +08:00
if (!getApp().painterScreenRatio) {
2018-07-05 15:27:12 +08:00
try {
2018-07-10 18:24:28 +08:00
const systemInfo = wx.getSystemInfoSync();
getApp().painterScreenRatio = systemInfo.screenWidth / 750;
2018-07-05 15:27:12 +08:00
} catch (e) {
2018-07-10 18:24:28 +08:00
const error = `Painter get system info failed, ${JSON.stringify(e)}`;
that.triggerEvent('imgErr', { error: error });
console.error(error);
return;
2018-07-05 15:27:12 +08:00
}
}
2018-07-10 18:24:28 +08:00
screenK = getApp().painterScreenRatio;
2018-07-05 15:27:12 +08:00
this.downloadImages().then((palette) => {
const { width, height } = palette;
this.canvasWidthInPx = width.toPx();
this.canvasHeightInPx = height.toPx();
if (!width || !height) {
console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`);
return;
}
this.setData({
painterStyle: `width:${width};height:${height};`,
});
const ctx = wx.createCanvasContext('k-canvas', this);
const pen = new Pen(ctx, palette);
pen.paint(() => {
this.saveImg();
});
2018-07-10 18:24:28 +08:00
});
2018-07-05 15:27:12 +08:00
},
downloadImages() {
return new Promise((resolve, reject) => {
let preCount = 0;
let completeCount = 0;
const paletteCopy = JSON.parse(JSON.stringify(this.properties.palette));
2018-07-10 18:24:28 +08:00
if (paletteCopy.background && util.isValidUrl(paletteCopy.background)) {
preCount++;
2018-07-05 15:27:12 +08:00
downloader.download(paletteCopy.background).then((path) => {
paletteCopy.background = path;
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
}, () => {
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
});
}
if (paletteCopy.views) {
for (const view of paletteCopy.views) {
2018-07-10 18:24:28 +08:00
if (view && view.type === 'image' && view.url && util.isValidUrl(view.url)) {
preCount++;
/* eslint-disable no-loop-func */
2018-07-05 15:27:12 +08:00
downloader.download(view.url).then((path) => {
view.url = path;
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
}, () => {
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
});
}
}
}
if (preCount === 0) {
resolve(paletteCopy);
}
2018-07-10 18:24:28 +08:00
});
2018-07-05 15:27:12 +08:00
},
saveImg() {
const that = this;
setTimeout(() => {
wx.canvasToTempFilePath({
canvasId: 'k-canvas',
success: function (res) {
wx.getImageInfo({
src: res.tempFilePath,
success: (infoRes) => {
if (that.paintCount > MAX_PAINT_COUNT) {
const error = `The result is always fault, even we tried ${MAX_PAINT_COUNT} times`;
console.error(error);
that.triggerEvent('imgErr', { error: error });
return;
}
// 比例相符时才证明绘制成功,否则进行强制重绘制
if (Math.abs((infoRes.width * that.canvasHeightInPx - that.canvasWidthInPx * infoRes.height) / (infoRes.height * that.canvasHeightInPx)) < 0.01) {
that.triggerEvent('imgOK', { path: res.tempFilePath });
} else {
that.startPaint();
}
that.paintCount++;
},
fail: (error) => {
console.error(`getImageInfo failed, ${JSON.stringify(error)}`);
that.triggerEvent('imgErr', { error: error });
},
});
},
fail: function (error) {
console.error(`canvasToTempFilePath failed, ${JSON.stringify(error)}`);
that.triggerEvent('imgErr', { error: error });
},
}, this);
}, 300);
},
},
});
2018-07-10 18:24:28 +08:00
let screenK = 0.5;
2018-07-05 15:27:12 +08:00
2018-07-10 18:24:28 +08:00
function setStringPrototype() {
/* eslint-disable no-extend-native */
String.prototype.toPx = function toPx() {
const reg = /^[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g;
const results = reg.exec(this);
if (!this || !results) {
console.error(`The size: ${this} is illegal`);
return 0;
}
const unit = results[2];
const value = parseFloat(this);
2018-07-05 15:27:12 +08:00
2018-07-10 18:24:28 +08:00
let res = 0;
if (unit === 'rpx') {
res = value * screenK;
} else if (unit === 'px') {
res = value;
}
return res;
};
}