A Canvas ruler method, device and storage medium

By calculating and displaying the side lengths of irregular figures in Canvas, the problem of difficulty in measuring side lengths when drawing irregular figures is solved, and higher drawing convenience and accuracy are achieved, especially in the unified visual effect of closed-loop figures. .

CN118365734BActive Publication Date: 2024-11-15GUANGZHOU BAOLUN ELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410588951.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-05-13
Publication Date
2024-11-15
Estimated Expiration
2044-05-13

AI Technical Summary

Technical Problem

Canvas lacks measurement functions when drawing irregular graphics, making it difficult for users to accurately obtain and adjust the length of the edges of the graphics, affecting the convenience and accuracy of drawing.

Method used

By calculating the length values ​​of lines and vertices, the display ruler is displayed on the Canvas canvas, automatically compute and display the length of each edge, and place the ruler in the closed-loop graph to unify the visual effect.

Benefits of technology

Improves the convenience and accuracy of users when drawing irregular graphics, and simplifies the control and adjustment of graphics size and shape by automatically calculating and displaying the length of each edge.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118365734B_ABST
    Figure CN118365734B_ABST
Patent Text Reader

Abstract

The invention discloses a ruler method, device and storage medium of Canvas, belonging to the field of graphic rendering technology. The method comprises: when a user draws a line on a Canvas canvas, calculating the length value of the line; obtaining the ruler coordinates according to the center point coordinates of the line and a preset spacing; displaying the length value of the line as the line ruler at the position of the ruler coordinates; when the user finishes drawing on the Canvas canvas, if a closed-loop figure is drawn, deleting the line ruler on the closed-loop figure; calculating the direction of each vertex; obtaining the offset coordinates of each vertex according to the direction of each vertex and the preset spacing; calculating the midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of the adjacent vertex, and displaying the length value of each vertex and the adjacent vertex on the corresponding midpoint coordinates, so as to realize that when the Canvas draws any irregular figure, the length of each edge can be echoed, and the display position is unified, so as to improve the convenience and accuracy of the user when drawing irregular figures.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of graphics rendering technology, and in particular to a Canvas ruler method, device and storage medium. Background Art

[0002] Canvas is a graphics rendering technology provided by HTML5. Through the JavaScript API, graphics and animations can be drawn in real time on web pages. It provides a bitmap-based drawing area where developers can freely draw various shapes, paths, images, and texts. Canvas is often used to create dynamic graphics effects, such as data visualization, charts, and games.

[0003] However, Canvas only displays graphics and usually cannot meet users' actual drawing needs, especially the lack of the function of measuring graphics. In many drawing needs, when drawing lines and graphics, the length of the edges is often the focus of users because they usually affect each other. Summary of the invention

[0004] The present invention provides a Canvas ruler method, device and storage medium, so that when Canvas draws any irregular graphics, the length of each edge can be echoed and the display position can be unified, thereby improving the convenience and accuracy of users when drawing irregular graphics.

[0005] The present invention provides a Canvas ruler method, comprising: when a user draws a line on a Canvas canvas, calculating the distance between two end points of the line on a 2D plane to obtain the length value of the line;

[0006] Obtaining ruler coordinates according to the center point coordinates of the lines and the preset spacing;

[0007] Displaying the length value of the line as a line ruler at the position of the ruler coordinate;

[0008] When the user finishes drawing on the Canvas, determine whether the Canvas has a closed-loop figure drawn on it. If so, delete the line ruler on the closed-loop figure and obtain the two-dimensional coordinates of all vertices in the closed-loop figure.

[0009] Calculate the distance between each vertex and its adjacent vertex to obtain the length value between each vertex and its adjacent vertex;

[0010] Calculating the orientation of each of the vertices;

[0011] Obtaining the offset coordinates of each vertex according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing;

[0012] The midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of an adjacent vertex are calculated, and the length value between each vertex and its adjacent vertex is displayed on the corresponding midpoint coordinates.

[0013] Furthermore, before the user draws on the Canvas canvas, the Canvas drawing API (beginPath()) is called to start drawing graphics.

[0014] Furthermore, the ruler coordinates are obtained according to the center point coordinates of the lines and the preset spacing, specifically:

[0015] Add the center point coordinates to the preset spacing in the target direction to obtain the ruler coordinates;

[0016] The target direction is a direction perpendicular to the line.

[0017] Furthermore, when the user finishes drawing on the Canvas, specifically:

[0018] In response to the user calling the API (closePath()) operation, it is determined that the user has finished drawing on the Canvas canvas.

[0019] Furthermore, the calculation of the orientation of each vertex is specifically as follows:

