揭秘CanvasContext.draw:为何它是异步的

在这里插入图片描述

微信小程序中的Canvas绘图功能为开发者提供了一个强大的工具箱,让绘制图形变得更加直观和灵活。Canvas绘图上下文(CanvasContext)就像是一个画家手中的画笔,可以用来绘制线条、填充颜色、甚至绘制复杂的图像。但与传统的同步绘图方式不同,CanvasContext提供的draw方法是一个异步函数,这意味着它的执行不会立刻阻塞主线程,而是会在合适的时间点完成绘图操作。

为何draw方法采用异步模式呢?这是因为微信小程序的运行环境需要确保界面的流畅性。如果绘图操作是同步执行的,那么在执行复杂绘图任务时,可能会导致界面卡顿,影响用户体验。异步绘图的好处在于它可以将耗时的绘图操作放到后台线程执行,从而释放主线程的压力,保证UI响应速度。

const canvas = wx.createCanvasContext('myCanvas')
canvas.setFillStyle('red')
canvas.fillRect(10, 10, 100, 100)
canvas.draw(true, () => {
   
   
  console.log('绘图完成')
})

动手实践:异步绘图的妙用

既然了解了draw方法为何是异步的,那么如何在实际开发中发挥它的优势呢?下面我们就通过几个简单的例子来探索异步绘图的魅力。

异步绘图的基本用法示例

在使用异步绘图时,最重要的是掌握回调函数的使用。回调函数会在绘图操作完成后被调用,因此可以在其中执行一些依赖于绘图结果的操作。

function drawAsync(canvasCtx) {
   
   
  canvasCtx.setFillStyle('#ff0000')
  canvasCtx.fillRect(30, 30, 150, 150)
  canvasCtx.draw(true, () => {
   
   
    console.log('红色方块绘制完成')
    // 这里可以添加更多绘图操作
  })
}

const canvasCtx = wx.createCanvasContext('myCanvas')
drawAsync(canvasCtx)

如何利用异步特性优化性能

除了基本的绘图操作外,异步绘图还可以帮助我们优化性能。例如,在绘制一系列连续的帧时,可以利用异步特性来避免过度消耗CPU资源。通过将每一帧的绘制操作分散到不同的时间点,可以降低每次绘图对主线程的影响。

