当前位置:网站首页>uniapp使用canvas写环形进度条
uniapp使用canvas写环形进度条
2022-07-23 21:28:00 【swag_特约男演员】
遇到的坑
- 使用普通canvas需要延迟设置
draw()stroke()才生效。 - 使用
canvas2d在微信小程序的开发者工具上表现为固定在屏幕上,不随容器滚动,但是手机表现正常。 - 记住: 在组件中获取实例需要使用
in(this)。如const ctx = uni.createCanvasContext('myCanvas',this),const query = uni.createSelectorQuery().in(this)
组件代码
<template>
<div class="com-container">
<canvas canvas-id="canvas" id="canvas" class="my-canvas" type="2d"></canvas>
</div>
</template>
<script> export default {
props: {
progress: {
type: Number, default: 50 }, innerText: {
type: String, default: '850' } }, data() {
return {
ctx: null // 画布实例 } }, mounted() {
this.getCanvas() }, methods: {
/** * @method 获取画布 */ getCanvas() {
const query = uni.createSelectorQuery().in(this) query.select('#canvas') .fields({
node: true, size: true }) .exec((res) => {
const canvas = res[0].node canvas.width = 120 canvas.height = 120 const ctx = canvas.getContext('2d') this.ctx = ctx }) setTimeout(() => {
this.drawTrack() this.drawProgress(this.progress) this.drawText(this.innerText) }, 500); }, // 绘制轨道 drawTrack() {
this.ctx.save() this.ctx.beginPath() this.ctx.lineCap = "round" this.ctx.lineWidth = 8 this.ctx.strokeStyle = "#dde1ed" // x坐标,y坐标,半径,起始角,结束角,顺时针/逆时针 this.ctx.arc(60, 60, 50, 0, 2 * Math.PI) this.ctx.stroke() this.ctx.closePath() this.ctx.restore() }, // 绘制进度 drawProgress(num) {
this.ctx.save() this.ctx.beginPath() this.ctx.lineCap = "round" this.ctx.lineWidth = 8 this.ctx.strokeStyle = "#5f94f0" // context.arc(x,y,r,sAngle,eAngle,counterclockwise); //x坐标,y坐标,半径,起始角,结束角,顺时针/逆时针 this.ctx.arc( 60, 60, 50, -Math.PI / 2, -Math.PI / 2 + ((2 * num) / 100) * Math.PI, true ) this.ctx.stroke() this.ctx.closePath() this.ctx.restore() }, // 绘制文字 drawText(num) {
this.ctx.save() this.ctx.fillStyle = "#6094F3" this.ctx.font = "33px Helvetica" this.ctx.textAlign = "center" this.ctx.fillText(num, 60, 70) this.ctx.restore() } } } </script>
<style lang='scss' scoped> .com-container {
width: 240rpx; height: 240rpx; .my-canvas {
width: 100%; height: 100%; z-index: 999; } } </style>
边栏推荐
- Minimum spanning tree: prim
- 1063 Set Similarity
- Scala programming (intermediate advanced experimental application)
- Proof of green Tao theorem (1): preparation, notation and Gowers norm
- The total ranking of blogs is 918
- User manual of boost filesystem
- Comment présenter votre expérience de projet lors d'une entrevue
- 1309_ Add GPIO flip on STM32F103 and schedule test with FreeRTOS
- SQLite数据库
- Openlayers instance accessible map accessible map
猜你喜欢
随机推荐
05_ UE4 advanced_ Material UV scaling
Chapter1 data cleaning
Hezhou esp32c3 hardware configuration information serial port printout
Leetcode hot topic hot52-100
Payment products and their usage scenarios
scala编程(中级进阶实验应用)
OpenCV图像处理——拉普拉斯金字塔
1062 Talent and Virtue
Day109.尚医通:集成Nacos、医院列表、下拉列表查询、医院上线功能、医院详情查询
Comment présenter votre expérience de projet lors d'une entrevue
分布式能源的不确定性——风速测试(Matlab代码实现)
High numbers | calculation of double integral 3 | high numbers | handwritten notes
How to introduce your project experience in the interview
Connect with Hunan Ca and use U_ Key login
Cmake learning
HANA SQL 的Union和Union All
First acquaintance with JS (programming suitable for beginners)
VLAN comprehensive experiment
Failed to introspect class feignclientfactorybean exception troubleshooting
Oom mechanism








