Use html5 canvas to generate an image and save it locally

Source: Internet
Author: User
Tags linecap

Front-end code:

[Javascript]
Function drawArrow (angle)
{
// Init canvas
Var canvas = $ ('# cv_arrow') [0];
Var context = canvas. getContext ('2d ');
Var width = canvas. width;
Var height = canvas. height;
Context. clearRect (0, 0, width, height );
 
// Rotate
Var distance = iconSize/2 * Math. sqrt (2) * Math. sin (angle * Math. PI/180/2) * 2;
Var degree = 180-45-(180-angle)/2;
Var x = distance * Math. sin (degree * Math. PI/180 );
Var y = distance * Math. cos (degree * Math. PI/180 );
Context. translate (x,-y );
Var angleInRadians = angle * Math. PI/180;
Context. rotate (angleInRadians );
 
// Draw arrow
Context. fillStyle = 'rgb (0, 0, 0) '; // Black
Context. lineWidth = 1;
Context. strokeStyle = "#000000"; // Black
Context. lineCap = 'round '; // Circle angle
Context. lineJoin = 'round ';
Context. beginPath ();
Context. moveTo (iconSize/2, border );
Context. lineTo (border, iconSize-border );
Context. lineTo (iconSize/2, iconSize/2 );
Context. fill ();
Context. stroke ();
Context. closePath ();
Context. save ();
 
Context. restore ();
Context. fillStyle = 'rgb (255,255,255) '; // White
Context. lineWidth = 1;
Context. strokeStyle = "#000000 ";
Context. lineCap = 'round ';
Context. lineJoin = 'round ';
Context. beginPath ();
Context. moveTo (iconSize/2, border );
Context. lineTo (iconSize-border, iconSize-border );
Context. lineTo (iconSize/2, iconSize/2 );
Context. fill ();
Context. stroke ();
Context. closePath ();
Context. save ();
 
_ Canvas = canvas;
}
Code sent to the background:

[Javascript]
For (var I = 0; I <360; I ++)
{
DrawArrow (1 );
 
Var data = _ canvas. toDataURL ();
 
// The prompt message before deleting the string "data: image/png; base64 ,"
Var b64 = data. substring (22 );
 
$. Ajax ({url: "RotateCanvas. aspx", data: {data: b64, name: I. toString ()}, success:
Function ()
{
// Alert ('OK ');
}
});
}
Code received in the background:

[Csharp]
If (Request ["name"]! = Null)
{
String name = Request ["name"];
String savePath = Server. MapPath ("~ /Images/output /");
 
Try
{
FileStream fs = File. Create (savePath + "/" + name + ". png ");
Byte [] bytes = Convert. FromBase64String (Request ["data"]);
 
Fs. Write (bytes, 0, bytes. Length );
Fs. Close ();
}
Catch (Exception ex)
{
}
}
The final result is as follows:

 

The image generation is good, non-distorted, and transparent, and does not require post processing.

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.