HTML5 Exercise (1) make the stars __html

Source: Internet
Author: User
1, understand canvas
< Canvas id = "Stars" height = "+" > </canvas > This is the canvas.
2, set the body background color
< style type = "Text/css" > body {background-color:black; } </Style > 3, initializing Canvas and context
Context as the global variable var context;      function init () {//get canvas var stars = document. getElementById ("stars"); windowwidth = window.      Innerwidth; Stars.      width = windowwidth; Get context = stars. GetContext ("2d"); }
4. Define Star objects
 //Star object var  star  =  function  () {     this. x  = -1;//Sit down Mark      this. y  = -1; Ordinate      this. Text = "*"; Text      this. Font = "Italic bold 24px serif";      this. color  =  "White"; Color     //Generate coordinates      this. GetPos = function () {         var  xx  =  windowwidth  *&n Bsp Math. Random ()  //not exceeding borders          var  yy  =  600  *   Math. Random ()  //do not exceed boundary          this. x  =  Math. Floor (XX);          this. y  =  Math. Floor (YY);     }     //produce random colors, four different brightness stars &NBSP;&Nbsp;   this. GetColor = function () {         var  _r  =  Math. Random ();          if (_r < 0.2) {               this. color  =   "#0000FF";         } else  if (_r < 0.5) {       & nbsp;      this. color  =  "White";         } else  if (_r < 0.8) {       & nbsp;      this. color  =  "#989898";         } else {               this. color  =  "#B8B8B8";         }     }     //Generate randomFontSize, maximum is 15 pixels, minimum 3 pixels      this. getfont  =  function () {         var  _r  =  Math. Ra Ndom () * 12+3;          this. font  =  "italic bold " + Math. Ceil (_r) + "Px serif";      }     //initialization      T His. init = function () {         this. getpos ();      & nbsp;   this. GetColor ();          this. GetFont ();     }     //drawing      this. Draw = function () {

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.