From 5f322f93059feb23a30961f21971997add19d806 Mon Sep 17 00:00:00 2001 From: smackgg Date: Wed, 15 Jan 2020 14:19:52 +0800 Subject: [PATCH] renderToCanvas should await initCtx --- src/index.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index e25084c..b32b019 100644 --- a/src/index.js +++ b/src/index.js @@ -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