Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
namespaces for dependencies imported by html tag

How can I make it so that I won't be able to use raw createCanvas, but rather through namespace like p5.createCanvas, while still having the dependencies handled by the html?

index.html

<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script language="javascript" type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>
    <script language="javascript" type="text/javascript" src="src/app.js"></script>
 
    <style>
        body {
            padding: 0;
            margin: 0;
        }
    </style>
</head>

<body>
</body>

</html>

app.js

function setup() {
    createCanvas(10, 10);
}

function draw() {
    background(0);
    circle(10, 10, 10);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think the closest thing to what you are looking for in p5.js is instance mode. By default p5.js will only globally export the p5 constructor. If you have globally declared a setup function then p5.js will globally export all of its functions, properties, and constants. If you don't want the global namespace polluted you can use instance mode instantiation, where you declare a function that takes an object that contains all of the p5 functions, properties, and constants (see example below).

function mySketch(p) {
  p.setup = function() {
    p.createCanvas(400, 400);
    p.background(100);
  };
  
  p.draw = function() {
    p.circle(p.mouseX, p.mouseY, 20);
  };
  
  p.mouseClicked = function() {
    if (window.createCanvas) {
      console.log('createCanvas is globally defined.');
    } else {
      console.log('createCanvas is NOT globally defined.');
    }
  }
}

new p5(mySketch);
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script language="javascript" type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>
 
    <style>
        body {
            padding: 0;
            margin: 0;
        }
    </style>
</head>

<body>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda