当前位置:网站首页>canvas文字绘制(大小、粗体、倾斜、对齐、基线)
canvas文字绘制(大小、粗体、倾斜、对齐、基线)
2022-08-11 09:02:00 【好奇的菜鸟】
一、fillText()函数绘制文字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//设置填充文字样式
ctx.font = "20px Georgia";
//设置文字及其位置
ctx.fillText("Hello World", 10, 50);
ctx.font = "30px Verdana"
//设置线性渐变色
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta")
gradient.addColorStop("0.5", "blue")
gradient.addColorStop("1.0", "red")
ctx.fillStyle = gradient;
ctx.fillText("Big smile!", 10, 90)
}
</script>
</html>

二、strokeText()函数绘制文字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//设置填充文字样式
ctx.font = "20px Georgia";
//设置文字及其位置
ctx.fillText("Hello World", 10, 50);
ctx.font = "30px Verdana"
//设置线性渐变色
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta")
gradient.addColorStop("0.5", "blue")
gradient.addColorStop("1.0", "red")
ctx.strokeStyle = gradient;
ctx.strokeText("Big smile!", 10, 90)
}
</script>
</html>

三、设置文字大小
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//设置填充文字样式,大小20px
ctx.font = "20px Georgia";
//设置文字及其位置
ctx.fillText("Hello World", 10, 50);
//设置填充文字样式,大小30px
ctx.font = "30px Verdana"
ctx.fillText("Hello World", 10, 90);
}
</script>
</html>

四、设置文字字体
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//设置填充文字样式,字体Georgia
ctx.font = "20px Georgia";
//设置文字及其位置
ctx.fillText("Hello World(Georgia)", 10, 50);
//设置填充文字样式,大小20px
ctx.font = "20px Verdana"
ctx.fillText("Hello World(Verdana)", 10, 90);
//设置填充文字样式,大小20px
ctx.font = "20px Arial"
ctx.fillText("Hello World(Arial)", 10, 110);
}
</script>
</html>

五、设置文字粗体
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//设置填充文字样式,粗体 normal
ctx.font = "normal 20px Arial";
//设置文字及其位置
ctx.fillText("Hello World(normal)", 10, 50);
//设置填充文字样式,粗体 bold
ctx.font = "bold 20px Arial"
ctx.fillText("Hello World(bold)", 10, 90);
//设置填充文字样式,粗体 bolder
ctx.font = "bolder 20px Arial"
ctx.fillText("Hello World(bolder)", 10, 110);
//设置填充文字样式,粗体 lighter
ctx.font = "lighter 20px Arial"
ctx.fillText("Hello World(lighter)", 10, 130);
//设置填充文字样式,粗体 100
ctx.font = "100 20px Arial"
ctx.fillText("Hello World(100)", 10, 150);
//设置填充文字样式,粗体 600
ctx.font = "600 20px Arial"
ctx.fillText("Hello World(600)", 10, 170);
//设置填充文字样式,粗体 900
ctx.font = "900 20px Arial"
ctx.fillText("Hello World(900)", 10, 190);
}
</script>
</html>

六、斜体设置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//设置填充文字样式,font-weight normal
ctx.font = "normal 20px Arial";
//设置文字及其位置
ctx.fillText("Hello World(normal)", 10, 50);
//设置填充文字样式,font-style italic
ctx.font = "italic 20px Arial"
ctx.fillText("Hello World(italic)", 10, 90);
//设置填充文字样式,font-style oblique
ctx.font = "oblique 20px Arial"
ctx.fillText("Hello World(oblique)", 10, 130);
}
</script>
</html>

七、文字对齐
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="500" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
//绘制一条基本线
ctx.strokeStyle = 'red';
ctx.moveTo(150, 20);
ctx.lineTo(150, 170);
ctx.stroke();
//设置填充文字样式
ctx.font = "20px Arial";
//设置对方式
ctx.textAlign = 'start'
//设置文字及其位置
ctx.fillText("textAlign='start'", 150, 60);
//设置对方式
ctx.textAlign = 'end'
//设置文字及其位置
ctx.fillText("textAlign='end'", 150, 80);
//设置对方式
ctx.textAlign = 'left'
//设置文字及其位置
ctx.fillText("textAlign='left'", 150, 100);
//设置对方式
ctx.textAlign = 'center'
//设置文字及其位置
ctx.fillText("textAlign='center'", 150, 120);
//设置对方式
ctx.textAlign = 'right'
//设置文字及其位置
ctx.fillText("textAlign='right'", 150, 140);
}
</script>
</html>

八、文字基线
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body onload="draw('myCanvas')">
<canvas id="myCanvas" width="200" height="500"></canvas>
</body>
<script>
function draw(id) {
var c = document.getElementById(id);
//获取canvs上下文
var ctx = c.getContext("2d");
var param = [{
//普通字母线
baseLne: "alphabetic",
y: 50
}, {
//方框底线
baseLne: "bottom",
y: 100
}, {
//悬挂基线
baseLne: "hanging",
y: 150
}, {
//表意基线
baseLne: "ideographic",
y: 200
}, {
//方框中线
baseLne: "middle",
y: 250
}, {
//方框顶端
baseLne: "top",
y: 300
}]
for (var i = 0; i < param.length; i++) {
//设置文字基线
ctx.textBaseline = param[i].baseLne;
ctx.font = '30px Arial';
ctx.fillText("Hello,world", 50, param[i].y);
ctx.moveTo(0, param[i].y);
ctx.lineTo(250, param[i].y);
ctx.stroke();
}
}
</script>
</html>

边栏推荐
猜你喜欢

基于 VIVADO 的 AM 调制解调(3)仿真验证

中国电子学会五级考点详解(一)-string类型字符串

基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示

单元测试系统化讲解之PowerMock

golang string manipulation

Audio and video + AI, Zhongguancun Kejin helps a bank explore a new development path | Case study

框架外的PHP读取.env文件(php5.6、7.3可用版)

IDEA的初步使用

STM32之串口传输结构体

SDUT 2877:angry_birds_again_and_again
随机推荐
通过Xshell连接Vagrant创建的虚拟机
js将table生成excel文件并去除表格中的多余tr(js去除表格中空的tr标签)
如何通过 IDEA 数据库管理工具连接 TDengine?
Jupyter Notebook 插件 contrib nbextension 安装使用
Filesystem Hierarchy Standard
对比学习系列(三)-----SimCLR
《剑指offer》题解——week3(持续更新)
新一代开源免费的轻量级 SSH 终端,非常炫酷好用!
IPQ4019/IPQ4029 support WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
shell之sed
SDUT 2877: angry_birds_again_and_again
老干妈创始人陶华碧现身直播间,70岁“国民女神”拥抱直播电商
IDEA的初步使用
DataGrip配置OceanBase
在软件工程领域,搞科研的这十年!
工业检测深度学习方法综述
观察表情和面部,会发现他有焦虑和失眠的痕迹
Kotlin算法入门计算质因数
自定义卷积核的分组转置卷积如何实现?
redis模拟面试