JavaScript asynchronous programming using Jscex to draw circles

Source: Internet
Author: User

I have been paying attention to Zhao's jscex for a long time. jscex uses the infinite possibilities of eval (str), from "$ async" to "async", and never supports if else or if else ·, jscex is constantly being improved and optimized. Jscex can be fully invested in the production environment. I finished talking about pi yesterday. Let's draw a circle today!

In a browser that supports html 5Run the following code:

 
 
  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <body> 
  4.  
  5. <canvas id="myCanvas" width="480" height="300" style="border:1px solid #c3c3c3;"> 
  6. Your browser does not support the canvas element.  
  7. </canvas> 
  8. <script type="text/javascript"> 
  9.  
  10.     var c = document.getElementById("myCanvas");  
  11.     var ccxt = c.getContext("2d");  
  12.     var x = 150;  
  13.     var y = 150;  
  14.     var r = 100;  
  15.     cxt.moveTo(x - r, y);  
  16.     for (var i = x - r; i < x + r + 1; i++) {  
  17.         var tempY = Math.pow(r * r - (x - i) * (x - i), 1 / 2);  
  18.         cxt.lineTo(i, y + tempY);  
  19.     }  
  20.     cxt.moveTo(x - r, y);  
  21.     for (var i = x - r; i < x + r + 1; i++) {  
  22.         var tempY = Math.pow(r * r - (x - i) * (x - i), 1 / 2);  
  23.         cxt.lineTo(i, y - tempY);  
  24.     }  
  25.       cxt.stroke();  
  26.  
  27. </script> 
  28.  
  29. </body> 
  30. </html> 

The following is displayed in the Canvas:

However, we clearly draw circles. Why didn't we see the circle process? Javascript is like this. After the explanation is complete, the process of explanation is not displayed. This is why javascript does not need to consider multithreading, just the UI thread. So how can we see the circle process?

Jscex debut!

 
 
  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <body> 
  4. <canvas id="myCanvas" width="480" height="300" style="border:1px solid #c3c3c3;"> 
  5. Your browser does not support the canvas element.  
  6. </canvas> 
  7.     <script language="javascript" type="text/javascript" src="lib/uglifyjs-parser.js"></script> 
  8.     <script language="javascript" type="text/javascript" src="src/jscex.js"></script> 
  9.     <script language="javascript" type="text/javascript" src="src/jscex.builderBase.js"></script> 
  10.     <script language="javascript" type="text/javascript" src="src/jscex.async.js"></script> 
  11. <script type="text/javascript"> 
  12.     var c = document.getElementById("myCanvas");  
  13.     var ccxt = c.getContext("2d");  
  14.     var x = 150;  
  15.     var y = 150;  
  16.     var r = 100;  
  17.     var drawAsync = eval(Jscex.compile("async", function () {  
  18.         cxt.moveTo(x - r, y);  
  19.         for (var i = x - r; i < x + r + 1; i++) {  
  20.             $await(Jscex.Async.sleep(10));  
  21.             var tempY = Math.pow(r * r - (x - i) * (x - i), 1 / 2);  
  22.             cxt.lineTo(i, y + tempY);  
  23.             cxt.stroke();  
  24.         }  
  25.         cxt.moveTo(x - r, y);  
  26.         for (var i = x - r; i < x + r + 1; i++) {  
  27.             $await(Jscex.Async.sleep(10));  
  28.             var tempY = Math.pow(r * r - (x - i) * (x - i), 1 / 2);  
  29.             cxt.lineTo(i, y - tempY);  
  30.             cxt.stroke();  
  31.         }  
  32.         
  33.     }));  
  34.     drawAsync().start();  
  35. </script> 
  36.  
  37. </body> 
  38. </html> 

In this way, you can witness the entire process of circle painting!

Your browser does not support the canvas element.

For more information about js, go to callback ····

Original article: http://www.cnblogs.com/iamzhanglei/archive/2011/08/16/2140113.html

  1. ToDo website example developed based on Node. js, Express, and Jscex
  2. Use Jscex to improve JavaScript asynchronous programming experience
  3. Use HTML 5 and Javascript to design plotting programs
  4. 16 excellent JavaScript tutorials and library recommendations
  5. The inherent disability of JavaScript in Dart VS JavaScript


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.