[0020] Calculate the direction vector between each vertex and the next vertex according to a preset order;

[0021] Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex;

[0022] The normal vector is normalized to serve as the orientation of each vertex.

[0023] Furthermore, the offset coordinates of each vertex are obtained according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing, specifically:

[0024] The coordinates of each vertex are added with a preset distance in the direction of each vertex to obtain the offset coordinates of each vertex.

[0025] As a preferred solution, the present invention can automatically calculate and display the length of each side when drawing irregular figures in Canvas, and place the length scale of each side directly inside the figure, close to the respective edge positions, when drawing closed-loop figures, thereby unifying the visual effects of the figures and enabling users to more easily control and adjust the size and shape of the figures, thereby improving the convenience and accuracy of users in drawing irregular figures when drawing closed irregular figures.

[0026] Other features and advantages of the present invention will be described in detail in the following detailed description.

[0027] Accordingly, the present invention also provides a Canvas ruler device, comprising: a line ruler module and a graphic ruler module;

[0028] The line ruler module is used to calculate the distance between the two end points of the line on the 2D plane when the user draws a line on the Canvas canvas, and obtain the length value of the line;

[0029] Obtaining ruler coordinates according to the center point coordinates of the lines and the preset spacing;

[0030] Displaying the length value of the line as a line ruler at the position of the ruler coordinate;

[0031] The graphic ruler module is used to determine whether a closed-loop graphic is drawn on the Canvas when the user finishes drawing on the Canvas. If so, delete the line ruler on the closed-loop graphic and obtain the two-dimensional coordinates of all vertices in the closed-loop graphic.

[0032] Calculate the distance between each vertex and its adjacent vertex to obtain the length value between each vertex and its adjacent vertex;

[0033] Calculating the orientation of each of the vertices;

[0034] Obtaining the offset coordinates of each vertex according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing;

[0035] The midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of an adjacent vertex are calculated, and the length value between each vertex and its adjacent vertex is displayed on the corresponding midpoint coordinates.

[0036] Further, the line ruler module includes: a first calculation module;

[0037] The first calculation module is used to add the center point coordinates to the preset spacing in the target direction to obtain the ruler coordinates;

[0038] The target direction is a direction perpendicular to the line.

[0039] Further, the graphic ruler module includes: a second calculation module;

[0040] The second calculation module is used to calculate the direction vector between each vertex and the next vertex according to a preset order;

[0041] Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex;

[0042] Normalizing the normal vector as the orientation of each vertex;

[0043] The coordinates of each vertex are added with a preset distance in the direction of each vertex to obtain the offset coordinates of each vertex.

[0044] Correspondingly, the present invention also provides a computer-readable storage medium, which includes a stored computer program; wherein, when the computer program is running, it controls the device where the computer-readable storage medium is located to execute a Canvas ruler method as described in the content of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] Figure 1 It is a flowchart of an embodiment of the Canvas ruler method provided by the present invention;

[0046] Figure 2 It is a structural schematic diagram of an embodiment of the Canvas ruler device provided by the present invention. DETAILED DESCRIPTION

[0047] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0048] Embodiment 1

[0049] Please refer to Figure 1 , a Canvas ruler method provided by an embodiment of the present invention, comprising steps S101-S103:

[0050] Step S101: when a user draws a line on a Canvas canvas, the distance between the two end points of the line on a 2D plane is calculated to obtain the length of the line;

[0051] In this embodiment, a Canvas is created as a drawing stage, and the spacing value between each edge and the ruler display is set. Before the user draws on the Canvas, the Canvas drawing API (beginPath()) is called to start drawing graphics.

[0052] Step S102: Obtaining ruler coordinates according to the center point coordinates of the lines and the preset spacing;

[0053] Furthermore, the ruler coordinates are obtained according to the center point coordinates of the lines and the preset spacing, specifically:

[0054] Add the center point coordinates to the preset spacing in the target direction to obtain the ruler coordinates;

[0055] The target direction is a direction perpendicular to the line.

[0056] Step S103: displaying the length value of the line as a line ruler at the position of the ruler coordinate;

[0057] Step S104: when the user finishes drawing on the Canvas, it is determined whether a closed-loop figure is drawn on the Canvas. If so, the line ruler on the closed-loop figure is deleted, and the two-dimensional coordinates of all vertices in the closed-loop figure are obtained;

[0058] In this embodiment, if the user draws a closed-loop figure, the entire figure is redrawn, including the scale. The two-dimensional coordinates of all vertices in the closed-loop figure are ordered, generally in a clockwise or counterclockwise direction along the edge of the closed-loop figure.

