From 0b20913755b33d86b036f200169d31fe859daf64 Mon Sep 17 00:00:00 2001 From: qq_38870145 Date: Sat, 16 Mar 2024 21:49:00 +0800 Subject: [PATCH] Sat Mar 16 21:49:00 CST 2024 inscode --- src/components/visualSort/index.vue | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/visualSort/index.vue b/src/components/visualSort/index.vue index 0fc8772..140e149 100644 --- a/src/components/visualSort/index.vue +++ b/src/components/visualSort/index.vue @@ -61,6 +61,8 @@ function bubbleSort(arr) { // 每个长方形之间的间隔 var space = 20; + const total=arr.reduce((p,c)=>p+c,0) + // 清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 设置字体 @@ -70,7 +72,7 @@ function bubbleSort(arr) { ctx.fillStyle = '#61C5FE'; // x 的 值等于 第i个长方形 * (长方形的宽+每个长方形的间隔) // y 的 值等于 画板的高度 - 第i的元素的值 - ctx.fillRect(i * (width + space), maxWidth - arr[i], width, arr[i]); + ctx.fillRect(i * (width + space), maxWidth - arr[i], width, (arr[i]/total)*800); ctx.fillStyle = '#240be4'; // maxWidth - arr[i]-5,这里多- 5,是为了能让文字,在长方形上方一点显示,看的明显些 ctx.fillText(arr[i], i * (width + space), maxWidth - arr[i] - 5); @@ -131,7 +133,7 @@ const sortBtn = () => { --> - + -- GitLab