当前位置:网站首页>Canvas -- drawing of rectangle -- making of histogram
Canvas -- drawing of rectangle -- making of histogram
2022-07-26 03:10:00 【ca11meback】
(1)rect(): Add an arc to the current path ;
grammar :context.rect(x,y,width,height);
Four parameters : Starting point coordinates x,y: Top left coordinates ; Width width: The width of the rectangle ; Height height: The height of the rectangle ;
(2)strokeRect() Method can draw a rectangle directly ;
grammar :context.strokeRect(x,y,width,height);
The column chart is a relatively simple figure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
#box {
border: 1px black solid;
}
</style>
<body>
<canvas id="box" height="700" width="700"> </canvas>
<script>
/** @type {HTMLCanvasElement} */
var canvas = document.querySelector("#box")
var ctx = canvas.getContext("2d")
var arr=[354,644,231,452,279]
ctx.lineTo(100,600)
ctx.lineTo(600,600)
ctx.stroke()
let lim=500/Math.max(...arr)
console.log(lim)
for(i=0;i<arr.length;i++){
ctx.beginPath()
console.log(arr[i])
ctx.fillStyle="gold"
ctx.fillRect(100+i*70,600-lim*arr[i],50,lim*arr[i])
}
</script>
</body>
</html>Design thinking : If the data we need is in the array , The first thing we need to do is draw a horizontal axis , To make our bar graphs display on the horizontal axis , Then set a maximum value , To ensure that the height of the array in the array cannot exceed this maximum , Even if it exceeds, there can only be so many , Then you can enter for Cycle through each of our columns ,fillrect() Use color to fill this column , The four parameters in it and strokerect() The same difference is ,x,y, Cylindrical length , The width of the column , there x The starting value of is the starting position of our horizontal axis , And then add i* spacing ,( It should be noted that this spacing must be greater than the length of the column , Otherwise, the columns will overlap ),y The value of is the horizontal axis y Subtract the array from the value of [i] Value * Height Limited , Because of this y The value of is guaranteed to be on the horizontal axis , Then write the length of the column , The height of the column is equal to the array [i] Value * Height Limited
边栏推荐
- 【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
- FPGA_Vivado软件初次使用流程_超详细
- 班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
- [noip2001 popularization group] the problem of maximum common divisor and minimum common multiple
- FPGA_ Initial use process of vivado software_ Ultra detailed
- Influence of middle tap change on ZVS oscillation circuit
- Safety margin of mass consumption
- Cloud native guide what is cloud native infrastructure
- Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
- The difference between the world wide web, the Internet and the Internet
猜你喜欢

FPGA_ Initial use process of vivado software_ Ultra detailed

实现一个方法,找出数组中的第k大和第m大的数字相加之和

canvas——绘制文本——饼图的修改
![[NOIP2001 普及组]装箱问题](/img/b7/1310b3e68d0ee016465fc069315af6.png)
[NOIP2001 普及组]装箱问题

STM——EXTI外部中断学习笔记

ES6 set and map

Opening method of win11 microphone permission

YOLOv3: An Incremental Improvement

小测(一)

Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
随机推荐
canvas——绘制文本——饼图的修改
After clicking play, the variables in editorwindow will be destroyed inexplicably
Neo4j import CSV data error: neo4j load CSV error: couldn't load the external resource
中国信通院陈屹力:降本增效是企业云原生应用的最大价值
Arthas download and startup
STM - exti external interrupt learning notes
FPGA_ Initial use process of vivado software_ Ultra detailed
Longest Substring Without Repeating Characters
How to reinstall win7 system?
ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
Golang log programming system
Win11隐藏输入法状态栏方法
一切的源头,代码分支策略的选择
MySQL tutorial: MySQL database learning classic (from getting started to mastering)
How about GF Securities? Is it safe to open an account online?
Standardize your own debug process
GoLang日志编程系统
What's good for starting a business with 10000 yuan? Is we media OK?
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
canvas——绘制曲线——挂钟,饼图,五角星