Skip to content

Commit

Permalink
Merge pull request #72 from jasondu/fix/1.1.9
Browse files Browse the repository at this point in the history
Fix/1.1.9
  • Loading branch information
jasondu authored Jan 22, 2019
2 parents 2bdbd5f + 811cef7 commit 1e7f201
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 267 deletions.
56 changes: 54 additions & 2 deletions miniprogram_dist/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,68 @@ Component({
this.factor = screenWidth / 750;
},
methods: Object.assign({
/**
* 计算画布的高度
* @param {*} config
*/
getHeight(config) {
const getTextHeight = (text) => {
let fontHeight = text.lineHeight || text.fontSize;
let height = 0;
if (text.baseLine === 'top') {
height = fontHeight;
} else if (text.baseLine === 'middle') {
height = fontHeight / 2;
} else {
height = 0;
}
return height;
}
const heightArr = [];
(config.blocks || []).forEach((item) => {
heightArr.push(item.y + item.height);
});
(config.texts || []).forEach((item) => {
let height;
if (Object.prototype.toString.call(item.text) === '[object Array]') {
item.text.forEach((i) => {
height = getTextHeight({...i, baseLine: item.baseLine});
heightArr.push(item.y + height);
});
} else {
height = getTextHeight(item);
heightArr.push(item.y + height);
}
});
(config.images || []).forEach((item) => {
heightArr.push(item.y + item.height);
});
(config.lines || []).forEach((item) => {
heightArr.push(item.startY);
heightArr.push(item.endY);
});
const sortRes = heightArr.sort((a, b) => b - a);
let canvasHeight = 0;
if (sortRes.length > 0) {
canvasHeight = sortRes[0];
}
if (config.height < canvasHeight || !config.height) {
return canvasHeight;
} else {
return config.height;
}
},
create(config) {
this.ctx = wx.createCanvasContext('canvasid', this);

this.initCanvas(config.width, config.height, config.debug)
const height = this.getHeight(config);
this.initCanvas(config.width, height, config.debug)
.then(() => {
// 设置画布底色
if (config.backgroundColor) {
this.ctx.save();
this.ctx.setFillStyle(config.backgroundColor);
this.ctx.fillRect(0, 0, this.toPx(config.width), this.toPx(config.height));
this.ctx.fillRect(0, 0, this.toPx(config.width), this.toPx(height));
this.ctx.restore();
}
const { texts = [], images = [], blocks = [], lines = [] } = config;
Expand Down
9 changes: 6 additions & 3 deletions miniprogram_dist/poster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ Component({
}
this.listener[event] = fun;
},
downloadResource() {
downloadResource(reset) {
return new Promise((resolve, reject) => {
if (reset) {
this.downloadStatus = null;
}
const poster = this.selectComponent('#poster');
if (this.downloadStatus && this.downloadStatus !== 'fail') {
if (this.downloadStatus === 'success') {
Expand All @@ -58,9 +61,9 @@ Component({
}
})
},
onCreate() {
onCreate(reset = false) {
!this.data.hideLoading && wx.showLoading({ mask: true, title: '生成中' });
return this.downloadResource().then(() => {
return this.downloadResource(typeof reset === 'boolean' && reset).then(() => {
!this.data.hideLoading && wx.hideLoading();
const poster = this.selectComponent('#poster');
poster.create(this.data.config);
Expand Down
4 changes: 2 additions & 2 deletions miniprogram_dist/poster/poster.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function Poster(options = {}) {
return poster;
};

Poster.create = () => {
Poster.create = (reset = false) => {
const poster = Poster();
if (!poster) {
console.error('请设置组件的id="poster"!!!');
} else {
return Poster().onCreate();
return Poster().onCreate(reset);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wxa-plugin-canvas",
"version": "1.1.8",
"version": "1.1.9",
"description": "小程序海报组件",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 1e7f201

Please sign in to comment.