html - 角度画布在调整大小时不刷新
问题描述
我有一个 Angular Canvas,我想根据窗口大小调整它的大小。我在我的组件中这样做了:
@HostListener('window:resize', ['$event'])
onResize(event) {
this.canvaHeight = window.innerHeight * 0.91;
this.canvaWidth = window.innerWidth * 0.75;
}
在我的 html 中:
<canvas id="canvas" #canvas width="{{ canvaWidth }}" height="{{ canvaHeight }}" (mousedown)="select($event)" (mousemove)="move($event)" (mouseup)="drop()"></canvas>
当我调整窗口大小时,画布大小会正确更改,但上面的所有内容都会消失。当我执行调用更新函数的操作时,它会重新出现。但是如果我在 onResize 函数中调用它,它就不会再次出现,我也不知道为什么。
@HostListener('window:resize', ['$event'])
onResize(event) {
this.canvaHeight = window.innerHeight * 0.91;
this.canvaWidth = window.innerWidth * 0.75;
this.update();
}
任何想法 ?我必须以某种方式刷新 DOM 吗?那怎么办呢?
编辑:如果我移动一个对象,即使它还没有重新出现,它也确实被移动了。
我还尝试了 Promise 和一些刷新 DOM 的方法,例如 ApplicationRef.tick() 和 ChangeDetectorRef.detectChanges() 但它不起作用
解决方案
感谢 Blindman67,我找到了一种让它工作的方法:
@HostListener('window:resize')
onResize() {
this.canvaHeight = Math.floor(window.innerHeight * 0.91);
this.canvaWidth = Math.floor(window.innerWidth * 0.75);
this.canvas.nativeElement.height = this.canvaHeight;
this.canvas.nativeElement.width = this.canvaWidth;
this.update();
}
(我仍然需要存储其他东西的值)
(Math.floor 是为了更正浏览器的警告)
推荐阅读
- javascript - 通过 ajax 交付的 HTML 将内容远程加载到 Bootstrap 模式不起作用
- electron - 使用 electron-builder 的 snap 安装程序的默认主目录
- bash - 从命令输出计算数字的平均值
- javascript - 修复了打印 CSS 中的背景
- python - 如何控制json响应是否返回白页
- c# - 如何从非提升的 c# 程序更改系统时间
- python - Django - 使用第一个过滤的相关对象进行注释
- html - 使用 Safari 中的变换引导导航栏重叠
- r - 反转列中的值
- html - 在 Firebase 部署期间意外覆盖索引 html