-
-
Save sunnylqm/3140747 to your computer and use it in GitHub Desktop.
HTML5 Canvas Game Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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