当前位置:网站首页>Uniapp uses canvas to write a circular progress bar
Uniapp uses canvas to write a circular progress bar
2022-07-23 21:41:00 【swag_ Special actor】
Pit encountered
- Using normal canvas Delay setting required
draw()stroke()To take effect . - Use
canvas2dOn the developer tool of wechat applet, it is fixed on the screen , Do not roll with the container , But the mobile phone behaves normally . - remember : To get an instance in a component, you need to use
in(this). Such asconst ctx = uni.createCanvasContext('myCanvas',this),const query = uni.createSelectorQuery().in(this)
Component code
<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 // Canvas example } }, mounted() {
this.getCanvas() }, methods: {
/** * @method Get canvas */ 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); }, // Drawing tracks drawTrack() {
this.ctx.save() this.ctx.beginPath() this.ctx.lineCap = "round" this.ctx.lineWidth = 8 this.ctx.strokeStyle = "#dde1ed" // x coordinate ,y coordinate , radius , Starting angle , End angle , Clockwise / Anti-clockwise this.ctx.arc(60, 60, 50, 0, 2 * Math.PI) this.ctx.stroke() this.ctx.closePath() this.ctx.restore() }, // Draw progress 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 coordinate ,y coordinate , radius , Starting angle , End angle , Clockwise / Anti-clockwise 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() }, // Draw text 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>
边栏推荐
猜你喜欢

One of QT desktop whiteboard tools (to solve the problem of unsmooth curve -- Bezier curve)

Cluster chat server: how to solve the problem of cross server communication | redis publish subscribe
![[isprint function determines whether characters can be output]](/img/c8/c06dc36dbd0bf1feeba64c7753faf5.png)
[isprint function determines whether characters can be output]

实验设计

Cluster chat server: creation of project directory

Yushu A1 robot dog gesture control

Edge cloud | 1. overview

WinDbg practice -- Introduction

-2021 sorting and sharing of the latest required papers related to comparative learning

集群聊天服务器:工程目录的创建
随机推荐
Cluster chat server: chatservice business layer
h264编码参数
Bisection function details
DBSCAN点云聚类
Compare kernelshap and treeshap based on speed, complexity and other factors
PCL出错:error C2589“(“:“::“右边的非法标记)
Pay more attention to which securities company has the lowest commission? Is it safe to open an account online?
Customer exit variable in query
Quick review of interview (III): probability theory and mathematical statistics
Postgraduate entrance examination | advanced mathematics Chapter4 indefinite integral
Chapter 2 回归
Modular development
开源分布式链路追踪对比
集群聊天服务器为什么引入负载均衡器
Still have 1 requests outstanding when connection from slaveX/X.X.X.X:33202 is closed
Cluster chat server: how to solve the problem of cross server communication | redis publish subscribe
集群聊天服务器:chatService业务层
Openlayers instance animated GIF GIF animation
[attack and defense world web] difficulty four-star 12 point advanced question: flatscience
Payment products and their usage scenarios