从 vue 文件更改 p5 实例内的变量值

发布于 2025-01-11 03:12:48 字数 1856 浏览 0 评论 0原文

问题陈述:如何从 Nombres.vue 文件更改 dessin.jsmyText 的值?

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2025-01-18 03:12:48

您可以将 vue 变量作为函数的参数。我已经通过导入 js 文件解决了这个问题,而不需要它们。

像这样:

import P5 from 'p5';
import { fromCenter } from '../scripts/algorithms/fromCenter';
import { circlePacking } from '../scripts/algorithms/circlePacking';
import { mazeGenerator } from '../scripts/algorithms/mazeGenerator';
import { colorDistance } from '../scripts/algorithms/colorDistance';
import { shrinkingCircles } from '../scripts/algorithms/shrinkingCircles';
import { randomOpacity } from '../scripts/algorithms/randomOpacity';

export default {
    name: 'Game',
    data() {
        return {
            averageColor: {},
            method: 5,
            image: 'src/components/icons/koe.jpg',

        };

    },
    mounted() {
        let p5;
        let game = document.getElementById('game');
        switch (this.method) {
            case 0:
                p5 = new P5(fromCenter(game, this.image));
                break;
            case 1:
                p5 = new P5(circlePacking(game, this.image));
                break;
            case 2:
                p5 = new P5(mazeGenerator(game, this.image));
                break;
            case 3:
                p5 = new P5(colorDistance(game, this.image));
                break;
            case 4:
                p5 = new P5(shrinkingCircles(game, this.image));
                break;
            case 5:
                p5 = new P5(randomOpacity(game, this.image));
                break;

然后在你的 dessin.js 文件中你可以得到这些变量,比如:

export function randomOpacity(element, imageToShow) {
return function (p) {
    const p5Canvas = element;

希望这有帮助。

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:

import P5 from 'p5';
import { fromCenter } from '../scripts/algorithms/fromCenter';
import { circlePacking } from '../scripts/algorithms/circlePacking';
import { mazeGenerator } from '../scripts/algorithms/mazeGenerator';
import { colorDistance } from '../scripts/algorithms/colorDistance';
import { shrinkingCircles } from '../scripts/algorithms/shrinkingCircles';
import { randomOpacity } from '../scripts/algorithms/randomOpacity';

export default {
    name: 'Game',
    data() {
        return {
            averageColor: {},
            method: 5,
            image: 'src/components/icons/koe.jpg',

        };

    },
    mounted() {
        let p5;
        let game = document.getElementById('game');
        switch (this.method) {
            case 0:
                p5 = new P5(fromCenter(game, this.image));
                break;
            case 1:
                p5 = new P5(circlePacking(game, this.image));
                break;
            case 2:
                p5 = new P5(mazeGenerator(game, this.image));
                break;
            case 3:
                p5 = new P5(colorDistance(game, this.image));
                break;
            case 4:
                p5 = new P5(shrinkingCircles(game, this.image));
                break;
            case 5:
                p5 = new P5(randomOpacity(game, this.image));
                break;

Then in your dessin.js file you can get those variables like:

export function randomOpacity(element, imageToShow) {
return function (p) {
    const p5Canvas = element;

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文