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

大佬,关于在自定义组件中实现绘图 #436

Open
liangshiwu opened this issue Feb 25, 2021 · 1 comment
Open

大佬,关于在自定义组件中实现绘图 #436

liangshiwu opened this issue Feb 25, 2021 · 1 comment

Comments

@liangshiwu
Copy link

在自定义组件中canvas组件注册无效,这个因为createCanvasContext方法是有两个参数,在page页面默认传了一个this,在组件里面需要手动传this。
所以修改一下才可以在自定义组件中使用,

this.context = wx.createCanvasContext(opts.canvasId, opts.that);

@xBinKai
Copy link

xBinKai commented Aug 17, 2023

@liangshiwu 感谢提供的信息。
调整了代码,现在可以在自定义组件中使用了:

  1. 修改wxcharts.js源代码如下:
var Charts = function Charts(opts) {
  ...
  // this.context = wx.createCanvasContext(opts.canvasId); 为解决组件内不显示问题,替换为以下代码
  this.context = opts.that ? wx.createCanvasContext(opts.canvasId, opts.that) : wx.createCanvasContext(opts.canvasId);
  ...
}
  1. 组件中使用时:
new wxCharts({
  ...
  that: this  // 传递组件的this上下文
});
  1. 注意在自定义组件wxml中不能使用canvas的2d接口,不可以使用type="2d".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants