Javascript 调整HTML5画布大小以适应窗口
如何自动缩放HTML5Javascript 调整HTML5画布大小以适应窗口,javascript,html,html5-canvas,Javascript,Html,Html5 Canvas,如何自动缩放HTML5元素以适应页面 例如,我可以通过将高度和宽度属性设置为100%来缩放,但是不会缩放,是吗?如果你的div完全填满了网页,那么你就可以填充该div,从而有一个画布来填充该div 您可能会发现这很有趣,因为您可能需要使用css来使用百分比,但这取决于您使用的浏览器以及它与规范的一致程度: 画布的内在维度 元素的大小等于 用数字来协调空间 以CSS像素解释。然而, 该元素可以任意调整大小 通过样式表。在渲染过程中, 将缩放图像以适应此布局 尺寸 您可能需要获取div的偏移宽度和
元素以适应页面
例如,我可以通过将
高度
和宽度
属性设置为100%来缩放
,但是
不会缩放,是吗?如果你的div完全填满了网页,那么你就可以填充该div,从而有一个画布来填充该div
您可能会发现这很有趣,因为您可能需要使用css来使用百分比,但这取决于您使用的浏览器以及它与规范的一致程度:
画布的内在维度
元素的大小等于
用数字来协调空间
以CSS像素解释。然而,
该元素可以任意调整大小
通过样式表。在渲染过程中,
将缩放图像以适应此布局
尺寸
您可能需要获取div的偏移宽度和高度,或者获取窗口的高度/宽度并将其设置为像素值。除非您希望画布自动提高图像数据的比例(这正是James Black的答案所说的,但它看起来并不漂亮),否则您必须自己调整大小并重新绘制图像 我相信我已经找到了一个优雅的解决方案: JavaScript
/* important! for alignment, you should make things
* relative to the canvas' current width/height.
*/
function draw() {
var ctx = (a canvas context);
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
//...drawing code...
}
window.addEventListener("load", function()
{
var canvas = document.createElement('canvas'); document.body.appendChild(canvas);
var context = canvas.getContext('2d');
function draw()
{
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(0, 0); context.lineTo(canvas.width, canvas.height);
context.moveTo(canvas.width, 0); context.lineTo(0, canvas.height);
context.stroke();
}
function resize()
{
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
}
window.addEventListener("resize", resize);
resize();
});
CSS
html, body {
width: 100%;
height: 100%;
margin: 0;
}
body { margin: 0; }
canvas { display: block; }
到目前为止,还没有对我的性能造成太大的负面影响。基本上,你要做的是将onresize事件绑定到你的身体上,一旦捕捉到事件,你只需要使用window.innerWidth和window.innerHeight调整画布的大小
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Canvas Resize</title>
<script type="text/javascript">
function resize_canvas(){
canvas = document.getElementById("canvas");
if (canvas.width < window.innerWidth)
{
canvas.width = window.innerWidth;
}
if (canvas.height < window.innerHeight)
{
canvas.height = window.innerHeight;
}
}
</script>
</head>
<body onresize="resize_canvas()">
<canvas id="canvas">Your browser doesn't support canvas</canvas>
</body>
</html>
画布调整大小
函数resize_canvas(){
canvas=document.getElementById(“canvas”);
if(canvas.width
根据浏览器客户端的尺寸设置画布坐标空间的宽度和高度,需要您在调整浏览器大小时调整大小并重新绘制
一个不太复杂的解决方案是在Javascript变量中维护可绘制的维度,但根据screen.width、screen.height维度设置画布维度。使用CSS来适应:
#containingDiv {
overflow: hidden;
}
#myCanvas {
position: absolute;
top: 0px;
left: 0px;
}
浏览器窗口通常不会比屏幕本身大(除非屏幕分辨率被误报,因为它可能与不匹配的双显示器一起),因此背景不会显示,像素比例也不会变化。画布像素将与屏幕分辨率成正比,除非您使用CSS缩放画布 function resize(){
function resize() {
var canvas = document.getElementById('game');
var canvasRatio = canvas.height / canvas.width;
var windowRatio = window.innerHeight / window.innerWidth;
var width;
var height;
if (windowRatio < canvasRatio) {
height = window.innerHeight;
width = height / canvasRatio;
} else {
width = window.innerWidth;
height = width * canvasRatio;
}
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
};
window.addEventListener('resize', resize, false);
var canvas=document.getElementById('game');
var canvasRatio=canvas.height/canvas.width;
var windowRatio=window.innerHeight/window.innerWidth;
var宽度;
变异高度;
if(窗口比率<画布比率){
高度=窗内高度;
宽度=高度/画布比率;
}否则{
宽度=window.innerWidth;
高度=宽度*画布比率;
}
canvas.style.width=宽度+px;
canvas.style.height=高度+px;
};
addEventListener('resize',resize,false);
我认为这就是我们应该做的:
一种纯CSS方法添加到上述@jerseyboy的解决方案中。
适用于Firefox(在v29中测试)、Chrome(在v34中测试)和Internet Explorer(在v11中测试)
html,
身体{
宽度:100%;
身高:100%;
保证金:0;
}
帆布{
背景色:#ccc;
显示:块;
位置:绝对位置;
排名:0;
左:0;
右:0;
底部:0;
宽度:100%;
身高:100%;
}
var canvas=document.getElementById('canvas');
if(canvas.getContext){
var ctx=canvas.getContext('2d');
ctx.fillRect(25,25100100);
ctx.clearRect(45,45,60,60);
ctx.strokeRect(50,50,50,50);
}
链接到示例:
但是要小心,正如@jerseyboy在他的评论中所说:
用CSS重新缩放画布很麻烦。至少在铬和铬上 Safari,鼠标/触摸事件位置与 画布像素位置,您必须变换坐标 系统 CSS
html, body {
width: 100%;
height: 100%;
margin: 0;
}
body { margin: 0; }
canvas { display: block; }
JavaScript
/* important! for alignment, you should make things
* relative to the canvas' current width/height.
*/
function draw() {
var ctx = (a canvas context);
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
//...drawing code...
}
window.addEventListener("load", function()
{
var canvas = document.createElement('canvas'); document.body.appendChild(canvas);
var context = canvas.getContext('2d');
function draw()
{
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(0, 0); context.lineTo(canvas.width, canvas.height);
context.moveTo(canvas.width, 0); context.lineTo(0, canvas.height);
context.stroke();
}
function resize()
{
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
}
window.addEventListener("resize", resize);
resize();
});
我使用的是sketch.js,因此在对画布运行init命令后,我使用jquery更改宽度和高度。它以父元素为基础标注尺寸 $('DrawCanvas').sketch().attr('height',$('DrawCanvas').parent().height()).attr('width',$('DrawCanvas').parent().width())
下面的解决方案对我最有效。因为我对编码比较陌生,所以我喜欢通过视觉确认某些东西是否按照我期望的方式工作。我在以下站点找到了它: 代码如下:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Resize HTML5 canvas dynamically | www.htmlcheats.com</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden; /* Disable scrollbars */
display: block; /* No floating content on sides */
}
</style>
</head>
<body>
<canvas id='c' style='position:absolute; left:0px; top:0px;'>
</canvas>
<script>
(function() {
var
// Obtain a reference to the canvas element using its id.
htmlCanvas = document.getElementById('c'),
// Obtain a graphics context on the canvas element for drawing.
context = htmlCanvas.getContext('2d');
// Start listening to resize events and draw canvas.
initialize();
function initialize() {
// Register an event listener to call the resizeCanvas() function
// each time the window is resized.
window.addEventListener('resize', resizeCanvas, false);
// Draw canvas border for the first time.
resizeCanvas();
}
// Display custom canvas. In this case it's a blue, 5 pixel
// border that resizes along with the browser window.
function redraw() {
context.strokeStyle = 'blue';
context.lineWidth = '5';
context.strokeRect(0, 0, window.innerWidth, window.innerHeight);
}
// Runs each time the DOM window resize event fires.
// Resets the canvas dimensions to match window,
// then draws the new borders accordingly.
function resizeCanvas() {
htmlCanvas.width = window.innerWidth;
htmlCanvas.height = window.innerHeight;
redraw();
}
})();
</script>
</body>
</html>
动态调整HTML5画布大小| www.htmlcheats.com
html,正文{
宽度:100%;
身高:100%;
边际:0px;
边界:0;
溢出:隐藏;/*禁用滚动条*/
显示:块;/*侧面无浮动内容*/
}
(功能(){
变量
//使用canvas元素的id获取对该元素的引用。
htmlCanvas=document.getElementById('c'),
//在画布元素上获取用于绘图的图形上下文。
context=htmlCanvas.getContext('2d');
//开始侦听调整事件大小并绘制画布。
初始化();
函数初始化(){
//注册事件侦听器以调用resizeCanvas()函数
//每次调整窗口大小时。
window.addEventListener('r
**document.documentElement.client**Width
**inner**Width
**document.documentElement.client**Height
**inner**Height