Skip to content

Commit

Permalink
merge feat 1.1.10 add pixpixelRatio
Browse files Browse the repository at this point in the history
  • Loading branch information
jingguangyan committed Jul 9, 2019
2 parents a8a0bc1 + ab3a11c commit 58537ed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -68,6 +68,7 @@ yarn add wxa-plugin-canvas --production
| height | Number(单位:rpx) || 画布高度 |
| backgroundColor | String || 画布颜色 |
| debug | Boolean || false隐藏canvas,true显示canvas,默认false |
| pixelRatio | Number || 1为一般,值越大越清晰 |
| preload | Boolean || true:图片资源预下载 默认false |
| hide-loading | Boolean || true:隐藏loading 默认false |
| blocks | Object Array(对象数组) || 看下文 |
Expand Down
16 changes: 9 additions & 7 deletions miniprogram_dist/index/index.js
Expand Up @@ -120,10 +120,10 @@ const main = {
this.ctx.closePath();
this.ctx.restore();
},
downloadResource(images = []) {
downloadResource({ images = [], pixelRatio = 1 }) {
const drawList = [];
this.drawArr = [];
images.forEach((image, index) => drawList.push(this._downloadImageAndInfo(image, index)));
images.forEach((image, index) => drawList.push(this._downloadImageAndInfo(image, index, pixelRatio)));
return Promise.all(drawList);
},
initCanvas(w, h, debug) {
Expand Down Expand Up @@ -245,7 +245,7 @@ const helper = {
/**
* 下载图片并获取图片信息
*/
_downloadImageAndInfo(image, index) {
_downloadImageAndInfo(image, index, pixelRatio) {
return new Promise((resolve, reject) => {
const { x, y, url, zIndex } = image;
const imageUrl = url;
Expand All @@ -260,8 +260,8 @@ const helper = {
const borderRadius = image.borderRadius || 0;
const setWidth = image.width;
const setHeight = image.height;
const width = this.toRpx(imgInfo.width);
const height = this.toRpx(imgInfo.height);
const width = this.toRpx(imgInfo.width / pixelRatio);
const height = this.toRpx(imgInfo.height / pixelRatio);

if (width / height <= setWidth / setHeight) {
sx = 0;
Expand Down Expand Up @@ -337,9 +337,10 @@ const helper = {
},
toPx(rpx, int) {
if (int) {
return parseInt(rpx * this.factor);
return parseInt(rpx * this.factor * this.pixelRatio);
}
return rpx * this.factor;
return rpx * this.factor * this.pixelRatio;

},
toRpx(px, int) {
if (int) {
Expand Down Expand Up @@ -428,6 +429,7 @@ Component({
create(config) {
this.ctx = wx.createCanvasContext('canvasid', this);

this.pixelRatio = config.pixelRatio || 1;
const height = this.getHeight(config);
this.initCanvas(config.width, height, config.debug)
.then(() => {
Expand Down
4 changes: 2 additions & 2 deletions miniprogram_dist/poster/index.js
Expand Up @@ -17,7 +17,7 @@ Component({
if (this.data.preload) {
const poster = this.selectComponent('#poster');
this.downloadStatus = 'doing';
poster.downloadResource(this.data.config.images).then(() => {
poster.downloadResource(this.data.config).then(() => {
this.downloadStatus = 'success';
this.trigger('downloadSuccess');
}).catch((e) => {
Expand Down Expand Up @@ -52,7 +52,7 @@ Component({
this.once('downloadFail', (e) => reject(e));
}
} else {
poster.downloadResource(this.data.config.images)
poster.downloadResource(this.data.config)
.then(() => {
this.downloadStatus = 'success';
resolve();
Expand Down
1 change: 1 addition & 0 deletions pages/index/index.js
Expand Up @@ -7,6 +7,7 @@ const posterConfig = {
height: 1334,
backgroundColor: '#fff',
debug: false,
pixelRatio: 1,
blocks: [
{
width: 690,
Expand Down

0 comments on commit 58537ed

Please sign in to comment.