p5.js integration with Angular / not using global functions

江枫思渺然 提交于 2019-12-11 15:12:28

问题


In tutorials & youtube channel all examples use sketch.js & global functions & canvas / video tags are automatically created by library. Is there a way to manually create "p5.js context", pass it video & canvas tag and use API on some global object? I would like to write my p5.js code inside my components


回答1:


I don't really know anything about Angular, but it sounds like you're looking for instance mode in P5.js.

See here for more info, but it looks like this:

var s = function( sketch ) {

  var x = 100; 
  var y = 100;

  sketch.setup = function() {
    sketch.createCanvas(200, 200);
  };

  sketch.draw = function() {
    sketch.background(0);
    sketch.fill(255);
    sketch.rect(x,y,50,50);
  };
};

var myp5 = new p5(s);

You might have to play around a little to pass it external objects, but those are the basics.



来源:https://stackoverflow.com/questions/48522496/p5-js-integration-with-angular-not-using-global-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!