当前位置:网站首页>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
边栏推荐
- STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
- Detailed explanation of extended physics informedneural networks paper
- 【C进阶】深入探索数据的存储(深度剖析+典例解读)
- Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation
- Autojs cloud control source code + display
- Win11更改磁盘驱动器号的方法
- [untitled]
- (9) Attribute introspection
- Nahamcon CTF 2022 babyrev reverse analysis
- Arthas' dynamic load class (retransform)
猜你喜欢

Execution process behind shell commands

Arthas view the source code of the loaded class (JAD)

【无标题】

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

26 points that must be paid attention to for stability test

Parallelloopbody in opencv

My friend took 25koffer as soon as he learned automation test. When will my function test end?

Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘

Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法

ES6 set and map
随机推荐
An article allows you to understand the relevance of cloud native containerization
复制列表时踩过的坑:浅拷贝与深拷贝
Usage of '...' in golang
JSD-2204-酷鲨商城(管理商品模块)-Day02
Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation
记一次SQL优化
经典面试问题——OOP语言的三大特征
Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
How to correctly calculate the CPU utilization of kubernetes container
MySQL教程:MySQL数据库学习宝典(从入门到精通)
持续交付和DevOps是一对好基友
(9) Attribute introspection
Unity quickly builds urban scenes
ES6 set and map
Be highly vigilant! Weaponization of smartphone location data on the battlefield
FPGA_Vivado软件初次使用流程_超详细
Jenkins' study notes are detailed
Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递