Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faet: circle render to image #5634

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/circle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ Page({
<van-circle value="{{ value }}" size="120" text="大小定制" />
```

### Canvas 转为 Image 方式呈现
通过`render-to-image`属性设置生成图片。

```html
<van-circle value="{{ value }}" render-to-image size="120" text="这是张图片" />
```

## API

### Props
Expand All @@ -102,6 +109,8 @@ Page({
| text | 文字 | _string_ | - |
| stroke-width | 进度条宽度 | _number_ | `4` |
| clockwise | 是否顺时针增加 | _boolean_ | `true` |
| reander-to-image | 渲染成图片 | _boolean_ | `false` |


### Slots

Expand Down
4 changes: 4 additions & 0 deletions packages/circle/demo/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
<van-circle value="{{ value }}" size="120" text="大小定制" />
</demo-block>

<demo-block title="canvas转为Image方式呈现">
<van-circle value="{{ value }}" render-to-image size="120" text="这是张图片" />
</demo-block>

<van-button type="primary" size="small" data-step="10" bind:tap="run">增加</van-button>
<van-button type="danger" size="small" data-step="-10" bind:tap="run">减少</van-button>
35 changes: 34 additions & 1 deletion packages/circle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ VantComponent({
type: Boolean,
value: true,
},
renderToImage:{
type: Boolean,
value: false,
}
},

data: {
hoverColor: BLUE,
circleImg:''
},

methods: {
Expand Down Expand Up @@ -176,6 +181,29 @@ VantComponent({
});
},

generateCircleImage():Promise<string>{
const {size}=this.data
const dpr = getSystemInfoSync().pixelRatio;

return new Promise((resolve,reject) => {
setTimeout(()=>{
wx.canvasToTempFilePath({
width:size,
height:size,
destWidth:size* dpr,
destHeight:size* dpr,
canvasId: 'van-circle',
success:(res)=> {
resolve(res.tempFilePath)
},
fail(err){
reject(err)
}
},this)
},200)
})
},

reRender() {
// tofector 动画暂时没有想到好的解决方案
const { value, speed } = this.data;
Expand Down Expand Up @@ -220,7 +248,12 @@ VantComponent({

this.setHoverColor().then(() => {
this.drawCircle(this.currentValue);
});
}).then(async ()=>{
if(this.data.renderToImage){
const circleImg=await this.generateCircleImage()
this.setData({circleImg})
}
})
},

destroyed() {
Expand Down