[英]Automatically resize Html5 canvas with respect to the screen resolution
我创建了一个HTML5画布游戏,其中涉及从两个画布中拖动和选择对象(单词)。 屏幕上还有两个按钮。 但是不幸的是,由于我输入了在画布上绘制单词的坐标,因此我的程序在选择单词方面进行了硬编码。 我的屏幕分辨率是1366 x768。 现在,无论何时更改分辨率,按钮都会移动到其他位置,而鼠标永远不会选择拖动的单词,而是选择上方的其他单词。 我现在很麻烦,请帮忙!
<html>
<head>
<title>Word Search</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type = text/css href="style.css">
<script type="text/javascript" src="keywords.js"></script>
<script type="text/javascript" src="game.js"></script>
<script type="text/javascript" src="highlight.js"></script>
<script type="text/javascript" src="grid.js"></script>
<script src="http://code.createjs.com/easeljs-0.7.0.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<h1 id="heading">Find Keywords</h1>
<h3 id="results">Drag through the lettered Squares..!</h3>
<canvas id ='canvas1' height = 450 width="450" style= "border :1px solid black"> </canvas>
<canvas id ='canvas2' height = 450 width="250" style= "border :1px solid black"></canvas>
<input type='button' value="HIGHLIGHT ALL" id="button1" onclick="highlight();" />
<input type='button' value="NEXT" id="button2" onclick="next();"/>
<script>
var words =[];
window.onload = function(){
begin();
};
function begin()
{
wordSearchPuzzle();
}
function wordSearchPuzzle()
{
words.length=0;
words = getwords(8);
var puz =wordfind(words);
game(words,puz);
}
function next()
{
var canvas = document.getElementById("canvas1");
var ctx1 = canvas.getContext("2d");
var canvas2 = document.getElementById("canvas2");
var ctx2 = canvas2.getContext("2d");
ctx1.clearRect(0, 0, canvas.width, canvas.height);
ctx2.clearRect(0, 0, canvas2.width, canvas2.height);
wordSearchPuzzle();
}
</script>
</body>
</html>
这是可能的,我已经完成了他的工作,使用了可以根据屏幕大小动态调整大小的画布。
如果我没记错的话,我做了这样的事情。
我将画布放在div中,并给div设置了动态宽度,类似于style =“ width:30%”
我将画布放置在该div内,然后将CSS绑在一起,以使画布具有style =“ width:100%”
<div style="width:30%">
<canvas id="can" style="width:100%"></canvas>
</div>
我绑定到窗口调整大小事件并读取客户端大小,以确定更改画布的元素宽度和高度
window.resize = function(){
var canvas = document.getElementById("can");
canvas.width = canvas.clientWidth;
canvas.height = //some value based on width
}
然后,您需要重新绘制画布,因为调整大小会清除画布中的所有内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.