function animate(canvasCtx, frameCount) {
   
   
  if (frameCount > 0) {
   
   
    canvasCtx.clearRect(0, 0, 300, 200)
    canvasCtx.setFillStyle('#00ff00')
    canvasCtx.
D:/0125py/text3.py:116: UserWarning: Glyph 21015 (\N{CJK UNIFIED IDEOGRAPH-5217}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 36710 (\N{CJK UNIFIED IDEOGRAPH-8F66}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 25509 (\N{CJK UNIFIED IDEOGRAPH-63A5}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 32452 (\N{CJK UNIFIED IDEOGRAPH-7EC4}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 32455 (\N{CJK UNIFIED IDEOGRAPH-7EC7}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 26102 (\N{CJK UNIFIED IDEOGRAPH-65F6}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 38388 (\N{CJK UNIFIED IDEOGRAPH-95F4}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 37319 (\N{CJK UNIFIED IDEOGRAPH-91C7}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 38598 (\N{CJK UNIFIED IDEOGRAPH-96C6}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 25240 (\N{CJK UNIFIED IDEOGRAPH-6298}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 65288 (\N{FULLWIDTH LEFT PARENTHESIS}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 38047 (\N{CJK UNIFIED IDEOGRAPH-949F}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 65289 (\N{FULLWIDTH RIGHT PARENTHESIS}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 27969 (\N{CJK UNIFIED IDEOGRAPH-6D41}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 31243 (\N{CJK UNIFIED IDEOGRAPH-7A0B}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 22343 (\N{CJK UNIFIED IDEOGRAPH-5747}) missing from current font. self.canvas.draw() D:/0125py/text3.py:186: UserWarning: Glyph 21015 (\N{CJK UNIFIED IDEOGRAPH-5217}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 36710 (\N{CJK UNIFIED IDEOGRAPH-8F66}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 25509 (\N{CJK UNIFIED IDEOGRAPH-63A5}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 32452 (\N{CJK UNIFIED IDEOGRAPH-7EC4}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 32455 (\N{CJK UNIFIED IDEOGRAPH-7EC7}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 26102 (\N{CJK UNIFIED IDEOGRAPH-65F6}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 38388 (\N{CJK UNIFIED IDEOGRAPH-95F4}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 37319 (\N{CJK UNIFIED IDEOGRAPH-91C7}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 38598 (\N{CJK UNIFIED IDEOGRAPH-96C6}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 25240 (\N{CJK UNIFIED IDEOGRAPH-6298}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 65288 (\N{FULLWIDTH LEFT PARENTHESIS}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 38047 (\N{CJK UNIFIED IDEOGRAPH-949F}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 65289 (\N{FULLWIDTH RIGHT PARENTHESIS}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 27969 (\N{CJK UNIFIED IDEOGRAPH-6D41}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 31243 (\N{CJK UNIFIED IDEOGRAPH-7A0B}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 22343 (\N{CJK UNIFIED IDEOGRAPH-5747}) missing from current font. dialog.exec()
08-30
你遇到的是一组 **Matplotlib 字体警告**: ``` UserWarning: Glyph XXX (\N{CJK ...}) missing from current font. ``` --- ## ✅ 问题解释: 这些警告说明 Matplotlib **当前使用的字体不支持中文字符**(CJK = Chinese, Japanese, Korean),导致中文字符无法正常显示,出现缺失警告。 --- ## ✅ 解决方法: 你需要为 Matplotlib 设置一个**支持中文的字体**,例如 `SimHei`(黑体)、`Microsoft YaHei`(微软雅黑)等。 --- ## ✅ 解决方案一:代码中临时设置字体 在你的代码中添加以下代码,**在绘图前设置字体**: ```python import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体显示中文 plt.rcParams['axes.unicode_minus'] = False # 正常显示负号 ``` ### ✅ 添加位置示例: 在你的代码中,找到绘制图表的部分,在 `import matplotlib.pyplot as plt` 之后、绘图之前加上: ```python # 设置中文字体和解决负号显示问题 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False ``` --- ## ✅ 解决方案二:永久修改 Matplotlib 配置文件 如果你希望**所有 Matplotlib 程序都能正常显示中文**,可以修改 Matplotlib 的配置文件: ### 1. 找到配置文件路径: ```python import matplotlib print(matplotlib.matplotlib_fname()) ``` 输出类似: ``` C:\Python39\lib\site-packages\matplotlib\mpl-data\matplotlibrc ``` ### 2. 编辑该文件: 用文本编辑器打开 `matplotlibrc` 文件,修改以下两行: ```ini font.family : sans-serif font.sans-serif : DejaVu Sans, Bitstream Vera Sans, ... -> 添加 SimHei axes.unicode_minus : True -> 改为 False ``` 修改后应类似: ```ini font.family : sans-serif font.sans-serif : SimHei, DejaVu Sans, Bitstream Vera Sans, ... axes.unicode_minus : False ``` --- ## ✅ 示例:修改后的代码片段(关键部分) ```python import matplotlib.pyplot as plt import numpy as np from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas from matplotlib.ticker import FixedLocator, FixedFormatter from PySide6.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QComboBox, QTableWidget, QTableWidgetItem, QHeaderView # 设置中文字体和解决负号显示问题 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 数据和流程名称保持不变 data = { "T001": [167, 113, 99, 54, 58, 140], "T002": [136, 111, 172, 125, 72, 68], "T003": [121, 134, 63, 82, 148, 59], "T004": [109, 76, 64, 60, 129, 121], } process_names = ['接车', '送车1', '送车2', '确报1', '确报2', '编成'] ``` --- ## ✅ 常见支持中文的字体列表: | 字体名 | 中文名称 | |---------------------|----------------| | `SimHei` | 黑体 | | `Microsoft YaHei` | 微软雅黑 | | `FangSong` | 仿宋 | | `KaiTi` | 楷体 | | `LiSu` | 隶书 | 你可以根据系统中安装的字体进行选择。 --- ## ✅ 验证字体是否可用: ```python import matplotlib.font_manager # 查看所有可用字体 fonts = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]) print(fonts) ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值