提交 0b209137 编写于 作者: Q qq_38870145

Sat Mar 16 21:49:00 CST 2024 inscode

上级 8621c50f
...@@ -61,6 +61,8 @@ function bubbleSort(arr) { ...@@ -61,6 +61,8 @@ function bubbleSort(arr) {
// 每个长方形之间的间隔 // 每个长方形之间的间隔
var space = 20; var space = 20;
const total=arr.reduce((p,c)=>p+c,0)
// 清空画布 // 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.clearRect(0, 0, canvas.width, canvas.height);
// 设置字体 // 设置字体
...@@ -70,7 +72,7 @@ function bubbleSort(arr) { ...@@ -70,7 +72,7 @@ function bubbleSort(arr) {
ctx.fillStyle = '#61C5FE'; ctx.fillStyle = '#61C5FE';
// x 的 值等于 第i个长方形 * (长方形的宽+每个长方形的间隔) // x 的 值等于 第i个长方形 * (长方形的宽+每个长方形的间隔)
// y 的 值等于 画板的高度 - 第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'; ctx.fillStyle = '#240be4';
// maxWidth - arr[i]-5,这里多- 5,是为了能让文字,在长方形上方一点显示,看的明显些 // maxWidth - arr[i]-5,这里多- 5,是为了能让文字,在长方形上方一点显示,看的明显些
ctx.fillText(arr[i], i * (width + space), maxWidth - arr[i] - 5); ctx.fillText(arr[i], i * (width + space), maxWidth - arr[i] - 5);
...@@ -131,7 +133,7 @@ const sortBtn = () => { ...@@ -131,7 +133,7 @@ const sortBtn = () => {
</div> </div>
</div> --> </div> -->
<canvas id="myCanvas" width="600" height="200"> </canvas> <canvas id="myCanvas" width="600" height="800"> </canvas>
</div> </div>
</a-card> </a-card>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册