Tag: Color round code cursor path generates height HTML func
Practiced hand project: JavaScript Verification Code
1 <!DOCTYPE HTML>2 <HTML>3 4 <Head>5 <MetaCharSet= "UTF-8">6 <title></title>7 <style>8 #mycanvas{9 cursor:Pointer;Ten } One </style> A </Head> - <!--Place to optimize: random color of ① interference lines; ② can add a little bit of interference . - - <Body> the <CanvasID= "MyCanvas"width= ' All 'Height= ' + '> - your browser does not support canvas, please try a different browser ~ - </Canvas> - </Body> + - <Script> + /*Generate 4-bit random numbers*/ A functionrand () { at varStr= "abcdefghijklmnopqrstuvwxyz0123456789"; - vararr=Str.split (""); - varValidate= ""; - varRannum; - for(varI= 0; I< 4; I++) { - Rannum=Math.floor (Math.random ()* $); //random number between [0,35] in Validate+=Arr[rannum]; - } to returnValidate; + } - the /*random x-coordinate value of the interference line*/ * functionLineX () { $ varRanlinex=Math.floor (Math.random ()* -);Panax Notoginseng returnRanlinex; - } the + /*random y-coordinate value of the interference line*/ A functionLiney () { the varRanliney=Math.floor (Math.random ()* +); + returnRanliney; - } $ $ functionClickchange () { - varMyCanvas=document.getElementById ('MyCanvas'); - varCXT=Mycanvas.getcontext ('2d'); the Cxt.fillstyle= '#000'; - Cxt.fillrect (0, 0, -, +);Wuyi the /*generate 20 lines of interference*/ - for(varJ= 0; J< -; J++) { Wu Cxt.strokestyle= '#fff'; - Cxt.beginpath ();//if Beginpath is omitted, the number of lines of interference is accumulated for each click of the verification code. About Cxt.moveto (LineX (), Liney ()); $ Cxt.lineto (LineX (), Liney ()); - Cxt.linewidth= 0.5; - Cxt.closepath (); - Cxt.stroke (); A } + the Cxt.fillstyle= 'Yellow'; - Cxt.font= 'Bold 20px Arial'; $ Cxt.filltext (rand (), -, -); //fills the random number text generated by rand () into the canvas the } the the Clickchange (); the - /*Click on the verification code to replace*/ in Mycanvas.onclick= function(e) { the E.preventdefault ();//prevent mouse clicks from occurring by default behavior the Clickchange (); About }; the </Script> the the </HTML>
The results show:
JS Verification Code generation (entry level)