当前位置:网站首页>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加上起始的角度*大于半径的长度,因为要文本显示在饼图之外。
边栏推荐
- JVM内存模型解析
- Influence of middle tap change on ZVS oscillation circuit
- STM——EXTI外部中断学习笔记
- Code dynamically controls textview to move right (not XML)
- Literature speed reading | in the face of danger, anxious people run faster?
- Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?
- Longest Substring Without Repeating Characters
- Usage of fuser and lsof
- FPGA_ Initial use process of vivado software_ Ultra detailed
- 图像识别(七)| 池化层是什么?有什么作用?
猜你喜欢

中国信通院陈屹力:降本增效是企业云原生应用的最大价值

从各大APP年度报告看用户画像——标签,比你更懂你自己

图像识别(七)| 池化层是什么?有什么作用?

LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS
![[translation] cloud like internal load balancer for kubernetes?](/img/e5/f003ebed05a94d2936cfef5617f745.jpg)
[translation] cloud like internal load balancer for kubernetes?
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

FPGA_ Initial use process of vivado software_ Ultra detailed

朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?

这种动态规划你见过吗——状态机动态规划之股票问题(上)

Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
随机推荐
Golang log programming system
在混合云中管理数据库:八个关键注意事项
Execution process behind shell commands
Golang 中‘...‘的用法
Case: using kept+haproxy to build a Web Cluster
Usage of arguments.callee
How to close the case prompt icon of win11? Closing method of win11 case prompt Icon
How to design automated test cases?
JSD-2204-酷鲨商城(管理商品模块)-Day02
Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
JVM内存模型解析
Extended Physics-InformedNeural Networks论文详解
Skill list of image processing experts
ShardingSphere数据分片
朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?
Convert rich text to normal text
[sql] case expression
The sixth day of the third question of daily Luogu
Standardize your own debug process