当前位置:网站首页>canvas——绘制文本——饼图的修改
canvas——绘制文本——饼图的修改
2022-07-26 03:06:00 【ca11meback】

ctx.font="30px suiyi"
ctx.textAlign="center"
ctx.textBaseline="bottom"
ctx.font需要的注意的是设置字体的大小时候,后面一定跟字体库,即便是使用默认样式,也需要跟一个参数,如果不跟参数的话,前面设置字体大小的参数也无法生效
ctx.textAlign表示的文本参数的位置
ctx.textBaseline表示文本下划线的位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
#box {
border: 1px black solid;
}
</style>
<body>
<canvas id="box" height="700" width="700"> </canvas>
<script>
/** @type {HTMLCanvasElement} */
var canvas = document.querySelector("#box")
var ctx = canvas.getContext("2d")
var deg = Math.PI / 180
var arr = [{
name: "小面",
money: 10
}, {
name: "火锅",
money: 200
}, {
name: "串串",
money: 100
}, {
name: "冒菜",
money: 80
},{
name: "涮羊肉",
money: 300
},{
name: "牛杂煲",
money: 168
}]
total=0;
for(var i=0;i<arr.length;i++){
total=total+arr[i].money
}
console.log(total)
var start=60
arr.forEach(el => {
ctx.beginPath()
var r=parseInt(Math.random()*255)
var g=parseInt(Math.random()*255)
var b=parseInt(Math.random()*255)
ctx.fillStyle=`rgb(${r},${g},${b})`
var bili=(el.money/total)*360
ctx.font="20px suiyi"
ctx.arc(300,300,200,start*deg,(start+bili)*deg)
ctx.fillText(`${el.name}`,300+250*Math.cos((start+bili/2)*deg),300+250*Math.sin((start+bili/2)*deg))
ctx.lineTo(300,300)
start=start+bili
ctx.fill()
ctx.stroke()
});
</script>
</body>
</html>与前面的饼图唯一的改变就是添加了文本,文本的位置在每个扇形的中间,怎么获取扇形中间的位置呢?用的是比例的角度/2加上起始的角度*大于半径的长度,因为要文本显示在饼图之外。
边栏推荐
- The difference between the world wide web, the Internet and the Internet
- Autojs cloud control source code + display
- 一切的源头,代码分支策略的选择
- Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
- Code dynamically controls textview to move right (not XML)
- Jenkins' study notes are detailed
- FPGA_ Initial use process of vivado software_ Ultra detailed
- cmd cpm 命令汇总
- [steering wheel] tool improvement: common shortcut key set of sublime text 4
- Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
猜你喜欢

STM32——PWM学习笔记

AMD64 (x86_64) architecture ABI document:
![[sql] case expression](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[sql] case expression

Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?

如何正确计算 Kubernetes 容器 CPU 使用率

在混合云中管理数据库:八个关键注意事项

如何用U盘进行装机?

Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection

Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
![[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)](/img/b4/62a4c06743fdedacdffd9b156a760f.png)
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)
随机推荐
持续交付和DevOps是一对好基友
cmd cpm 命令汇总
Arthas view the source code of the loaded class (JAD)
Zhimeng prompts you how to solve the problem of setting the field as linkage type
如何用U盘进行装机?
My friend took 25koffer as soon as he learned automation test. When will my function test end?
Extended Physics-InformedNeural Networks论文详解
JVM内存模型解析
重装Win7系统如何进行?
软件测试岗:阿里三面,幸好做足了准备,已拿offer
STM32——PWM学习笔记
Standardize your own debug process
[NOIP2001 普及组]装箱问题
How about GF Securities? Is it safe to open an account online?
(9) Attribute introspection
Method of manually cloning virtual machine in esxi6.7
一篇文章让你理解 云原生 容器化相关
当点击Play以后,EditorWindow中的变量会被莫名其妙销毁.
Opencv 以指定格式保存图片
微信公众号互助、开白群,小白报团取暖