从 vue 文件更改 p5 实例内的变量值
问题陈述:如何从 Nombres.vue
文件更改 dessin.js
中 myText
的值?
我正在尝试在 Quasar 项目中使用 p5.js
并且我有从 vues 与我的草图交互时遇到麻烦。
遵循此处解释的实例模式技术,我'到目前为止,我的草图已成功显示在我的页面中。
我的页面是 Nombre.vue
(它被插入到 MainLayout.vue
父页面中...),其内容是:
<template>
<q-page padding class="bg-primary">
<div class="row items-center justify-center">
<div id="p5Canvas" container class="col justify-center"></div>
</div>
</q-page>
</template>
<script>
const P5 = require("p5");
const dessin = require("../js/dessin.js");
export default {
name: "Nombres",
mounted() {
// NOTE: Use p5 as an instance mode
new P5(dessin.main);
},
};
</script>
在 dessin.js
文件中,我在 main
函数中拥有草图的所有代码:
let p5;
let myText = "foo"
export function main(_p5) {
p5 = _p5;
let fontFixed,
tailleFonte = 120,
couleurFond = 0,
xText = 0;
p5.preload = () => {
fontFixed = p5.loadFont("/fonts/manti_fixed.otf");
};
p5.setup = () => {
let canvas = p5.createCanvas(p5.windowWidth * 0.9, 500);
canvas.parent("p5Canvas");
// Set text characteristics
p5.textFont(fontFixed);
p5.textSize(tailleFonte);
p5.textAlign(p5.CENTER, p5.CENTER);
xText = (p5.windowWidth * 0.9) / 2;
};
p5.draw = () => {
p5.background(couleurFond);
p5.fill(250);
p5.text(myText, xText, 250);
};
}
Problem Statement : How can I change the value of myText
in dessin.js
from the Nombres.vue
file?
I'm trying to use p5.js
in a Quasar project and I'm having trouble to interact from vues with my sketch.
Following the instance mode technique explained here, I've succeeded in having my sketch displayed in my page so far.
My page is Nombre.vue
(and it is inserted in a MainLayout.vue
parent page...) and its contents is :
<template>
<q-page padding class="bg-primary">
<div class="row items-center justify-center">
<div id="p5Canvas" container class="col justify-center"></div>
</div>
</q-page>
</template>
<script>
const P5 = require("p5");
const dessin = require("../js/dessin.js");
export default {
name: "Nombres",
mounted() {
// NOTE: Use p5 as an instance mode
new P5(dessin.main);
},
};
</script>
In a dessin.js
file, I have all the code for the sketch in a main
function :
let p5;
let myText = "foo"
export function main(_p5) {
p5 = _p5;
let fontFixed,
tailleFonte = 120,
couleurFond = 0,
xText = 0;
p5.preload = () => {
fontFixed = p5.loadFont("/fonts/manti_fixed.otf");
};
p5.setup = () => {
let canvas = p5.createCanvas(p5.windowWidth * 0.9, 500);
canvas.parent("p5Canvas");
// Set text characteristics
p5.textFont(fontFixed);
p5.textSize(tailleFonte);
p5.textAlign(p5.CENTER, p5.CENTER);
xText = (p5.windowWidth * 0.9) / 2;
};
p5.draw = () => {
p5.background(couleurFond);
p5.fill(250);
p5.text(myText, xText, 250);
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 vue 变量作为函数的参数。我已经通过导入 js 文件解决了这个问题,而不需要它们。
像这样:
然后在你的 dessin.js 文件中你可以得到这些变量,比如:
希望这有帮助。
You can give the vue variables as a parameter to your function. I've solved this by importing the js files, and not require them.
Like this:
Then in your dessin.js file you can get those variables like:
Hope this helps.