来自 Javascript 的 Svelte Mount DOM 元素
Svelte Mount DOM Element from Javascript
我正在尝试使用如下所示的 Svelte 安装 pixi.js
canvas。 app.view
是一个 HTML Canvas
元素,但我不确定如何使用 Svelte 显示它。
<script>
import * as PIXI from 'pixi.js'
import { onMount } from 'svelte';
let app = new PIXI.Application({
width: 256, // default: 800
height: 256, // default: 600
antialias: true, // default: false
transparent: false, // default: false
resolution: 1 // default: 1
})
</script>
<style></style>
<app.view />
我只是暂时使用它,但如果能够将它添加到模板中就太好了。
onMount(() => {
document.body.appendChild(app.view);
})
来自 bind:this
的文档
To get a reference to a DOM node, use bind:this
.
<div bind:this={container}/>
let container;
onMount(() => {
container.appendChild(app.view);
});
这是一个活生生的例子:https://svelte.dev/repl/118b7d4540c64f8491d10a24e68948d7?version=3.12.1
如果您想避免包装器元素或自己实例化 canvas,您可以在 onMount
中创建 Pixi 应用程序并向其传递一个 canvas 元素:
<canvas bind:this={view}/>
let view;
let app;
onMount(() => {
app = new PIXI.Application({
view,
// ...other props
});
});
另见 the official bind:this
example 使用 canvas。
我正在尝试使用如下所示的 Svelte 安装 pixi.js
canvas。 app.view
是一个 HTML Canvas
元素,但我不确定如何使用 Svelte 显示它。
<script>
import * as PIXI from 'pixi.js'
import { onMount } from 'svelte';
let app = new PIXI.Application({
width: 256, // default: 800
height: 256, // default: 600
antialias: true, // default: false
transparent: false, // default: false
resolution: 1 // default: 1
})
</script>
<style></style>
<app.view />
我只是暂时使用它,但如果能够将它添加到模板中就太好了。
onMount(() => {
document.body.appendChild(app.view);
})
来自 bind:this
To get a reference to a DOM node, use
bind:this
.
<div bind:this={container}/>
let container;
onMount(() => {
container.appendChild(app.view);
});
这是一个活生生的例子:https://svelte.dev/repl/118b7d4540c64f8491d10a24e68948d7?version=3.12.1
如果您想避免包装器元素或自己实例化 canvas,您可以在 onMount
中创建 Pixi 应用程序并向其传递一个 canvas 元素:
<canvas bind:this={view}/>
let view;
let app;
onMount(() => {
app = new PIXI.Application({
view,
// ...other props
});
});
另见 the official bind:this
example 使用 canvas。