canvas宽高问题,已解决 发表于 2018-09-27 | 分类于 前端 , Canvas | | 阅读次数 问js 设置 canvas 宽高为浏览器可用宽高,滚动条仍然出现,如下代码 123456789101112131415161718192021222324252627282930313233343536373839404142<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>CANVAS</title> <style> * { padding: 0; margin: 0; } html, body { height: 100%; } #canvas { background-color: #cba; } </style></head><body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'); canvas.width = document.body.clientWidth; // canvas.width = window.innerWeight; canvas.height = document.body.clientHeight; // canvas.height = window.innerHeight; context = canvas.getContext('2d'); context.beginPath(); context.arc(100, 100, 50, 0, 1.5 * Math.PI, true); context.closePath(); context.strokeStyle = '#f0f'; context.stroke(); </script></body></html> 答用一元素包裹一下即可 1234567891011121314151617181920212223242526272829303132333435<!DOCTYPE html><html><head> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } .canvas-box { /* IE兼容问题 */ /* display: flex; */ font-size: 0; } canvas { background-color: #fcf; } </style></head><body> <div class="canvas-box"> <canvas></canvas> </div> <script> var canvas = document.querySelector('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; </script></body></html> 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