1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
7+ < title > Document</ title >
8+ < style >
9+ # canvas {
10+ display : block;
11+ margin : 0 auto;
12+ background-color : # f1f1f1 ;
13+ }
14+ .ctrl {
15+ margin-top : 20px ;
16+ text-align : center;
17+ }
18+ span {
19+ user-select : none;
20+ }
21+ span : hover {
22+ background-color : # eee ;
23+ cursor : pointer;
24+ }
25+ h2 {
26+ margin : 5px ;
27+ text-align : center;
28+ }
29+ </ style >
30+ </ head >
31+ < body >
32+ < h2 > 动动大脑,推理一个有几个三角形、四、五、六边形?</ h2 >
33+ < canvas id ="canvas " width ="800 " height ="500 "> </ canvas >
34+ < div class ="ctrl ">
35+ < span id ="show "> 是否显示外轮廓</ span >
36+ < span id ="update "> 重新生成</ span >
37+ </ div >
38+ </ body >
39+ < script src ="./scene.js "> </ script >
40+ < script >
41+ let ctx = canvas . getContext ( '2d' )
42+ if ( devicePixelRatio ) {
43+ let oldWidth = canvas . width ;
44+ let oldHeight = canvas . height ;
45+ canvas . width = oldWidth * devicePixelRatio ;
46+ canvas . height = oldHeight * devicePixelRatio ;
47+ canvas . style . width = oldWidth + 'px' ;
48+ canvas . style . height = oldHeight + 'px' ;
49+ ctx . scale ( devicePixelRatio , devicePixelRatio ) ;
50+ }
51+ let game = new Scene ( ctx , 800 , 500 )
52+ let radius = [ 100 , 150 , 200 , 250 ]
53+ let speed = [ 1 , 1.5 , 2 , 2.5 ]
54+
55+ update . onclick = function ( ) {
56+ create ( )
57+ show . flag = false
58+ }
59+ show . onclick = function ( ) {
60+ show . flag = ! show . flag
61+ game . setOutline ( show . flag )
62+ }
63+ function create ( ) {
64+ let count = rand ( 5 , 11 )
65+ game . clear ( )
66+ for ( let i = 0 ; i < count ; i ++ ) {
67+ let p = new Polygon ( game , rand ( 3 , 7 ) , 100 * rand ( 1 , 7 ) , 100 * rand ( 1 , 4 ) , radius [ rand ( 0 , radius . length - 1 ) ] , 15 * rand ( 0 , 24 ) )
68+ p . setSpeed ( speed [ rand ( 0 , speed . length - 1 ) ] )
69+ game . add ( p )
70+
71+ }
72+ }
73+ function rand ( min , max ) {
74+ return ( ( max - min ) * Math . random ( ) + min ) | 0
75+ }
76+ </ script >
77+ </ html >
0 commit comments