I have done
npm install p5 --save
I edited the .angular.cli.json
"scripts": [
"../node_modules/p5/lib/p5.min.js",
"../node_modules/p5/lib/addons/p5.sound.js",
"../node_modules/p5/lib/addons/p5.dom.js"
]
and imported as
import * as p5 from 'p5';
in app.component.ts file
but now where to add the function setup() and function draw() . I have tried adding it directly but it doesn't work
The p5. js is a JavaScript library for creative coding. A collection of pre-written code, it provides us with tools that simplify the process of creating interactive visuals with code in the web browser.
With p5. js, you can build visualizations using shapes, text, images, videos, and sounds. You can add sophisticated animations and interactions to your programs. And it's easy to get started, even with only a basic knowledge of web programming languages like HTML, CSS, and JavaScript.
p5. js has an impressive amount of functionality, history and community behind it to make it a valuable learning investment if you ever wanted to create art, design, motion or interactive pieces using code. A p5. js program can be anywhere from a few lines of code to thousands.
p5. js is a library that starts with the original goal of Processing –to make to make coding accessible for artists, designers, educators, and beginners– and reinterprets it for the web using the metaphor of a software sketchbook with a set of drawing functionality. With p5. js we are able to draw in the canvas.
That's the way you import it that causes the problem. You should import it by typing:
import 'p5';
Then declare a variable:
private p5;
You should now be able to do something like this:
ngOnInit() {
this.createCanvas();
}
private createCanvas() {
this.p5 = new p5(this.sketch);
}
private sketch(p: any) {
p.setup = () => {
p.createCanvas(700, 600);
};
p.draw = () => {
p.background(255);
p.fill(0);
p.rect(p.width / 2, p.height / 2, 50, 50);
};
}
Here's my ng version :
Angular CLI: 1.7.4
Node: 9.11.1
OS: win32 x64
Angular: 5.2.11
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.7.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0
and last version of p5 in package.json:
"p5": "^0.6.1"
Everything's working fine for me.
Hope it will help.
Update: also works for Angular CLI 7.1.0
Angular CLI: 6.2.3
Node: 10.9.0
package.json:
"p5": "^0.7.2"
import libraries
import * as p5 from 'p5';
import "p5/lib/addons/p5.sound";
import "p5/lib/addons/p5.dom";
p5 setup
ngOnInit() {
const sketch = (s) => {
s.preload = () => {
// preload code
}
s.setup = () => {
s.createCanvas(400, 400);
};
s.draw = () => {
s.background(255);
s.rect(100, 100, 100, 100);
};
}
let canvas = new p5(sketch);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With