Javascript saves the webpage as an image by using the html2canvas library, canvasxpress

Source: Internet
Author: User

Javascript saves the webpage as an image by using the html2canvas library, canvasxpress

Step 1: Save the web page as a Canvas, with the help of html2canvas library, http://html2canvas.hertzen.com/

Html2canvas (document. getElementById ("id1"), {onrendered: function (canvas) {document. getElementById ("id2 "). appendChild (canvas); // how to deal with the canvas after it is generated. Of course, you can open it in a new label, display it on the floating layer, etc.}, canvas_id: 'canvas '// Add the canvas id by modifying the html2canvas source code });

Note: The first parameter of html2canvas () is the area of the canvas to be generated. If the canvas is generated for the entire webpage, It is document. body. For details about the second parameter, refer to the settings of canvas attributes on the official website. Of course, you can add attributes you want to modify the source code, such as adding IDs to canvas.

Step 2: Save the canvas generated in step 1 as an image

Var canvas = document. getElementById ("<span style =" font-family: Arial, Helvetica, sans-serif; "> canvas" </span> <span style = "font-family: Arial, helvetica, sans-serif; ">), </span> url = canvas. toDataURL (); // the following code downloads the image function var triggerDownload =$ ("<a> "). attr ("href", url ). attr ("download", "img.png "). appendTo ("body"); triggerDownload [0]. click (); triggerDownload. remove ();

Here we can focus on the toDataURL () method. We can convert the canvas to an image url in the form of data, and assign this url to the label to display the image, the other part of the code is the download function that you need.


Q: Can javascript code be fully stored when you use a browser to save a webpage as "xxxhtm" or an html document?

There is no problem when the JS Code is saved, but sometimes ajax is used in the background to dynamically fill data or change data. Such data only exists in the memory of the browser, it cannot be saved to a local hard disk.

Use JavaScript to click an image on an HTML webpage to obtain the image connection and store the connection in hidden.

<Html>
<Head>
</Head>
<Body>





<Input type = "hidden" id = "hidd" value = ""/>

</Body>
</Html>
<Script language = javascript>
Function ImgOnClick (){
// Alert (this. src );
Document. getElementById ("hidd"). value = this. src;
Window. open (this. src );
}

Var imgs = document. images;
For (var I = 0; I {
Imgs [I]. onclick = ImgOnClick;
}

</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.