Visual large-screen automatic screenshot method, system and program for network shooting range
Through the cloneNode method and the rendering function of html2canvas, the problem that html2canvas cannot intercept the iframe element is solved, and a complete screenshot of the page containing the iframe element is realized, ensuring the accuracy and completeness of the screenshot.
Patent Information
- Application Number
- CN202410713815.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-04
- Publication Date
- 2024-10-01
AI Technical Summary
When the existing html2canvas plug-in intercepts a page containing an iframe element, it cannot correctly intercept the iframe part, causing the iframe area in the screenshot to appear blank.
Clone the DOM element through the cloneNode method, and use the rendering function of html2canvas to draw the iframe element from top to bottom according to the cascading relationship of the elements to the canvas object. Use the underlying API of canvas to convert the iframe element into byte stream data, and then generate it through background conversion. The online address of the screenshot to ensure that the iframe part is correctly captured.
A complete screenshot of a page containing an iframe element is realized, which avoids the problem that the iframe area in the screenshot is displayed as blank. The user can intuitively see the display content on the large visual screen and quickly select the required display content.
Smart Images

Figure CN118733914A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a method, system and program for automatically taking screenshots of a large visualized screen, and in particular to a method, system and program for automatically taking screenshots of a large visualized screen in a network shooting range. Background Art
[0002] Cyber Range is a technology that simulates and reproduces the network architecture, system equipment, operating status and operating environment of business processes in real cyberspace.
[0003] Visual large screen is a data visualization display method, which presents complex data and information such as charts, graphs and data dashboards on large screens, as well as third-party platforms such as 3D engine programs, GIS maps, etc. to users in an intuitive and vivid way. Visual large screens have a variety of styles and different display contents. In order to facilitate users to quickly select the large screen they need to display, the large screen content needs to be displayed in the form of posters or screenshots.
[0004] At present, it is common to use the third-party plug-in html2canvas to automatically capture the page. html2canvas is a popular JavaScript library that can convert HTML elements into images in the browser. It can automatically capture the entire page or a specified part of the page. However, if the page contains a page with complex elements such as iframes, html2canvas will not capture the part containing the iframe element, and the final image effect will display the part containing the iframe as blank. Summary of the invention
[0005] Purpose of the invention: In view of the problems existing in the above-mentioned prior art, the purpose of the present invention is to provide a method, system and program for automatically taking screenshots of a large-screen visualization of a network shooting range, which can solve the problem that html2canvas cannot capture the part containing iframe elements.
[0006] Technical solution: To achieve the above-mentioned invention object, the present invention adopts the following technical solution:
[0007] The network range visualization large screen automatic screenshot method of the present invention comprises the following steps:
[0008] S1: Based on html2canvas, cloneNode method is used to clone DOM elements; the cloneNode method specifically performs a deep traversal from the root element downwards, uses the createElementClone method to create a Node instance object, and then uses the getComputedStyle method provided by the window object to obtain style data, processes the style data through the encapsulated style counting method, and uses this.resolvePseudoContent to mount the before and after pseudo-elements, and determines whether there are child elements. If so, recursive remounting is performed, and finally the processed Node instance data is returned;
[0009] S2: Rendering Canvas through html2canvas, processing the iframe element, and generating a picture; the processing of the iframe element includes that html2canvas first draws the elements into the canvas object step by step from top to bottom according to the stacking relationship of the elements, and then uses the canvas's underlying API, toDataURL to convert the iframe element into byte stream data, and then converts the byte stream through the background to generate an online address of the screenshot, and finally displays the address of the picture through the src attribute of the img tag.
[0010] Furthermore, the specific method flow of step S1 is as follows:
[0011] S11: Create a Node instance object through the createElementClone method;
[0012] S12: Use the getComputedStyle method to obtain the style and process the style data;
[0013] S13: Use this.resolvePseudoContent to mount the before and after pseudo elements;
[0014] S14: Determine whether there is a child element. If so, return to S12 and repeat the operation;
[0015] S15: Return the processed Node instance data.
[0016] Furthermore, the rendering of Canvas is specifically to render the data processed by the cloning DOM process to the canvas, by generating a stacking context tree, placing the drawing nodes of the elements into multiple levels, determining the stacking order, and the logical processing of the stacking order is processed by renderStackContent, and then calling the underlying cancas API for rendering.
[0017] Furthermore, the specific method flow of step S2 is as follows:
[0018] S21: Implement CSS stacking layout rules through renderStackContent;
[0019] S22: Set the ref reference of the iframe element to canvasRef;
[0020] S26: Get the width and height of the iframe element through canvasRef;
[0021] S27: Use html2canvas to process iframe elements;
[0022] S28: Convert base64 format data into image address.
[0023] Based on the same inventive concept, the present invention also provides a network range visualization large-screen automatic screenshot system, comprising:
[0024] The cloning module is used to clone DOM elements based on html2canvas using the cloneNode method. The cloneNode method specifically performs a deep traversal from the root element downwards, uses the createElementClone method to create a Node instance object, and then uses the getComputedStyle method provided by the window object to obtain style data, processes the style data through the encapsulated style counting method, and uses this.resolvePseudoContent to mount the before and after pseudo-elements to determine whether there are child elements. If so, recursive remounting is performed, and finally the processed Node instance data is returned;
[0025] The rendering module is used to render Canvas through html2canvas, process iframe elements and generate pictures; the processing of iframe elements includes html2canvas first drawing them into the canvas object step by step from top to bottom according to the stacking relationship of the elements, and then using the canvas's underlying API, toDataURL to convert the iframe elements into byte stream data, and then converting the byte stream through the background to generate an online address of the screenshot, and finally displaying the address of the picture through the src attribute of the img tag.
[0026] Furthermore, the cloning module is used to clone DOM elements using the cloneNode method based on html2canvas, specifically including:
[0027] Create a Node instance object through the createElementClone method;
[0028] Use the getComputedStyle method to obtain the style and process the style data;
[0029] Use this.resolvePseudoContent to mount the before and after pseudo elements;
[0030] Determine whether there is a child element. If so, return to use the getComputedStyle method to obtain the style, process the style data, and repeat the operation;
[0031] Returns the processed Node instance data.
[0032] Furthermore, the rendering of Canvas is specifically to render the data processed by the cloning DOM process to the canvas, by generating a stacking context tree, placing the drawing nodes of the elements into multiple levels, determining the stacking order, and the logical processing of the stacking order is processed by renderStackContent, and then calling the underlying cancas API for rendering.
[0033] Furthermore, the rendering module renders Canvas through html2canvas, processes the iframe element, and generates a picture, which specifically includes:
[0034] Implement CSS stacking layout rules through renderStackContent; set the ref reference of the iframe element to canvasRef;
[0035] Get the width and height of the iframe element through canvasRef;
[0036] Use html2canvas to process iframe elements;
[0037] Convert base64 format data to image address.
[0038] Based on the same inventive concept, the present invention also provides a computer system, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is loaded into the processor, the steps of the method for automatically taking screenshots of a large-screen visualization of a network target range described in any one of the above items are implemented.
[0039] Based on the same inventive concept, the present invention also provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of the method for automatically taking screenshots of a large-screen visualization of a network shooting range according to any of the above items.
[0040] Beneficial effect: Compared with the prior art, the present invention processes the iframe element based on html2canvas. html2canvas will gradually draw it into the canvas object from top to bottom according to the stacking relationship of the elements. It uses the ref reference of the iframe element to process the distortion effect of the image capture, and uses the underlying API of canvas, toDataURL to convert the iframe element into byte stream data, and then converts the base64 format to generate a screenshot address through the background. Finally, the address of the image is displayed through the src attribute of the img tag, so that users can intuitively see the display content of the visualization large screen, and quickly select the visualization large screen they need. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 is a flow chart of a method according to an embodiment of the present invention;
[0042] Figure 2 A flowchart of cloning DOM elements according to an embodiment of the present invention;
[0043] Figure 3 The present invention is a flowchart of rendering a Canvas to generate a picture. DETAILED DESCRIPTION
[0044] The technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings and specific embodiments.
[0045] The principle of html2canvas to realize screenshot is actually to iteratively clone and parse DOM objects. DOM objects are JavaScript objects generated by browsers based on HTML tags. Through DOM objects, you can access and manipulate web page content, including HTML elements, text, attributes, etc. The DOM object model defines the objects required to represent and modify documents, the behaviors and properties of these objects, and the relationships between them. DOM is independent of platforms and programming languages and can be used by multiple programming languages such as Java and JavaScript. By modifying the properties of DOM objects, they can be automatically mapped to the corresponding HTML tags.
[0046] As attached Figure 1 As shown, an embodiment of the present invention discloses a method for automatically capturing a large-screen visual image of a network range, comprising the following steps:
[0047] Based on html2canvas, the cloneNode method is used to clone DOM elements. The cloneNode method specifically performs a deep traversal from the root element downwards, uses the createElementClone method to create a Node instance object, then uses the getComputedStyle method provided by the window object to obtain style data, processes the style data through the encapsulated style counting method, and uses this.resolvePseudoContent to mount the before and after pseudo-elements to determine whether there are child elements. If so, recursive remounting is performed, and finally the processed Node instance data is returned;
[0048] Canvas is rendered through html2canvas, and the iframe element is processed to generate a picture; the processing of the iframe element includes that html2canvas first draws the elements into the canvas object step by step from top to bottom according to the stacking relationship of the elements, and then uses the canvas's underlying API, toDataURL to convert the iframe element into byte stream data, and then converts the byte stream through the background to generate an online address of the screenshot, and finally displays the address of the picture through the src attribute of the img tag.
[0049] The html2canvas process of page elements is divided into two processes. The first process is to clone DOM elements, and the second process is to render Canvas. This method will perform special processing on iframe elements. html2canvas will gradually draw them into the canvas object from top to bottom according to the stacking relationship of the elements, and then use the canvas's underlying API, toDataURL, to convert the iframe elements into byte stream data. The byte stream is then converted into the online address of the screenshot through the background, and finally the address of the image is displayed through the src attribute of the img tag.
[0050] The core method of the DOM element cloning process is the cloneNode method, which traverses deeply from the root element downward, uses the this.createElementClone method to create a Node instance object, and then uses the getComputedStyle method provided by the window object to obtain the style data, and processes the style data through the encapsulated CSSParsedCounterDeclaration(this.context,style) style counting method. At the same time, this.resolvePseudoContent is used to mount the before and after pseudo-elements to determine whether there are child elements. If so, this.cloneChildNodes(node,clone,copyStyles) is called for recursive remounting, and finally the processed Node instance data is returned. The processing process of the cloneNode method is as follows:
[0051]
[0052]
[0053]
[0054] like Figure 2 As shown, the process of cloning DOM elements is as follows:
[0055] N1: Create a Node instance object through the createElementClone method;
[0056] N2: Use the getComputedStyle method to obtain the style and process the style data;
[0057] N3: Use this.resolvePseudoContent to mount the before and after pseudo elements;
[0058] C1: Determine whether there is a child element;
[0059] N4: Return the processed Node instance data.
[0060] This process implements the DOM element cloning process of html2canvas.
[0061] The process of rendering Canvas is to render the data processed by the clone DOM process to the canvas, and by generating a stacking context tree, put the element's drawing nodes into 7 levels to determine the stacking order. The logical processing of the stacking order is processed by renderStackContent, and then call the underlying cancasAPI for rendering.
[0062] (1) The renderStackContent function implements the CSS stacking layout rules. The label elements in the page will set the special style of each element through CSS syntax. The renderStackContent method will process the levels according to the rules based on the style of the elements, and display which elements from the bottom to the top.
[0063]
[0064]
[0065]
[0066] (2) Set the alias of the iframe element in the page to canvasRef through the ref method. Ref is a reference method for obtaining DOM elements or components. Get the width and height of the iframe element by using getComputedStyle(canvasBox).width and getComputedStyle(canvasBox).height. During the screenshot process, the page can be enlarged or reduced. In order to avoid image distortion and blur, the page scaling value needs to be processed and calculated and stored in the variable scale. Then, the configured canvasRef reference is passed in through html2canvas for asynchronous screenshot processing. Use canvas.toDataURL to specify the converted image format. The data finally processed by html2canvas is in the form of base64 byte stream and cannot be displayed directly. The base64 byte stream needs to be converted into an image address through the background.
[0067]
[0068]
[0069] like Figure 3 As shown, the process of rendering Canvas is as follows:
[0070] P1: Implement CSS stacking layout rules through renderStackContent;
[0071] P2: Set the ref reference of the iframe element to canvasRef;
[0072] P3: Get the width and height of the iframe element through canvasRef;
[0073] P4: Use html2canvas to process iframe elements;
[0074] P5: Convert base64 format data into image address;
[0075] This process processes the iframe element through html2canvas to generate the image address.
[0076] Based on the same inventive concept, this embodiment also provides a network range visualization large-screen automatic screenshot system, including:
[0077] The cloning module is used to clone DOM elements based on html2canvas using the cloneNode method. The cloneNode method specifically performs a deep traversal from the root element downwards, uses the createElementClone method to create a Node instance object, and then uses the getComputedStyle method provided by the window object to obtain style data, processes the style data through the encapsulated style counting method, and uses this.resolvePseudoContent to mount the before and after pseudo-elements to determine whether there are child elements. If so, recursive remounting is performed, and finally the processed Node instance data is returned;
[0078] The rendering module is used to render Canvas through html2canvas, process iframe elements and generate pictures; the iframe elements are processed, specifically, html2canvas will gradually draw them into the canvas object from top to bottom according to the stacking relationship of the elements, and then use the canvas's underlying API, toDataURL to convert the iframe elements into byte stream data, and then convert the byte stream through the background to generate an online address of the screenshot, and finally display the address of the picture through the src attribute of the img tag.
[0079] Based on the same inventive concept, this embodiment also provides a computer system, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is loaded into the processor, the steps of the method for automatically taking screenshots of a large-screen visualization of a network target range according to any one of the above items are implemented.
[0080] Based on the same inventive concept, this embodiment also provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of the method for automatically taking screenshots of a large-screen visualization of a network shooting range according to any of the above items.
[0081] The present invention uses the cloneNode method to clone elements. It will perform a deep traversal from the root element downwards, determine whether each element has child elements, use cloneChildNodes for recursive processing, use getComputedStyle to collect element style data, and use resolvePseudoContent to mount before and after pseudo-elements, and finally obtain the processed Node instance data.
[0082] The present invention uses the renderStackContent function to implement the CSS stacking layout rule, and determines which elements are displayed from the bottom layer to the top layer.
[0083] The present invention sets the alias of the iframe element in the page as canvasRef through the ref method, obtains the width and height of the iframe element through getComputedStyle(canvasBox).width and getComputedStyle(canvasBox).height, avoids distortion in the screenshot process by recalculating the element width and height according to the scales value, and converts the base64 byte stream into a picture address through the background.
Claims
1. A method for automatically taking screenshots of a large-screen visualization of a network range. It is characterized in that The following steps are involved: S1: Based on html2canvas, cloneNode method is used to clone DOM elements; the cloneNode method specifically performs a deep traversal from the root element downwards, uses the createElementClone method to create a Node instance object, and then uses the getComputedStyle method provided by the window object to obtain style data, processes the style data through the encapsulated style counting method, and uses this.resolvePseudoContent to mount the before and after pseudo-elements, and determines whether there are child elements. If so, recursive remounting is performed, and finally the processed Node instance data is returned; S2: Rendering Canvas through html2canvas, processing the iframe element, and generating a picture; the processing of the iframe element includes that html2canvas first draws the elements into the canvas object step by step from top to bottom according to the stacking relationship of the elements, and then uses the canvas's underlying API, toDataURL to convert the iframe element into byte stream data, and then converts the byte stream through the background to generate an online address of the screenshot, and finally displays the address of the picture through the src attribute of the img tag.
2. The method for automatically taking screenshots of a large-screen visualization of a network range according to claim 1, It is characterized in that The specific method flow of step S1 is as follows: S11: Create a Node instance object through the createElementClone method; S12: Use the getComputedStyle method to obtain the style and process the style data; S13: Use this.resolvePseudoContent to mount the before and after pseudo elements; S14: Determine whether there is a child element. If so, return to S12 and repeat the operation; S15: Return the processed Node instance data.
3. The method for automatically taking screenshots of a large-screen visualization of a network range according to claim 1, It is characterized in that The rendering of Canvas specifically renders the data processed by the cloning DOM process to the canvas, places the drawing nodes of the elements into multiple layers by generating a stacking context tree, determines the stacking order, and the logical processing of the stacking order is processed by renderStackContent, and then calls the underlying cancas API for rendering.
4. The method for automatically taking screenshots of a large-screen visualization of a network range according to claim 1, It is characterized in that The specific method flow of step S2 is as follows: S21: Implement CSS stacking layout rules through renderStackContent; S22: Set the ref reference of the iframe element to canvasRef; S23: Get the width and height of the iframe element through canvasRef; S24: Use html2canvas to process iframe elements; S25: Convert the base64 format data into a picture address.
5. A network range visualization large screen automatic screenshot system, It is characterized in that include: The cloning module is used to clone DOM elements based on html2canvas using the cloneNode method. The cloneNode method specifically performs a deep traversal from the root element downwards, uses the createElementClone method to create a Node instance object, and then uses the getComputedStyle method provided by the window object to obtain style data, processes the style data through the encapsulated style counting method, and uses this.resolvePseudoContent to mount the before and after pseudo-elements to determine whether there are child elements. If so, recursive remounting is performed, and finally the processed Node instance data is returned; The rendering module is used to render Canvas through html2canvas, process iframe elements and generate pictures; the processing of iframe elements includes html2canvas first drawing them into the canvas object step by step from top to bottom according to the stacking relationship of the elements, and then using the canvas's underlying API, toDataURL to convert the iframe elements into byte stream data, and then converting the byte stream through the background to generate an online address of the screenshot, and finally displaying the address of the picture through the src attribute of the img tag.
6. The network range visualization large screen automatic screenshot system according to claim 1, It is characterized in that The cloning module is used to clone DOM elements based on html2canvas using the cloneNode method, specifically including: Create a Node instance object through the createElementClone method; Use the getComputedStyle method to obtain the style and process the style data; Use this.resolvePseudoContent to mount the before and after pseudo elements; Determine whether there is a child element. If so, return to use the getComputedStyle method to obtain the style, process the style data, and repeat the operation; Returns the processed Node instance data.
7. The network shooting range visualization large screen automatic screenshot system according to claim 1, It is characterized in that The rendering of Canvas specifically renders the data processed by the cloning DOM process to the canvas, places the drawing nodes of the elements into multiple layers by generating a stacking context tree, determines the stacking order, and the logical processing of the stacking order is processed by renderStackContent, and then calls the underlying cancas API for rendering.
8. The network range visualization large-screen automatic screenshot system according to claim 1, It is characterized in that The rendering module renders Canvas through html2canvas, processes the iframe element, and generates a picture, which specifically includes: Implement CSS stacking layout rules through renderStackContent; set the ref reference of the iframe element to canvasRef; Get the width and height of the iframe element through canvasRef; Use html2canvas to process iframe elements; Convert base64 format data to image address.
9. A computer system comprising a memory, a processor and a computer program stored in the memory and executable on the processor, It is characterized in that When the computer program is loaded into the processor, the steps of the method for automatically taking screenshots of a large-screen visualization of a network shooting range according to any one of claims 1 to 4 are implemented.
10. A computer program product comprising a computer program / instructions, It is characterized in that When the computer program / instruction is executed by the processor, the steps of the method for automatically taking screenshots of a large-screen visualization of a network shooting range according to any one of claims 1 to 4 are implemented.
Citation Information
Patent Citations
Method for improving webpage screenshot definition based on WEB technology
CN112784202A
