3 回答
TA贡献1810条经验 获得超4个赞
只有内嵌css有效,外部css会出现拉伸的情况,
所以有两种方案
var myCanvas = "<canvas id='myCanvas' width='" + screen.availWidth + "px' height='"+ screen.availHeight + "px'></vanvas>";
document.body.insertAdjacentHTML("beforeEnd", myCanvas);
或者这种做法
var myCanvas = document.createElement("canvas");
myCanvas.setAttribute("width", screen.availWidth);
myCanvas.setAttribute("height", screen.availHeight);
myCanvas.setAttribute("id", "myCanvas");
document.body.appendChild(myCanvas);
两种方案的共性都是在把元素添加进DOM前设置他的大小
TA贡献2080条经验 获得超4个赞
css样式设置width:100%;height:100%;然后用下面四种方法皆可:
1.
canvas.width = document.documentElement.clientWidth;<br>
canvas.height = document.documentElement.clientHeight;
2.
canvas.width = screen.availWidth;<br>
canvas.height = screen.availHeight;
3.
canvas.width = screen.width;<br>
canvas.height = screen.height;
4.
canvas.width = window.innerWidth;<br>
canvas.height = window.innerHeight;
- 3 回答
- 0 关注
- 1250 浏览
添加回答
举报