Skip to content

Instantly share code, notes, and snippets.

@sunnylqm
Forked from wintercn/h5g
Created July 19, 2012 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnylqm/3140747 to your computer and use it in GitHub Desktop.
Save sunnylqm/3140747 to your computer and use it in GitHub Desktop.
HTML5 Canvas Game Template
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<!--允许全屏-->
<meta content="yes" name="apple-mobile-web-app-capable"/>
<meta content="yes" name="apple-touch-fullscreen"/>
<!--禁止电话号码和邮箱识别-->
<meta content="telephone=no,email=no" name="format-detection"/>
<!--TODO:添加一个ios用的icon-->
<link rel="apple-touch-icon" href="favicon.png"/>
<link rel="Shortcut Icon" href="favicon.png" type="image/x-icon" />
<!--TODO:添加一个网页标题-->
<title>A sample of HTML5 game</title>
<!--TODO:改成你想要的缩放比例-->
<meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no" />
<style type="text/css">
html,body {
margin: 0 0 0 0;
padding: 0 0 0 0;
width:100%;
height:100%;
}
body {
display:-webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-pack: center;
-webkit-box-align: center;
text-align:center;
cursor:default;
}
* {
-webkit-text-size-adjust: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-touch-callout: none;
}
</style>
<script type="text/javascript">
//关闭选择
document.addEventListener("selectstart",function(e){ e.preventDefault(); });
//避免鼠标变成文本选择形状
document.addEventListener("mousedown",function(e){ e.preventDefault(); });
//避免上下滚屏
document.addEventListener("touchmove",function(e){ e.preventDefault(); });
</script>
</head>
<body>
<canvas id="main" width="400" height="200"></canvas>
<script type="text/javascript">
//TODO:此处开始编写游戏初始化代码
var CANVAS_CSS_WIDTH = 400;
var CANVAS_CSS_HEIGHT = 200;
var BACKING_SCALE;
function backingScale(context) {
//如果是retina的iDevice,其window.devicePixelRatio为2,需要将canvas尺寸X2。
//如果是retina的mbp,其会自动将canvas尺寸X2。要排除这一自动X2的特例,需要检测context.webkitBackingStorePixelRatio是否为2。
if (window.devicePixelRatio > 1 && context.webkitBackingStorePixelRatio < 2) {
return window.devicePixelRatio;
}
return 1;
}
function init() {
var canvas = document.getElementById("#main");
var context = canvas.getContext("2d");
BACKING_SCALE = backingStore(context);
canvas.width = BACKING_SCALE * CANVAS_CSS_WIDTH;
canvas.height = BACKING_SCALE * CANVAS_CSS_HEIGHT;
context.fillRect(0,0,canvas.width,canvas.height);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment