当前位置:网站首页>Chapter 2 line graph of canvas
Chapter 2 line graph of canvas
2022-06-24 07:49:00 【yxqq378287007】
《canvas》 The first 2 Chapter Line graph
The first 2 Chapter Line graph
2.1 Introduction to line graph
A straight line 、 rectangular 、 polygon .
2.2 A straight line
2.2.1 canvas Coordinate system
canvas Use W3C Coordinate system .
Top left corner of origin ,x Axis positive direction right ,y Axis positive direction down .
2.2.2 Drawing straight lines
moveTo() and lineTo() Use it together to draw a straight line .
- A straight line
ctx.moveTo(x1, y1); // Move to start
ctx.lineTo(x2, y2); // Connect to end point
ctx.stroke(); // Began to draw
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.moveTo(50, 100); cxt.lineTo(150, 50); cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- Multiple lines
- Three straight lines
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.moveTo(50, 50); cxt.lineTo(100, 50); cxt.lineTo(50, 100);// Last point (100, 50) For a new starting point . //cxt.moveTo(50, 100); cxt.lineTo(100, 100); cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- triangle
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.moveTo(50, 100);// Head tail coincidence cxt.lineTo(150, 50); cxt.lineTo(150, 100); cxt.lineTo(50, 100);// Head tail coincidence cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- rectangular
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.moveTo(50, 100);// Head tail coincidence cxt.lineTo(50, 50); cxt.lineTo(150, 50); cxt.lineTo(150, 100); cxt.lineTo(50, 100);// Head tail coincidence cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.3 rectangular
Rectangles are divided into stroke rectangles and fill rectangles .
2.3.1 Stroke rectangle
strokeStyle Properties and strokeRect() Method to draw a stroke rectangle .
ctx.strokeStyle = Property value ;
cts.strokeRect(x, y, width, height);
- strokeStyle attribute
3 Species value : Color value 、 Gradients and patterns .
ctx.strokeStyle = "#FF0000";
ctx.strokeStyle = "#F00";
ctx.strokeStyle = "red";
ctx.strokeStyle = "rgb(255, 0, 0)";
ctx.strokeStyle = "rgba(255, 0, 0, 0.8)";
- strokeRect() Method
Set it up first strokeStyle attribute , After use strokeRect() Method .
- Stroke rectangle
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.strokeStyle = "red"; cxt.strokeRect(50, 50, 80, 80); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.3.2 Filled rectangle
fillStyle Properties and fillRect() Method to draw a filled rectangle .
ctx.fillStyle = Property value ;
cts.fillRect(x, y, width, height);
- fillStyle attribute
3 Species value : Color value 、 Gradients and patterns .
ctx.fillStyle= "#FF0000";
ctx.fillStyle= "#F00";
ctx.fillStyle= "red";
ctx.fillStyle= "rgb(255, 0, 0)";
ctx.fillStyle= "rgba(255, 0, 0, 0.8)";
- fillRect() Method
Set it up first fillStyle attribute , After use fillRect() Method .
- Filled rectangle
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.fillStyle = "HotPink"; cxt.fillRect(50, 50, 80, 80); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- Draw both a stroke rectangle and a fill rectangle
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.strokeStyle = "red"; cxt.strokeRect(50, 50, 80, 80); cxt.fillStyle = "#FFE8E8"; cxt.fillRect(50, 50, 80, 80); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- Two overlapping filled rectangles
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.fillStyle = "HotPink"; cxt.fillRect(50, 50, 80, 80); cxt.fillStyle = "rgba(0, 0, 255, 0.3)"; cxt.fillRect(30, 30, 80, 80); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.3.3 rect() Method
rect(x, y, width, height);
strokeRect() and fillRect() After calling , Draw rectangle now .
rect() After calling , Call again stroke() or fill() To draw a rectangle .
ctx.rect(50, 50, 80, 80);
ctx.strokeStyle="red";
ctx.stroke();
// Equivalent to
ctx.strokeStyle="red";
ctx.strokeRect(50, 50, 80, 80);
ctx.rect(50, 50, 80, 80);
ctx.fillStyle="red";
ctx.fill();
// Equivalent to
ctx.fillStyle="red";
ctx.fillRect(50, 50, 80, 80);
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); // Draw a stroked rectangle cxt.rect(50, 50, 80, 80); cxt.strokeStyle = "red"; cxt.stroke(); // Draw the fill rectangle cxt.rect(50, 50, 80, 80); cxt.fillStyle = "#FFE8E8"; cxt.fill(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.3.4 Empty rectangle
cxt.clearRect(x, y, width, height);
- Clear the specified area
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.fillStyle = "HotPink"; cxt.fillRect(50, 50, 80, 80); cxt.clearRect(60, 60, 50, 50); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- Empty canvas
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.fillStyle = "HotPink"; cxt.fillRect(50, 50, 80, 80); var btn = $$("btn"); btn.onclick = function () {
cxt.clearRect(0, 0, cnv.width, cnv.height); } } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas><br />
<input id="btn" type="button" value=" Empty canvas" />
</body>
</html>
2.4 polygon
Calculate the coordinates of each vertex , And then use moveTo() and lineTo() Draw it out .
2.4.1 arrow
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.moveTo(40, 60); cxt.lineTo(100, 60); cxt.lineTo(100, 30); cxt.lineTo(150, 75); cxt.lineTo(100, 120); cxt.lineTo(100, 90); cxt.lineTo(40, 90); cxt.lineTo(40, 60); cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.4.2 Regular polygon
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); var n = 15; var centerX = 100; var centerY = 75; var r = 50; createPolygon(cxt, n, centerX, centerY, r);// Call custom methods createPolygon() cxt.fillStyle = "HotPink"; cxt.fill(); } /* * n: Express n Edge shape * centerX、centerY: Express n The coordinates of the center of the edge * r: Express n The size of the edge */ function createPolygon(cxt, n, centerX, centerY, r) {
cxt.beginPath(); for (var i = 0; i < n; i++) {
var x = Math.cos(2*Math.PI*i/n); var y = Math.sin(2*Math.PI*i/n); cxt.lineTo(centerX+r*x, centerY+r*y); } cxt.closePath(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.4.3 Polygonal star
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); var n = 6; var centerX = cnv.width/2; var centerY = cnv.height/2; var rMax = 50; var rMin = 25; createPolygonalStar(cxt, n, centerX, centerY, rMax, rMin) cxt.closePath(); cxt.stroke(); /* * n: Express n Horns * centerX、centerY: Express n Angular star center coordinates * rMax: Express n The radius of the outer circle of the angle star * rMin: Express n Radius of the inner circle of the angle star */ function createPolygonalStar(cxt, n, centerX, centerY, rMax, rMin) {
cxt.beginPath(); for (var i = 0; i < n; i++) {
var xMax = rMax*Math.cos(2*Math.PI*i/n); var yMax = rMax*Math.sin(2*Math.PI*i/n); cxt.lineTo(centerX+xMax, centerY+yMax); var xMin = rMin*Math.cos(2*Math.PI*(i+0.5)/n); var yMin = rMin*Math.sin(2*Math.PI*(i+0.5)/n); cxt.lineTo(centerX+xMin, centerY+yMin); } cxt.closePath(); } } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
2.5 Paint palette
- Checkered palette
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); var n = 8; var step = 30; for(var y = 0; y < n; y++) {
for(var x = 0; x < n; x++) {
cxt.fillStyle = "rgb(" + Math.floor(255-255*y/n) + ", " + Math.floor(255-255*x/n) + ", 0)"; cxt.fillRect(x*step, y*step, step, step); } } } </script>
</head>
<body>
<canvas id="canvas" width="300" height="300" style="border:1px dashed gray;"></canvas>
</body>
</html>
- Gradient palette
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); var r = 255, g = 0, b = 0; var step = 6; for(var i = 0; i < 150; i++) {
if(i < 25) {
g += 10; } else if(i > 25 && i < 50) {
r -= 10; } else if (i > 50 && i < 75) {
g -= 10; b += 10; } else if(i >= 75 && i < 100) {
r += 10; } else {
b -= 10; } cxt.fillStyle = "rgb(" + r + ", " + g + ", " + b + ")"; cxt.fillRect(step*i, 0, step, cnv.height-10); } } </script>
</head>
<body>
<canvas id="canvas" width="1000" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
边栏推荐
- Session & cookie details
- Take my brother to make a real-time Leaderboard
- 鸿蒙开发四
- LeetCode 207:课程表(拓扑排序判断是否成环)
- exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
- Global and Chinese market of Earl Grey tea 2022-2028: Research Report on technology, participants, trends, market size and share
- pair类备注
- 报错“Computation failed in `stat_summary_hex()`”
- L1-019 谁先倒 (15 分)
- First acquaintance with JUC - day01
猜你喜欢

The two most frequently asked locks in the interview

GPU is not used when the code is running

免费ICP域名备案查接口

RDD的执行原理

阿里云全链路数据治理

OpenGauss数据库在 CentOS 上的实践,配置篇

Alibaba cloud full link data governance

ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决

热赛道上的冷思考:乘数效应才是东数西算的根本要求

Win10 build webservice
随机推荐
Detailed explanation of C language compilation, link and operation
语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
Any remarks
Detailed explanation of PHP data serialization test example
Hongmeng development IV
Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Lend you a pair of insight, Frida native trace
Super fast reading in OI
Experience of Shenzhou computer
Unity Culling 相关技术
Global and Chinese market of Earl Grey tea 2022-2028: Research Report on technology, participants, trends, market size and share
语料库数据处理个案实例(句子检索相关个案)
atguigu----16-自定义指令
any类备注
GPU is not used when the code is running
IndexError: Target 7 is out of bounds.
timer使用备注
atguigu----15-内置指令
Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
Event related | reveal how Ti-One's support ability for large-scale events is developed