uniapp 小程序 五星评分精确到0.1

uniapp 小程序 五星评分精确到0.1

  1. 上图

  2. 上组件

javascript 复制代码
<template>
  <view class="container">
    <view class="canvas-wrap">
      <canvas
        type="2d"
        id="canvas"
        class="canvas"
        disable-scroll="true"
      ></canvas>
    </view>
  </view>
</template>
<script>
	
	// star style
	const ratingColor = '#CBB195';
	const starColor = '#fff';
	const borderColor = '#CBB195';
	const borderWidth = uni.upx2px(2);
	const height = uni.upx2px(24);
	const width = uni.upx2px(144);
	
	// star config
	const disabled = false;
	const starNum = 5;
	const step = 0.1;
	const toFixed = 1;
	const starGap = uni.upx2px(6);
	const oneStarPercent = 20;
	const oneStepPercent = 2;
	const starWidth = Math.floor((width - starNum * starGap) / starNum);
	const degree = 72
	
export default {
	props:{
		score: {
			type: Number,
			default (){
				return 0
			}
		},
		// 总分为5时  type传1 
		// 总分为100时 type传2
		// 也可自定义总分
		type: {
			type: Number,
			default (){
				return 1
			}
		},
		ratingColor: {
			type: String,
			default (){
				return '#CBB195'
			}
		},
		starColor: {
			type: String,
			default (){
				return '#fff'
			}
		},
		borderColor: {
			type: String,
			default (){
				return '#CBB195'
			}
		},
	},
	data() {
		this.canvasDraw = null // 绘图对象
		this.canvasEle = null // canvas元素对象
		this.canvasNode = null
		this.percent = 0
		return {
			isDrawing: false, // 是否正在绘图
		}
	},
	created() {},
	beforeDestroy() {
		/** 销毁对象 */
		if (this.canvasDraw) {
			this.canvasDraw = null // 绘图对象
			this.canvasEle = null // canvas元素对象
			this.canvasNode = null 
		}
	},
	mounted() {
		/** 初始化 */
		let that = this
		setTimeout(()=>{
			that.$nextTick(()=>{
				that.initCanvas()
			})
		},300)
	},
	methods: {
		/** 初始化canvas */
		initCanvas() {
			const query = wx.createSelectorQuery().in(this)
			query
				.select('#canvas')
				.fields({ node: true, size: true, rect: true })
				.exec((res) => {
					const ele = res[0]
					const pixelRatio = wx.getSystemInfoSync().pixelRatio
					this.canvasEle = ele
					this.canvasNode = ele.node // wx的canvas节点
					this.canvasNode.width = ele.width * pixelRatio // 设置canvas节点宽度
					this.canvasNode.height = ele.height * pixelRatio // 设置canvas节点高度
					this.canvasDraw = this.canvasNode.getContext('2d')
					this.canvasDraw.scale(pixelRatio, pixelRatio)
					if(this.score){
						console.log(this.score)
						this.drawRate(this.score)
					}
			})
		},
		drawRate(value) {
		// 此处可根据自身的总分 计算五星比例  
			if(this.type == 2){
				value = (value/20).toFixed(1)
			}
			this.percent = value * oneStarPercent;
			for (let i = 0; i < starNum; i++) {
				this.drawStar({
					x: starWidth / 2 + i * (starWidth + starGap) + starGap / 2, 
					y: height / 2, 
					r: starWidth / 4, 
					R: starWidth / 2, 
					rot: 0, 
					index: i
				})
			}
		},
		drawStar({x, y, r, R, rot, index}){
			const gradient = this.canvasDraw.createLinearGradient(x - R, 0 , x + R, 0)
			const stop = Math.min(Math.max((index + 1) * oneStarPercent - this.percent, 0), oneStarPercent)
			const rate = (oneStarPercent - stop) / oneStarPercent
			gradient.addColorStop(rate, this.ratingColor)
			gradient.addColorStop(Math.min(1, rate + 0.01), this.starColor)
		
			this.canvasDraw.beginPath();
			for (let i = 0; i < 360 / degree; i ++) {
				this.canvasDraw.lineTo( Math.cos( (18 + i*degree - rot)/180 * Math.PI) * R + x,
		               -Math.sin( (18 + i*degree - rot)/180 * Math.PI) * R + y)
				this.canvasDraw.lineTo( Math.cos( (54 + i*degree - rot)/180 * Math.PI) * r + x,
		               -Math.sin( (54 + i*degree - rot)/180 * Math.PI) * r + y)
			}
			this.canvasDraw.closePath();
			this.canvasDraw.lineWidth = borderWidth;
			this.canvasDraw.fillStyle = gradient;
			this.canvasDraw.strokeStyle = this.borderColor;
			this.canvasDraw.lineJoin = "round";
			this.canvasDraw.fill();
			this.canvasDraw.stroke();
		}
  },
}
</script>
<style>
.container {
  width: 144rpx;
  height: 24rpx;
}
.canvas-wrap {
  display: flex;
  height: 100%;
  /* background: #FFFFFF; */
}
.canvas {
  flex: 1;
  width: 100%;
  height: 100%;
  /* background: #FFFFFF; */
}
</style>
  1. 使用
javascript 复制代码
<template>
<view class="star-box">
	<starRate v-if="score" :score="score" type="2"></starRate>
</view>
</template>
<script>
import starRate from './starRate.vue'
export default {
	components:{ starRate },
	data(){
		 return {
		 	score:100
		 }
	}
};
</script>
  1. 搞定!

注意 由于原生canvas的特性 开发工具显示有问题,真机不影响

相关推荐
FungLeo1 小时前
成为全栈·Vue 管理后台篇·Biome、类型检查、构建和API 代理如何组成质量门禁
前端·javascript·vue.js·typescript·vite·前端工程化·biome
程序员大阳2 小时前
副队长ES6+教程(10)--迭代器 Iterator 与 for...of
javascript·es6·遍历·for of
夕水2 小时前
受不了满屏广告的诗词网站,以及很多打不开的诗词网站,我干脆自己租服务器用trae重构了一个(纯公益、无广告)
前端·javascript
程序员大阳2 小时前
副队长ES6+教程(15)--async和await
javascript·es6·async·promise·同步·异步·await
kyriewen17 小时前
编辑弹窗点了取消,列表里的数据怎么还是变了?
前端·javascript·人工智能
code2cat19 小时前
【随笔】MCP长任务:查询已经返回,为什么任务还没有完成
javascript·人工智能·后端·mcp
FRing0319 小时前
vue2干货总结11(短信验证、登录功能、统一处理错误(响应拦截器)、将token信息存入vuex、vuex持久化处理)
开发语言·前端·javascript·vue.js·vue2
泯泷19 小时前
改一个字,整段都红了?——中文内容差异对比的算法与协议设计
前端·javascript·typescript
afumu20 小时前
免费 API 满地都是,为什么还有人做「让网页 AI 干活」这种东西
javascript·chrome·浏览器
云起SAAS20 小时前
本地生活同城多商户商城系统源码源码 合伙人分红加区域代理加排队返现
前端·javascript·后端