JavaScript uses canvas to draw coordinates and lines

JavaScript uses canvas to draw coordinates and lines

This article shares the specific code of using canvas to draw coordinates and lines in JavaScript for your reference. The specific content is as follows

The specific code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Draw multiple points at the specified position</title>
    <style>
        canvas{
            border: 1px dashed gray;
        }
    </style>
</head>
<body>
    <canvas id="cvs" width="500" height="500"></canvas>
</body>
</html>

js code:

<script>
    var cvs = document.getElementById('cvs');
    var ctx = cvs.getContext('2d');
 
    // The distance between the coordinate axis and the lower right and left margins of the canvas var padding = {
        top:20,
        right:20,
        bottom:20,
        left:20
    }
    // Width and height of the arrow in the coordinate axis var arrow = {
        width:12,
        height:20
    }
    // Find the coordinates of the vertex on the coordinate axis var vertexTop = {
        x:padding.left,
        y:padding.top
    }
    // Find the coordinates of the origin of the coordinate axis var origin = {
        x:padding.left,
        y:cvs.height - padding.bottom
    }
    // Find the coordinates of the right vertex of the coordinate axis var vertexRight = {
        x:cvs.width - padding.left,
        y:cvs.height - padding.bottom
    }
 
    //Set line width ctx.lineWidth = 2;
    //Draw two lines of the coordinate axis ctx.beginPath();
    ctx.moveTo(vertexTop.x,vertexTop.y);
    ctx.lineTo(origin.x,origin.y);
    ctx.lineTo(vertexRight.x,vertexRight.y);
    ctx.stroke();
 
    //How to draw an arrow//Draw an arrow on top// ^
    // |
    // |
    ctx.beginPath();
    ctx.moveTo(vertexTop.x,vertexTop.y);
    ctx.lineTo(vertexTop.x - arrow.width/2,vertexTop.y + arrow.height);
    ctx.lineTo(vertexTop.x,vertexTop.y + arrow.height/2);
    ctx.lineTo(vertexTop.x + arrow.width/2,vertexTop.y + arrow.height);
    ctx.fill();
 
    //Draw the arrow on the right // --->
    ctx.beginPath();
    ctx.moveTo(vertexRight.x,vertexRight.y);
    ctx.lineTo(vertexRight.x - arrow.height,vertexRight.y - arrow.width);
    ctx.lineTo(vertexRight.x - arrow.height/2,vertexRight.y);
    ctx.lineTo(vertexRight.x - arrow.height,vertexRight.y + arrow.width);
    ctx.fill();
 
    /*
     * Draw a point at a specified position in the coordinate axis. Coordinate algorithm:
     * The x-axis of a point: the x-coordinate of the origin + the horizontal distance from the point to the origin * The y-axis of a point: the y-coordinate of the origin - the vertical distance from the point to the origin */
    //Define the coordinates of the points var points = [[10,10],[50,50],[90,90],[130,130],[170,170],[200,200]];
    //Draw points in the coordinates and use a loop to iterate through the coordinates in the array //Set the color ctx.fillStyle = "green";
    points.forEach(function(arr){
        ctx.fillRect(origin.x + arr[0],origin.y - arr[1],5,5);
    });
    //Connect the lines based on the points //Prevent redrawing ctx.beginPath();
    ctx.lineWidth = 2;
    ctx.strokeStyle = "yellow";
    points.forEach(function (arr) {
        ctx.lineTo(origin.x + arr[0] + 1.8, origin.y - arr[1] + 1.8);
    });
    //Strokectx.stroke();
</script>

The effect is as follows:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A simple example of integrating Spring with Disruptor
  • Introduction and use of NIO based on Java
  • Java Practice: Using Spring to Develop Barcodes and Verification Codes
  • Summary of the differences between java sleep() and wait()
  • More Features of the JavaScript Console
  • Java Practice: City Polyphone Processing
  • Java practical sensitive word filter
  • Java Practice: Foodie Alliance Ordering System
  • Java Basics: Sorting Performance Comparison of List Elements
  • Java Multithreading Disruptor Introduction

<<:  How to implement Nginx configuration detection service status

>>:  Learn MySQL database in one hour (Zhang Guo)

Recommend

How to Install Oracle Java 14 on Ubuntu Linux

Recently, Oracle announced the public availabilit...

Differences and usage examples of for, for...in, for...of and forEach in JS

for loop Basic syntax format: for(initialize vari...

Detailed explanation of Docker compose orchestration tool

Docker Compose Docker Compose is a tool for defin...

CSS3 to achieve floating cloud animation

Operation effect html <head> <meta chars...

The textarea tag cannot be resized and cannot be dragged with the mouse

The textarea tag size is immutable Copy code The c...

Detailed explanation of two points to note in vue3: setup

Table of contents In vue2 In vue3 Notes on setup ...

The functions and differences between disabled and readonly

1: readonly is to lock this control so that it can...

How to query and update the same table in MySQL database at the same time

In ordinary projects, I often encounter this prob...

Pure CSS3 realizes the effect of div entering and exiting in order

This article mainly introduces the effect of div ...

Descending Index in MySQL 8.0

Preface I believe everyone knows that indexes are...

Install mysql5.7.17 using RPM under Linux

The installation method of MySQL5.7 rpm under Lin...