使用javascript设置画布大小 [英] Set Canvas size using javascript

查看:393
本文介绍了使用javascript设置画布大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html中有以下代码:

I have the following code in html:

<canvas  id="myCanvas" width =800 height=800>

我想要而不是指定宽度作为 800 来调用JavaScript函数 getWidth()来获得宽度,例如

I want, instead of specifying the width as 800, to call the JavaScript function getWidth() to get the width e.g.

 <canvas  id="myCanvas" width =getWidth() height=800>

正确的语法是什么?因为我在做的事情是行不通的。

What is the correct syntax to do it? Because what I'm doing doesn't work.

推荐答案

您可以像这样设置宽度:

You can set the width like this :

function draw() {
  var ctx = (a canvas context);
  ctx.canvas.width  = window.innerWidth;
  ctx.canvas.height = window.innerHeight;
  //...drawing code...
}

这篇关于使用javascript设置画布大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