Canvas装载文字两种格式

本文介绍了如何使用Canvas API设置文字样式并绘制文字,包括设置字体、对齐方式及基线等属性。此外还详细讲解了如何应用阴影效果增强图形的表现力。
context对象可以设置以下 text 属性:
font:文字字体,同 CSSfont-family 属性
textAlign:文字水平对齐方式。可取属性值: start, end, left,right, center。默认值:start.
textBaseline:文字竖直对齐方式。可取属性值:top, hanging, middle,alphabetic, ideographic, bottom。默认值:alphabetic

有两个方法可以绘制文字: fillText和 strokeText。第一个绘制带 fillStyle 填充的文字,后者绘制只有 strokeStyle 边框的文字。两者的参数相同:要绘制的文字和文字的位置(x,y) 坐标。还有一个可选选项——最大宽度。如果需要的话,浏览器会缩减文字以让它适应指定宽度。文字对齐属性影响文字与设置的(x,y) 坐标的相对位置。

下面是一个在 canvas 中绘制”hello world” 文字的例子

context.fillStyle    = ‘#00f’;  
context.font         = ‘italic 30px sans-serif’;  
context.textBaseline = ‘top’;  
context.fillText  (‘Hello world!’, 0, 0);  
context.font         = ‘bold 30px sans-serif’;  
context.strokeText(‘Hello world!’, 0, 50);  

阴影
目前只有 Konqueror 和 Firefox 3.1 nightly build 支持 Shadows API 。API 的属性为

shadowColor:阴影颜色。其值和 CSS 颜色值一致。
shadowBlur:设置阴影模糊程度。此值越大,阴影越模糊。其效果和 Photoshop 的高斯模糊滤镜相同。
shadowOffsetX 和 shadowOffsetY:阴影的 x 和 y 偏移量,单位是像素。


下面是
canvas 阴影的例子:
context.shadowOffsetX = 5;  
context.shadowOffsetY = 5;  
context.shadowBlur    = 4;  
context.shadowColor   = ‘rgba(255, 0, 0, 0.5)’;  
context.fillStyle     = ‘#00f’;  
• context.fillRect(20, 20, 150, 100); 
``` import tkinter as tk from tkinter import ttk from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import numpy as np from mpl_toolkits.mplot3d import Axes3D class PackingSystemApp: def __init__(self, master): self.master = master self.master.title("飞机拖车码垛系统") # 创建3D图形 self.fig = Figure(figsize=(5, 4), dpi=100) self.ax = self.fig.add_subplot(111, projection='3d') self.ax.set_xlim([0, 200]) self.ax.set_ylim([0, 80]) self.ax.set_zlim([0, 80]) self.canvas = FigureCanvasTkAgg(self.master, self.fig) self.canvas.draw() self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 创建待载行李数量标签 self.label = ttk.Label(self.master, text="待载行李数量:8") self.label.pack(side=tk.TOP, fill=tk.X) # 创建START和PAUSE按钮 self.start_button = ttk.Button(self.master, text="START", command=self.start) self.start_button.pack(side=tk.TOP, fill=tk.X) self.pause_button = ttk.Button(self.master, text="PAUSE", command=self.pause) self.pause_button.pack(side=tk.TOP, fill=tk.X) # 创建显示区域 self.display_frame = ttk.LabelFrame(self.master) self.display_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True) self.fill_rate_label = ttk.Label(self.display_frame, text="填充率:0 % 时间:0 s") self.fill_rate_label.pack(side=tk.TOP, fill=tk.X) # 创建控制按钮 self.control_frame = ttk.Frame(self.master) self.control_frame.pack(side=tk.BOTTOM, fill=tk.X) self.start_button_control = ttk.Button(self.control_frame, text="开启", command=self.start_simulation) self.start_button_control.pack(side=tk.LEFT) self.return_button_control = ttk.Button(self.control_frame, text="返回", command=self.return_to_main) self.return_button_control.pack(side=tk.LEFT) self.close_button_control = ttk.Button(self.control_frame, text="关闭", command=self.close) self.close_button_control.pack(side=tk.RIGHT) def start(self): print("Start button clicked") def pause(self): print("Pause button clicked") def start_simulation(self): print("Simulation started") def return_to_main(self): print("Return to main") def close(self): self.master.destroy() if __name__ == "__main__": root = tk.Tk() app = PackingSystemApp(root) root.mainloop()```运行后仍然报错,这是错误信息。D:\py project\pythonProject2\1.py:32: UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from font(s) DejaVu Sans. self.canvas.draw() Traceback (most recent call last): File "D:\py project\pythonProject2\1.py", line 73, in <module> app = PackingSystemApp(root) File "D:\py project\pythonProject2\1.py", line 45, in __init__ self.pause_button = ttk.Button(control_top_frame, text="PAUSE", command=self.pause) AttributeError: 'PackingSystemApp' object has no attribute 'pause'
最新发布
03-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值