首页 > 解决方案 > 当我有响应的画布宽度和高度时,html5画布的问题无法呈现矩形

问题描述

当我在代码中显示画布示例的响应宽度和高度时,html5 画布的问题无法呈现矩形。如果我删除它呈现矩形的代码的响应部分,但没有呈现矩形并且无法理解为什么

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')


canvas.width = innerWidth
canvas.height = innerHeight - 100


ctx.strokeRect(50, 50, 50, 50);


// without this part of code it renders rectangle but with it rectangle is not rendered
window.addEventListener('resize', resizeWindow)

function resizeWindow() {
    canvas.width = innerWidth
    canvas.height = innerHeight - 100
}
resizeWindow()

标签: canvas

解决方案


推荐阅读