Mei*_*hes 5 javascript canvas aurelia
有没有办法订阅视图以观察 Aurelia 中的调整大小/布局事件?我有一个要调整大小的画布。我想知道是否有一种“aurelia”的方式来做到这一点?
我试过:
<div ref="canvasContainer" class="widget-dial-container" resize.delegate="doresize()">
<canvas ref="canvas" class="widget-dial-canvas" />
</div>
Run Code Online (Sandbox Code Playgroud)
但它从不调用我的doresize()方法。
我已经尝试绑定到 DOM offsetWidth 和 offsetHeight,但这也不起作用(@bindable canvasContainer;在 vm 中有和没有)
正如 Kruga 所提到的,该resize事件仅window受其自身支持。您可以使用 Aurelia 平台抽象层的PLATFORM对象以跨平台方式附加到它。你必须跑jspm install aurelia-pal才能得到它。如果您不担心跨平台,那么您可以使用window对象。
以下模板和 VM 对我有用。我对调整大小计时器实施了限制:
HTML
<template>
<div style="height: 125px; min-width: 150px; width: 100%;" ref="canvasContainer">
<canvas ref="canvas" width.one-way="canvasContainer.offsetWidth"></canvas>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
模板
import {PLATFORM} from 'aurelia-pal';
export class App {
resizeTimer = null;
resizeEventHandler = () => this.resized();
attached() {
this.resized();
PLATFORM.global.addEventListener("resize", this.resizeEventHandler);
}
detached() {
PLATFORM.global.removeEventListener("resize", this.resizeEventHandler);
}
resized() {
clearTimeout(this.resizeTimer);
this.resizeTimer = setTimeout(() => {
let ctx = this.canvas.getContext("2d");
ctx.fillStyle = "green";
ctx.font = "30px Arial";
ctx.fillText(`Width: ${this.canvas.width}`,10,50);
}, 150);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3092 次 |
| 最近记录: |