9.11: Instance Mode (aka "namespacing") - p5.js Tutorial

Поделиться
HTML-код
  • Опубликовано: 11 янв 2025

Комментарии •

  • @palmweaver
    @palmweaver 6 лет назад +16

    Best namespace example video on YT, hands down. Also a pretty decent, slimmed down introduction to Object Oriented Programming in JS. My hat is off to you, sir

  • @wojtek4351
    @wojtek4351 5 лет назад +3

    Oh, that is amazing. Just spent last hour trying to figure out how to use this through reading the documentation and accidentally saw that you made a video on it. So much more clear. Thank you once again!

  • @justinmlawrence
    @justinmlawrence 8 лет назад +23

    You're awesome. p5.js is awesome.

  • @ddv2nine722
    @ddv2nine722 6 лет назад

    Who knows why, I decided to create a page. Deciding to deal with the idea of a second canvas that I wanted to add, later. I just been like 20 minuts adding "canvas1." everywhere xDD
    I think I have a special god for me, because it worked perfectly at the first try. I was So happy whe I saw it working. Thanks !

  • @SpikyCat
    @SpikyCat 8 лет назад +1

    Hey Daniel, im a yound programmer and i loved your book "The Nature of Code" keep up the good work!

    • @TheCodingTrain
      @TheCodingTrain  8 лет назад

      +SpikyCat thank you so much for the nice feedback!!

  • @mattmapleston3011
    @mattmapleston3011 4 года назад

    The best description and example of instance mode anywhere!

  • @goldthumb
    @goldthumb 2 года назад

    Thanks for letting us know some "secrets" behind p5js.

  • @marshahuang6126
    @marshahuang6126 3 года назад

    I have read instance mode on the p5 website but still lost of what it means, this video really helps me a lot.

  • @troythomas3135
    @troythomas3135 2 года назад

    That was great, I’m actually in a jam on my code and this helped lots

  • @arielbustamante
    @arielbustamante 4 года назад +1

    hey!, ideas about how to share global variables on instance mode sketches, i mean, sharing variables between instance mode sketches?

  • @hanslee2105
    @hanslee2105 7 лет назад +5

    So how do you write in a class with constructors and functions for that class inside of instance mode?

    • @KutadguB
      @KutadguB 7 лет назад +2

      did you find an answer yet? I am looking for a solution too

    • @hanslee2105
      @hanslee2105 7 лет назад

      Nope not yet, looked everywhere....still nothing.

    • @TheExenter
      @TheExenter 7 лет назад

      I'm looking for a solution too. Found nothing yet

    • @aymericbiancopelle1149
      @aymericbiancopelle1149 6 лет назад +1

      I found this solution pastebin.com/EyNbemRi kindof hacky but it works

    • @airhead3409
      @airhead3409 6 лет назад

      Just in case you're still wondering.
      var sketch = function(p) {
      var myObj;
      p.setup = function() {
      myObj = new MyObj(p); //

  • @QWEEKEN73
    @QWEEKEN73 4 года назад

    Was looking for something exactly like this! I'm using p5 to render a 3D image, and allowing the user to edit the color & spin the image 360. I needed a way to reset the Z-spin back to normal before converting the canvas to jpg. It kept saving the image at weird angles... I was able to do it w/ pure js, but being able to create my own method on a p5 obj keeps inline with everything! Thanks man!

  • @writethatdown100
    @writethatdown100 Год назад

    Can you just pass an empty function into p5()? So that way everything is outside the sketch function

  • @OwenHodges
    @OwenHodges 2 года назад

    What if my draw function calls another function that I defined. Do I slap "p." in front of that as well?

  • @1732ashish
    @1732ashish 6 лет назад

    so what i was trying is reading one canvas onto another.. but it seems sketch functions setup is called asynchronously. so how to get instance of first canvas in second canvas ? basically some hook for canvas loaded ?

  • @braidata
    @braidata 2 года назад

    Our coding idol!

  • @LouisBadak
    @LouisBadak 2 года назад

    hello, did anyone here try to use p5sound with instance mode? i've been trying to create an oscillator in an instance canvas and really can't find a way to create one... I keep getting the "p5.Oscillator() is not a constructor"...

  • @yiyokyuk2605
    @yiyokyuk2605 4 года назад

    Thanks~~~ I wonder how to embed this into Java JS file for making a two canvas webpage ?

  • @cimmik
    @cimmik Год назад

    It seems this could be very useful when working with genetic algorithms, when I want to visualize many at once and it doesn't make sense to show all examples on the same sketch.

  • @NiuzuGaming
    @NiuzuGaming 5 лет назад

    Great Video! Is there any way to add a canvas below the first one? Would be awesome to play around with a grid of canvas'.

  • @souvikroy6237
    @souvikroy6237 3 года назад

    i want to delete the existing instance of p5 and create another one.
    how do i do that?

  • @anamariaarbelaezlopez4783
    @anamariaarbelaezlopez4783 2 года назад

    thank you again!! ♥ i love your videos

  • @mikewillett2812
    @mikewillett2812 8 лет назад +2

    Thanks Daniel, great set of tutorials.
    This might be a noob question but when in instance mode, is there a way to pass variables through rather than needing to "copy, paste and edit" the way you did with the colors at the end of the video? If so, can you pass in p5 objects like Tables? I am trying to do show a number of sketches on one page (each with their own div tag so they can be found) but the data may vary and I want them to look the same so while all the same functions will be used only the underlying data will vary.

    • @TheCodingTrain
      @TheCodingTrain  8 лет назад +3

      Yes, you could create multiple p5 instances with the same codebase but pass in different data as input. This would be a great idea for a follow-up tutorial to this!

  • @leajiaure
    @leajiaure 6 лет назад

    Any idea how to call the instance of your sketch function in HTML? Because mtp5 is a variable not a function so I'm not sure how

  • @littlebachmonster
    @littlebachmonster 6 лет назад

    I love you. Im making a game with different screens with html buttons and toggling between them is very difficult

  • @martijnmulder5546
    @martijnmulder5546 7 лет назад +3

    Can you show how I can combine lesson 6.6: Multiple js Files with this lesson 9.8: Instance Mode (aka "namespacing").
    Also want to say a big Thank You for the spirit and depth of your tutorials :)

    • @TheCodingTrain
      @TheCodingTrain  7 лет назад

      I like this idea, could you ask here? github.com/CodingTrain/Rainbow-Topics/issues

    • @BlackSlimShady
      @BlackSlimShady 5 лет назад

      @@TheCodingTrain Was this ever done? Would love to see it!

    • @edwardcerverizzo7363
      @edwardcerverizzo7363 4 года назад +1

      @@BlackSlimShady There was a post on Github about it, but not much else.
      github.com/CodingTrain/Rainbow-Topics/issues/541

  • @sabinesformula
    @sabinesformula 8 лет назад +1

    So glad I found this tutorial!
    Is it possible to combine global mode with instance mode?
    For instance, under the global setup environment, if I press a button, turn sketch1 on and if i press another button, turn sketch2 on? THANK YOU!

    • @sabinesformula
      @sabinesformula 8 лет назад +1

      specifically, I want to load sound with p5.sound in global mode and use the same soundfile while changing to different sketches...

  • @metacarpo10
    @metacarpo10 4 года назад +1

    What about using external classes? What about if those classes use p5 objects and methods, like for example createVector(x,y) or max(v1,v2)?

    • @UrbanPretzle
      @UrbanPretzle 4 года назад

      I'm wondering the exact same - did you find a solution to this?

    • @metacarpo10
      @metacarpo10 4 года назад +1

      @@UrbanPretzle hi, yes I did, when creating an class object you have to give it the 's' prop parameter used in the instance mode as a property of the object. After that, any p5 method you want to use inside the class, like draw an ellipse or change the fill color, is a method of s, like s.ellipse(...) , s.fill(...), s.noStroke() etc.

    • @UrbanPretzle
      @UrbanPretzle 4 года назад

      @@metacarpo10 Thank you! I have already done this, thanks for checking :D

  • @aehphea1411
    @aehphea1411 7 лет назад +3

    Love you Daniel.
    For the camera going off problem; have you tried magic lantern software? (if you are using canon dslr) It might help you.

    • @TheCodingTrain
      @TheCodingTrain  7 лет назад +1

      Yes, I just haven't had the chance to try it but I should!

    • @aehphea1411
      @aehphea1411 7 лет назад

      I wish you have all the time to do more and more of these amazing stuff.

  • @DENMIR01
    @DENMIR01 4 года назад

    Hello Mr.Shiffman! I'm struggling with one project using p5.js, trying to make a multi-layer drawing app. I need to clear the drawing board. And I can make it neither by redrawing background (because it will cover other layers) nor by clear() function for createGraphic (because it's not implemented in p5.js and only works in Processing or just working other way and can't be used for my purpose). Recreating createGraphic leaves old drawings on the screen. Any suggestions?

    • @thecodedmastergeodash8807
      @thecodedmastergeodash8807 4 года назад

      You might be able to store every pixel that is colored in in an array, and draw using that array.
      var layer1 = [[200, 20], [201, 21], [202, 21]];
      var layer2 = [200, 30], [199, 31], [198, 33]];
      for(var i = 0; i < layer1.length; i++) {
      ellipse(layer1[i][1], layer1[i][2], size, size);
      }
      And then something that pop()s the array until it is empty when you delete a layer

  • @gregbaker3463
    @gregbaker3463 2 года назад

    can I use this to overlay a confetti code to drop over another draw function?

    • @gregbaker3463
      @gregbaker3463 2 года назад

      I have a sketch that builds and ice cream cone and I want to connect that sketch to another sketch that drops confetti

  • @aurelianobuendia24
    @aurelianobuendia24 8 лет назад +1

    So. . . how you add an object in instance mode?

    • @aurelianobuendia24
      @aurelianobuendia24 8 лет назад

      How to use parameters to an object in instance mode or things like that.

  • @myztazynizta
    @myztazynizta 8 лет назад +1

    Do something with offscreen canvases and compositing and alpha channels on images.

  • @guillaumerivet9570
    @guillaumerivet9570 4 года назад

    Hey Daniel : can we convert [ ] to mode instance. your tutorials help me so much : Guillaume

  • @haos4574
    @haos4574 6 лет назад

    how do you access the elements in the script from the console when you are debugging?

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад

      This workflow video series might help! ruclips.net/p/PLRqwX-V7Uu6Zu_uqEA6NqhLzKLACwU74X

  • @abmuoz
    @abmuoz 8 лет назад

    How can you display multiple sketches on a single page that are not necessarily the same sketch? Thanks :)

    • @TheCodingTrain
      @TheCodingTrain  8 лет назад

      You just need to create two "seed" sketch objects/functions. Using s might be easier than instance mode.

  • @ac3raven
    @ac3raven 4 года назад

    Can one canvas overlap another?

  • @jorgeandresgarciamontiel2420
    @jorgeandresgarciamontiel2420 5 лет назад

    Can you create a myp5 element inside a class in a different .js file?

    • @TheCodingTrain
      @TheCodingTrain  5 лет назад

      Would you mind asking at discourse.processing.org/! It's a better platform for Processing and p5.js related code questions. You can share code there easily! Feel free to link from here to your post.

  • @jithunniks
    @jithunniks 6 лет назад

    Is there any tool which lets us convert a p5 sketch code to instance mode code automatically?

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад +1

      Oh, this is a nice idea! I don't believe I've seen anyone make something like this.

    • @jithunniks
      @jithunniks 6 лет назад

      I found something github.com/DrSensor/p5-global2instance

  • @1732ashish
    @1732ashish 6 лет назад

    can i have mulitple canvas kind of thingy in processing Java mode ?

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад +1

      Take a look at createGraphics(). . but not easy to open multiple windows at once (but possible!)

    • @1732ashish
      @1732ashish 6 лет назад

      thanks for the reply

  • @viniciusdemoraesnascimento7055
    @viniciusdemoraesnascimento7055 4 года назад

    Great Daniel! thank you so much o/

  • @witsepanneels6085
    @witsepanneels6085 5 лет назад

    Your y variable isn't declared yet you're using it to draw the circles ???? How?

    • @witsepanneels6085
      @witsepanneels6085 5 лет назад

      I know you changed it in the middle of the video but why does it work before you change it

    • @witsepanneels6085
      @witsepanneels6085 5 лет назад

      Also could you use this. Instead of using p.?

  • @furrane
    @furrane 8 лет назад +3

    5:21 Nothing to add ^^

  • @elmo8696
    @elmo8696 6 лет назад +2

    I like the word window. My variable would never be anything but WINDOW

  • @chachf
    @chachf 4 года назад

    I find it incredible that this "p." must really be added in front of EVERYTHING... It turns the code into something much too heavy, horrible for testing or debugging. Wouldn't it be possible to introduce something such as "pushMatrix()"? That is: "This is a piece of code in which each command should be understood as p.command"
    And does it really make sense to put a "p." in front of a UNIVERSAL CONSTANT like PI!? I guess I will end up using s instead...(Sorry, I should have started by saying: excellent video, thank you Daniel!)

    • @Decimated_By_A_Train
      @Decimated_By_A_Train 4 года назад

      That defeats the purpose of instanced mode though, if you want to treat the private members as global just use global mode

  • @jorgeandresgarciamontiel2420
    @jorgeandresgarciamontiel2420 5 лет назад

    hey Daniel im a big fan and I have been watching your videos for a long time... I have a question that I don't know how to solve.
    When creating a class I cannot make it so the class gets all the functions of p5.js library.
    Can anyone help me?
    it says fill is not defined
    var game = function(g) {
    g.setup = function() {
    g.createCanvas(400,400);
    g.background(0);
    g.snake = new Snake();
    g.snake.test();
    }
    }
    class Snake {
    test() {
    fill(255); //this line throws error (Uncaught ReferenceError: fill is not defined)
    ellipse(0,0,100);
    }
    }
    var mySnake = new p5(game);

    • @amy-goodchild
      @amy-goodchild 4 года назад +1

      I realise this was a year ago, but in case it helps anyone else... It's because you are calling fill and ellipse outside of the p5js namespace. You need to use mySnake.fill() and mySnake.ellipse

  • @aurelianobuendia24
    @aurelianobuendia24 8 лет назад

    Whhhhyyyyyy shiffman , why can´t you add two canvas if not with the instance mode???? Why do I need to use this hard mode ? ?

    • @TheCodingTrain
      @TheCodingTrain  8 лет назад +2

      i might also suggest just using s as that then won't require instance mode.

    • @aurelianobuendia24
      @aurelianobuendia24 8 лет назад

      Wooa, that´s a cool idea

    • @helloworld-sq5ce
      @helloworld-sq5ce 5 лет назад

      @@TheCodingTrain after 4 hours of debugging and reading Docs .i just realized that i could just use , thanks God that i decided to read the comments lol.

    • @arielbustamante
      @arielbustamante 4 года назад

      I'm struggling with instance mode also, any simple examples of how to use more than one ?

    • @iamlightleak
      @iamlightleak 3 года назад

      @@TheCodingTrain I'm doing this right now. My problem here: The d sketch does not work behind HTML text. why?

  • @williambluish
    @williambluish 6 лет назад

    Why is it called p5.js? :O
    (edit) Oh, I finished watching the video. Now I understand.

  • @taihatranduc8613
    @taihatranduc8613 4 года назад

    you're so funny, bro

  • @vinay7397
    @vinay7397 Год назад

    I know this from another source, but you are really confusing, you babble a lot. I know I am being mean, but it's true.