[0059] Furthermore, when the user finishes drawing on the Canvas, specifically:

[0060] In response to the user calling the API (closePath()) operation, it is determined that the user has finished drawing on the Canvas canvas.

[0061] Step S105: Calculate the distance between each vertex and its adjacent vertex to obtain the length value between each vertex and its adjacent vertex;

[0062] Step S106: Calculate the orientation of each vertex;

[0063] Furthermore, the calculation of the orientation of each vertex is specifically as follows:

[0064] Calculate the direction vector between each vertex and the next vertex according to a preset order;

[0065] Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex;

[0066] The normal vector is normalized to serve as the orientation of each vertex.

[0067] Step S107: obtaining the offset coordinates of each vertex according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing;

[0068] Furthermore, the offset coordinates of each vertex are obtained according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing, specifically:

[0069] The coordinates of each vertex are added with a preset distance in the direction of each vertex to obtain the offset coordinates of each vertex.

[0070] Step S108: Calculate the midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of an adjacent vertex, and display the length value between each vertex and its adjacent vertex on the corresponding midpoint coordinates.

[0071] In this embodiment, the length value of each vertex and its adjacent vertex is displayed on the corresponding midpoint coordinate, ensuring that the scale data always remains in the middle of each edge, facing the side position inside the graphic, thereby unifying the position style and placing each edge of the closed-loop graphic at a uniform position inside the graphic, thereby improving the visualization consistency of the graphic.

[0072] Implementing the embodiments of the present invention has the following effects:

[0073] The present invention can automatically calculate and display the length of each side when drawing an irregular figure in Canvas, and place the length ruler of each side directly inside the figure, close to the respective edge positions, when drawing a closed-loop figure, thereby unifying the visual effect of the figure and enabling the user to more easily control and adjust the size and shape of the figure, thereby improving the convenience and accuracy of the user in drawing the irregular figure when drawing a closed irregular figure.

[0074] Embodiment 2

[0075] Please refer to Figure 2 , a Canvas ruler device provided by an embodiment of the present invention includes: a line ruler module 201 and a graphic ruler module 202;

[0076] The line ruler module is used to calculate the distance between the two end points of the line on the 2D plane when the user draws a line on the Canvas canvas, and obtain the length value of the line;

[0077] Obtaining ruler coordinates according to the center point coordinates of the lines and the preset spacing;

[0078] Displaying the length value of the line as a line ruler at the position of the ruler coordinate;

[0079] The graphic ruler module is used to determine whether a closed-loop graphic is drawn on the Canvas when the user finishes drawing on the Canvas. If so, delete the line ruler on the closed-loop graphic and obtain the two-dimensional coordinates of all vertices in the closed-loop graphic.

[0080] Calculate the distance between each vertex and its adjacent vertex to obtain the length value between each vertex and its adjacent vertex;

[0081] Calculating the orientation of each of the vertices;

[0082] Obtaining the offset coordinates of each vertex according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing;

[0083] The midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of an adjacent vertex are calculated, and the length value between each vertex and its adjacent vertex is displayed on the corresponding midpoint coordinates.

[0084] Further, the line ruler module includes: a first calculation module;

[0085] The first calculation module is used to add the center point coordinates to the preset spacing in the target direction to obtain the ruler coordinates;

[0086] The target direction is a direction perpendicular to the line.

[0087] Further, the graphic ruler module includes: a second calculation module;

[0088] The second calculation module is used to calculate the direction vector between each vertex and the next vertex according to a preset order;

[0089] Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex;

[0090] Normalizing the normal vector as the orientation of each vertex;

[0091] The coordinates of each vertex are added with a preset distance in the direction of each vertex to obtain the offset coordinates of each vertex.

[0092] The above-mentioned Canvas ruler device can implement the Canvas ruler method of the above-mentioned method embodiment. The options in the above-mentioned method embodiment are also applicable to this embodiment and will not be described in detail here. The rest of the contents of the embodiment of this application can refer to the contents of the above-mentioned method embodiment, and will not be repeated in this embodiment.

[0093] Embodiment 3

[0094] Correspondingly, the present invention also provides a computer-readable storage medium, which includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the Canvas ruler method described in any one of the above embodiments.

[0095] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer program in the terminal device.

[0096] The terminal device may be a computing device such as a desktop computer, a notebook, a PDA, a cloud server, etc. The terminal device may include, but is not limited to, a processor and a memory.

[0097] The processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the terminal device, and uses various interfaces and lines to connect various parts of the entire terminal device.

