Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.
Open
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
34 changes: 19 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ Component({
},
lifetimes: {
attached() {
const dpr = wx.getSystemInfoSync().pixelRatio
const query = this.createSelectorQuery()
this.dpr = dpr
query.select('#canvas')
.fields({node: true, size: true})
.exec(res => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
ctx.scale(dpr, dpr)
this.ctx = ctx
this.canvas = canvas
})
this.initCtx = new Promise(resolve => {
const dpr = wx.getSystemInfoSync().pixelRatio
const query = this.createSelectorQuery()
this.dpr = dpr
query.select('#canvas')
.fields({node: true, size: true})
.exec(res => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
ctx.scale(dpr, dpr)
this.ctx = ctx
this.canvas = canvas
resolve()
})
})
}
},
methods: {
async renderToCanvas(args) {
const {wxml, style} = args
await this.initCtx

const {wxml, style} = args
// 清空画布
const ctx = this.ctx
const canvas = this.canvas
Expand Down