Canvas装载文字两种格式

本文介绍了使用canvas绘制文字的基本属性设置,包括字体、对齐方式,并详细解释了阴影效果的实现方法,提供了实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

context对象可以设置以下 text 属性:
[color=indigo][b]font[/b][/color]:文字字体,同 CSSfont-family 属性
[color=indigo][b]textAlign[/b][/color]:文字水平对齐方式。可取属性值: start, end, left,right, center。默认值:start.
[color=indigo][b]textBaseline[/b][/color]:文字竖直对齐方式。可取属性值:top, hanging, middle,alphabetic, ideographic, bottom。默认值:alphabetic

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

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

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

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

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


下面是
canvas 阴影的例子:
• [color=red]context.shadowOffsetX[/color] = 5;
• [color=red]context.shadowOffsetY[/color] = 5;
• [color=red]context.shadowBlur[/color] = 4;
• [color=red]context.shadowColor[/color] = ‘rgba(255, 0, 0, 0.5)’;
• [color=red]context.fillStyle[/color] = ‘#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值