[0098] The memory can be used to store the computer program and / or module, and the processor realizes various functions of the terminal device by running or executing the computer program and / or module stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, at least one application required for a function, etc.; the data storage area can store data created according to the use of the mobile terminal, etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Med ia Card, SMC), a secure digital (SecureDi gita l, SD) card, a flash card (F l ash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.

[0099] Wherein, if the module / unit integrated in the terminal device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium, etc.

[0100] The specific embodiments described above further illustrate the purpose, technical solutions and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the present invention.

Claims

1. A Canvas ruler method, characterized in that: include: When the user draws a line on the Canvas canvas, the distance between the two endpoints of the line on the 2D plane is calculated to obtain the length value of the line; Obtaining ruler coordinates according to the center point coordinates of the lines and the preset spacing; Displaying the length value of the line as a line ruler at the position of the ruler coordinate; When the user finishes drawing on the Canvas, determine whether the Canvas has a closed-loop figure drawn on it. If so, delete the line ruler on the closed-loop figure and obtain the two-dimensional coordinates of all vertices in the closed-loop figure. Calculate the distance between each vertex and its adjacent vertex to obtain the length value between each vertex and its adjacent vertex; Calculate the orientation of each vertex, specifically: Calculate the direction vector between each vertex and the next vertex according to a preset order; Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex; Normalizing the normal vector as the orientation of each vertex; Obtaining the offset coordinates of each vertex according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing; The midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of an adjacent vertex are calculated, and the length value between each vertex and its adjacent vertex is displayed on the corresponding midpoint coordinates.

2. A Canvas ruler method as claimed in claim 1, characterized in that: Also includes: Before the user draws on the Canvas canvas, call the Canvas drawing API (beginPath()) to start drawing graphics.

3. A Canvas ruler method as claimed in claim 1, characterized in that: The ruler coordinates are obtained according to the center point coordinates of the lines and the preset spacing, specifically: Add the center point coordinates to the preset spacing in the target direction to obtain the ruler coordinates; The target direction is a direction perpendicular to the line.

4. A Canvas ruler method as claimed in claim 1, characterized in that: When the user finishes drawing on the Canvas, specifically: In response to the user calling the API (closePath()), it is determined that the user has finished drawing on the Canvas canvas.

5. A Canvas ruler method as claimed in claim 1, characterized in that: The offset coordinates of each vertex are obtained according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing, specifically: The coordinates of each vertex are added with a preset distance in the direction of each vertex to obtain the offset coordinates of each vertex.

6. A Canvas ruler device, characterized in that: include: Line ruler module and graphic ruler module; The line ruler module is used to calculate the distance between the two end points of the line on the 2D plane when the user draws a line on the Canvas canvas, and obtain the length value of the line; Obtaining ruler coordinates according to the center point coordinates of the lines and the preset spacing; Displaying the length value of the line as a line ruler at the position of the ruler coordinate; The graphic ruler module is used to determine whether a closed-loop graphic is drawn on the Canvas when the user finishes drawing on the Canvas. If so, delete the line ruler on the closed-loop graphic and obtain the two-dimensional coordinates of all vertices in the closed-loop graphic. Calculate the distance between each vertex and its adjacent vertex to obtain the length value between each vertex and its adjacent vertex; Calculate the orientation of each vertex, specifically: Calculate the direction vector between each vertex and the next vertex according to a preset order; Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex; Normalizing the normal vector as the orientation of each vertex; Obtaining the offset coordinates of each vertex according to the two-dimensional coordinates of each vertex, the orientation of each vertex and the preset spacing; The midpoint coordinates between the offset coordinates of each vertex and the offset coordinates of an adjacent vertex are calculated, and the length value between each vertex and its adjacent vertex is displayed on the corresponding midpoint coordinates.

7. A Canvas ruler device as claimed in claim 6, characterized in that: The line ruler module includes: a first calculation module; The first calculation module is used to add the center point coordinates to the preset spacing in the target direction to obtain the ruler coordinates; The target direction is a direction perpendicular to the line.

8. A Canvas ruler device as claimed in claim 6, characterized in that: The graphic ruler module includes: a second calculation module; The second calculation module is used to calculate the direction vector between each vertex and the next vertex according to a preset order; Using a cross product method to obtain a normal vector of the direction vector, in the calculation of the cross product method, the x-axis of the direction vector is calculated in a negative direction to obtain a normal vector of the direction vector between the vertex and the next vertex; Normalizing the normal vector as the orientation of each vertex; The coordinates of each vertex are added with a preset distance in the direction of each vertex to obtain the offset coordinates of each vertex.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored computer program; wherein, when the computer program is run, it controls the device where the computer-readable storage medium is located to execute a Canvas ruler method as claimed in any one of claims 1 to 5.