{"id":5497,"date":"2020-01-19T13:07:30","date_gmt":"2020-01-19T12:07:30","guid":{"rendered":"http:\/\/blog.via-internet.de\/?p=5497"},"modified":"2023-04-30T17:57:37","modified_gmt":"2023-04-30T15:57:37","slug":"working-with-p5-js","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2020\/01\/19\/working-with-p5-js\/","title":{"rendered":"Ionic | Working with p5.js"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/p5js.org\">p5.js<\/a> is a JavaScript library for creative coding, with a focus on making coding accessible and inclusive for artists, designers, educators, beginners, and anyone else.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this Post, you will learn how to create an ionic app using <a href=\"https:\/\/p5js.org\/\">p5.js<\/a> to create an amazing demo of the Lissajous Curve (<a href=\"https:\/\/de.wikipedia.org\/wiki\/Lissajous-Figur\">de<\/a>\/<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lissajous_curve\">en<\/a>).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1934\" height=\"610\" src=\"https:\/\/i1.wp.com\/blog.via-internet.de\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.00.21.png?fit=700%2C221&amp;ssl=1\" alt=\"\" class=\"wp-image-5499\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.00.21.png 1934w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.00.21-300x95.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.00.21-1024x323.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.00.21-768x242.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.00.21-1536x484.png 1536w\" sizes=\"auto, (max-width: 1934px) 100vw, 1934px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to learn more about this amazing graphs, take a look at some examples at <a href=\"https:\/\/codepen.io\/tag\/lissajous\/\">codepen.io.<\/a> Some amazing examples are <a href=\"https:\/\/codepen.io\/Alca\/pen\/oPPbxN\">here<\/a>, <a href=\"https:\/\/codepen.io\/lpato\/pen\/KGebJa\">here<\/a> and <a href=\"https:\/\/codepen.io\/hanumaukkadapu\/pen\/MWWqgaY\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As always, you will find the final project on <a href=\"https:\/\/github.com\/ionic-toolbox\/Working-with_p5.js\">Github<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You will need to install Node.Js, Ionic and p5.js.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First step is, to install Node.Js. I will refer to the installation pages on <a href=\"https:\/\/nodejs.org\/\">Node JS.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After installing Node.js, you will also have the Node Package manager, npm. We will use this tool to install the other requirements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Install Ionic<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ npm -g install ionic<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create base app structure<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ ionic start working-with-p5js blank --type angular\n$ cd working-with-p5js<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add p5.js to the ionic app<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ npm install --save-dev p5<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Test your base app<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ ionic serve<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"2134\" height=\"1632\" src=\"https:\/\/i0.wp.com\/blog.via-internet.de\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35.png?fit=700%2C535&amp;ssl=1\" alt=\"\" class=\"wp-image-5504\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35.png 2134w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35-300x229.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35-1024x783.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35-768x587.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35-1536x1175.png 1536w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.15.35-2048x1566.png 2048w\" sizes=\"auto, (max-width: 2134px) 100vw, 2134px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Starting with p5.js<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the base components of p5.js to our app.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Modify home.page.html<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"8\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;ion-header>\n\t&lt;ion-toolbar>\n\t\t&lt;ion-title>Working with p5.js&lt;\/ion-title>\n\t&lt;\/ion-toolbar>\n&lt;\/ion-header>\n\n&lt;ion-content>\n\t&lt;div id=\"canvasContainer\" class=\"canvas-container\">&lt;\/div>\n&lt;\/ion-content><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Modify home.page.ts<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Component, OnInit, ElementRef } from '@angular\/core';\n\nimport * as p5 from 'p5';\n\n@Component({\n\tselector: 'app-home',\n\ttemplateUrl: 'home.page.html',\n\tstyleUrls: [ 'home.page.scss' ],\n})\nexport class HomePage implements OnInit {\n\n  curve: any;\n  canvasSizeX = 200;\n  canvasSizeY = 200;\n\n  constructor(private el: ElementRef) { }\n\n  ngOnInit() {\n    const p5obj = new p5(p => {\n      p.setup = () => { this.setup(p); };\n      p.draw = () => { this.draw(p); };\n    }, this.el.nativeElement);\n  }\n\n  setup(p) {\n    const c = document.querySelector('#canvasContainer');\n    p\n      .createCanvas(this.canvasSizeX, this.canvasSizeY)\n      .parent(c);\n  }\n\n  draw(p) {\n    p.background(220);\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And the result ist your frist p5.js graphic:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"2134\" height=\"1632\" src=\"https:\/\/i0.wp.com\/blog.via-internet.de\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46.png?fit=700%2C535&amp;ssl=1\" alt=\"\" class=\"wp-image-5505\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46.png 2134w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46-300x229.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46-1024x783.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46-768x587.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46-1536x1175.png 1536w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-16.25.46-2048x1566.png 2048w\" sizes=\"auto, (max-width: 2134px) 100vw, 2134px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we will grab an example from p5.js: <a href=\"https:\/\/p5js.org\/examples\/form-regular-polygon.html\">form-regular-polygon<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Modify home.page.ts<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We modify the draw function and add a required polygon function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Component, OnInit, ElementRef } from '@angular\/core';\n\nimport * as p5 from 'p5';\n\n@Component({\n  selector: 'app-home',\n  templateUrl: 'home.page.html',\n  styleUrls: ['home.page.scss'],\n})\nexport class HomePage implements OnInit {\n\n  curve: any;\n  canvasSizeX = 720;\n  canvasSizeY = 400;\n\n  private ID = 'HomePage';\n  log(func, line = '') {\n    console.log(this.ID + '::' + func + '|' + line);\n  }\n\n  constructor(\n    private el: ElementRef\n  ) {\n    this.log('constructor');\n  }\n\n  ngOnInit() {\n    this.log('ngOnInit');\n\n    const p5obj = new p5(p => {\n      p.setup = () => {\n        this.setup(p);\n      };\n\n      p.draw = () => {\n        this.draw(p);\n      };\n    }, this.el.nativeElement);\n  }\n\n  setup(p) {\n    this.log('setup');\n\n    const c = document.querySelector('#canvasContainer');\n    p\n      .createCanvas(this.canvasSizeX, this.canvasSizeY)\n      .parent(c);\n  }\n\n  polygon(p, x, y, radius, npoints, color) {\n    const angle = p.TWO_PI \/ npoints;\n    p.beginShape();\n\n    p.fill(color);\n    for (let a = 0; a &lt; p.TWO_PI; a += angle) {\n      const sx = x + Math.cos(a) * radius;\n      const sy = y + Math.sin(a) * radius;\n      p.vertex(sx, sy);\n    }\n    p.endShape(p.CLOSE);\n  }\n\n  draw_figure(p, scaleX, scaleY, divisor, radius, npoints, color) {\n    p.push();\n    p.translate(this.canvasSizeX * scaleX, this.canvasSizeY * scaleY);\n    p.rotate(p.frameCount \/ divisor);\n    this.polygon(p, 0, 0, radius, npoints, color);\n    p.pop();\n  }\n\n  draw(p) {\n    p.\n      background('white');\n\n    this.draw_figure(p, 0.2, 0.5, 200.0, 82, 3, 'red');\n    this.draw_figure(p, 0.5, 0.5, 50.0, 80, 20, 'blue');\n    this.draw_figure(p, 0.8, 0.5, -100.0, 70, 7, 'green');\n  }\n}\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And we get a nice litte animation:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1800\" height=\"1398\" src=\"https:\/\/i1.wp.com\/blog.via-internet.de\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-21.05.44.png?fit=700%2C544&amp;ssl=1\" alt=\"\" class=\"wp-image-5510\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-21.05.44.png 1800w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-21.05.44-300x233.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-21.05.44-1024x795.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-21.05.44-768x596.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/Bildschirmfoto-2020-01-14-um-21.05.44-1536x1193.png 1536w\" sizes=\"auto, (max-width: 1800px) 100vw, 1800px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Resources and Reading<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Websites<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/codepen.io\/\">CodePen<\/a>&nbsp;(with a Processing.js <a href=\"http:\/\/codepen.io\/pen?template=LRzErQ\">template<\/a>)<\/li><li><a href=\"http:\/\/www.openprocessing.org\/\">OpenProcessing<\/a><\/li><li><a href=\"http:\/\/sketchpad.cc\/\">Sketchpad<\/a><\/li><li><a href=\"http:\/\/sketchpatch.net\/\">sketchPatch<\/a><\/li><li><a href=\"http:\/\/gamejolt.com\/\">Game Jolt<\/a><\/li><li><a href=\"https:\/\/itch.io\/\">itch.io<\/a><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction p5.js is a JavaScript library for creative coding, with a focus on making coding accessible and inclusive for artists, designers, educators, beginners, and anyone else. In this Post, you will learn how to create an ionic app using p5.js to create an amazing demo of the Lissajous Curve (de\/en). If you want to learn more about this amazing graphs, take a look at some examples at codepen.io. Some amazing examples are here, here and here. As always, you will find the final project on Github. Preparation You will need to install Node.Js, Ionic and p5.js. First step is, to install [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5712,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[90,100],"tags":[],"class_list":["post-5497","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-p5-js"],"jetpack_featured_media_url":"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/p5js-6.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/5497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=5497"}],"version-history":[{"count":1,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/5497\/revisions"}],"predecessor-version":[{"id":9495,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/5497\/revisions\/9495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media\/5712"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=5497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=5497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=5497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}