Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vickymin #35

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 101-clickable-grid/bootstrap.min.css

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions 101-clickable-grid/grid.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
body{
background-color: #CCC;
}
#box{
width:650px;
height:700px;
margin:0px auto;
}
form{
width:550px;
height:40px;
margin-top:20px;
margin-left:80px;
}
#generate, #clear{
background-color:#777;
color:#EEE;
width:80px;
margin-right:15px;
}
#generate:hover,#clear:hover{
background-color:#555;
width:80px;
box-shadow: 3px 3px 5px #555;
}
#main{
border:2px solid #999;
margin-top:5px;
margin-left: 20px;
background-color:#92A8D1;
}
26 changes: 26 additions & 0 deletions 101-clickable-grid/grid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<head>
<meta name="author" content="minjiepei">
<meta charset="utf-8">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="grid.css">
</head>

<body>
<div id="box">
<form role="form">
<div class="col-sm-7">
<div class="input-group-md">
<input type="text" class="form-control" id="integer" placeholder="select an integer between 1 to 20">
</div>
</div>
<div class="col-sm-5">
<button type="button" id="generate" class="btn btn-md">Generate</button>
<button type="reset" id="clear" class="btn btn-md">Clear</button>
</div>
</form>
<canvas id="main" width="600" height="600">Please upgrade your browser!</canvas>
</div>
<script src="grid.js"></script>

</body>
71 changes: 71 additions & 0 deletions 101-clickable-grid/grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var canvas = document.getElementById("main");
var context = canvas.getContext("2d");

var number; //用户输入的数字
var w; //方格边长
var space; //方格之间空隙

window.onload = function(){
//点击“Generate”按钮
var gen_button = document.getElementById("generate");
gen_button.onclick = generateHandler;
//点击“Clear”按钮
var clear_button = document.getElementById("clear");
clear_button.onclick = function(){clearCanvas(context,canvas);}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

都用camelCase,例如clearButton

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,谢谢老师!

}

//处理“Generate”按钮
var generateHandler = function(){
clearCanvas(context,canvas);
number = parseInt(document.getElementById("integer").value);
w = Math.floor(canvas.width / number) /1.03;
space = (canvas.width-number*w)/(number+1);
//判断用户输入数字是否在范围内
if (0 < number && number < 27){
drawGrid(context);
}
else{
alert("Plese enter an integer between 1 to 20!");
}
//监听用户点击方格的位置
canvas.addEventListener("mousedown", showPosition, false);
}

//清空canvas
var clearCanvas = function(context,canvas){
context.fillStyle = "#92A8D1";
context.fillRect(0,0,canvas.width,canvas.height);
}

//在canvas上画出方格及相应数字
var drawGrid = function(context){
var i,j;
for(i=0; i<number; i++){
for(j=0; j<number; j++){
//画方格
context.fillStyle = "#F5B1B1";
context.fillRect(i*w+(i+1)*space, j*w+(j+1)*space, w, w);
//画数字
context.fillStyle = "#92A8D1";
context.textAlign = "center";
context.font = (w/3).toString() + "px" + " Arial";
context.fillText(number*j+i+1, i*w+(i+1)*space+w/2, j*w+(j+1)*space+w/1.6);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

像1.6这种东西都是最好写成一个constant var ABC = 1.6

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的我改下

}
}
}

//找到用户点击方格的位置
var showPosition = function(event){
var x = event.clientX - canvas.offsetLeft;
var y = event.clientY - canvas.offsetTop;
var i,j;
for(i=0; i<=number; i++){
for(j=0; j<=number; j++){
if(x>=i*space+i*w && x<=i*space+(i+1)*w){
if(y>=j*space+j*w && y<=j*space+(j+1)*w){
console.log(number*j+i+1);
}
}
}
}
}