Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!