Git Product home page Git Product logo

p5-typescript-starter's Introduction

P5 TypeScript Starter

This project will quickly get you something working in p5.js and typescript.

Demo

Click here for Demo

Demo

This demo is based on the Regular Polygon sketch available in the p5js examples.

Getting Started

Installing

git clone https://github.com/Gaweph/p5-typescript-starter.git
npm install

Using

npm start

A local version will now be running on localhost:3000.

Advanced

Global and Instanced Modes

P5 is able to run in either global or instance mode.

This starter project uses global mode by default to bring it in line with most of the online resources provided by the project.

As stated on the P5 wiki:

While this is convenient (and friendlier) it's important to note that this can lead to problems and confusion down the road when mixing other JS libraries or trying to embed multiple p5 sketches on the same page. A safer, more advanced methodology is to create a p5 sketch as an object "instance".

The following examples are both functionally the same.

Global Mode

let x = 100;
let y = 100;

function setup() {
  createCanvas(windowWidth, windowHeight);
}

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

Instanced Mode

var sketch = (p: p5) => {
  this.x = 100;
  this.y = 100;
  p.setup = () => {
    p.createCanvas(p.windowWidth, p.windowHeight);
  };

  p.draw = () => {
    p.background(0);
    p.fill(255);
    p.rect(this.x, this.y, 50, 50);
  };
};

new p5(sketch);

This starter project will work with either mode, feel free to experiment with both.

Using External Libraries

To use an external library, e.g. qrcode-generator.

  1. Install the library with npm install --save-dev qrcode-generator.

  2. Add a script tag to your index.html.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.4/qrcode.min.js"></script>
  3. Import via global.d.ts.

    import qrcode = require('qrcode-generator');
  4. Use in sketch.ts.

    var qr = qrcode(4, 'L');
    qr.addData('https://github.com/Gaweph/p5-typescript-starter');
    qr.make();
    
    text(qr.createASCII(), 1, 1);

See dblock/p5qr for a working sample.

Copyright and License

MIT License, see LICENSE for details.

p5-typescript-starter's People

Contributors

dblock avatar dependabot[bot] avatar gaweph avatar grantralls avatar ikesau avatar julesfouchy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

p5-typescript-starter's Issues

How to deploy it to a server?

First of all, thank you for the template project, I have written a game based on this project, but how can I deploy this code to my Nginx? Or to github page like you did?

Can't access p5.dom methods

When I try to reference any p5.dom methods, such as .parent(), I get the error:
sketch/sketch.ts(125,14): error TS2339: Property 'parent' does not exist on type 'Element'.

When I look in the p5.d.ts file I can see it's there.
Is there anything else I need to add?

Routing to mulitple Sketches

Thanks for making this boileplate, it has helped me heaps in my p5 learning journey already.
How would you add URL routing such that multiple sketches can be developed in this repository? It's a common use case to work on different independent sketches and I'd like to take advantage of all the nice features such as typescript and automatic browser reloads that this setup has to offer.

I want to be able to work on different sketches and access them using URls like:

http://localhost:3000/sketch1
http://localhost:3000/sketch2

Ideally, the compiler should exclude files that are not relevant to the requested sketch to keep compile and load times fast.

Require is not defined

How do I use it with imports?
If I try to use:

import Shapes = require('./shapes');
import p5 = require("p5");

or:

import Shapes from "./shapes";
import p5 = require("p5");

I get

Uncaught ReferenceError: define is not defined
    at build.js:1

I tried setting "module": "amd" in tsconfig.json, but no luck.

createSlider seems to be undefined

createSlider throws an Uncaught ReferenceError: createSlider is not defined

e.g.

function setup() {
  createCanvas(windowWidth, windowHeight)
}

function draw() {
  rect(width/2 - 40 , height/2 - 30, 80, 60)
}

let x = createSlider(0, 1, 10)

Processing KEY constants are missing

P5JS's LEFT_ARROW, RIGHT_ARROW, etc. are missing from the types. The code does compile but it would be better, if we didn't have to look at the errors.

SourceMap directory is incorrect

By the tsc config, ts expects the source maps to be in sketch/sketch/build.js.map. The source maps are actually generated into build/build.js.map. I was able to fix the issue in my own project by changing the map root setting in tsconfig to ../build.

You can confirm the issue by walking through the following process...

  1. clone a clean copy of the repo.
  2. npm i
  3. npm run start-compile
  4. check the last line in build/build.js

werid typescript error

related: microsoft/TypeScript#35021

right away ran into this error after npm install & npm start:

node_modules/typescript/lib/tsc.js:10248
        return !!(module.flags & 1024);
                         ^

TypeError: Cannot read property 'flags' of undefined

Import destroys build

I don't know if this fits here, if not I'm sorry.

When I import a custom file at the top of my sketch.ts I get the error "error TS2686: 'p5' refers to a UMD global, but the current file is a module. Consider adding an import instead."
Why does this happen just because I add an import?
This is my sketch.ts:

import Carcasonne from './game'

const carcasonne = new Carcasonne()

var sketch = (p: p5) => {
 p.setup = () => {
   p.createCanvas(p.windowWidth, p.windowHeight);
 }

 p.draw = () => {
   p.background(0);
   p.fill(255);
   carcasonne.draw()
 }
}

new p5(sketch);

function windowResized() {
 createCanvas(windowWidth, windowHeight);
}

The rest is the same.

How to use clmtrackr

I am having trouble using clmtrackr (https://github.com/auduno/clmtrackr) with the template. clm is undefined in the browser no matter what approach I use for import. The library works perfectly fine with public javascript p5 examples.

Using the same way as for qrcode library does not work (import clm = require('clmtrackr')). I would be very grateful for any help. My package.json:

"dependencies": {
"clmtrackr": "^1.1.2",
"qrcode-generator": "^1.4.4"
},
"devDependencies": {
"@types/p5": "^1.3.3",
"@types/clmtrackr": "^1.1.1",
"browser-sync": "^2.26.12",
"npm-run-all": "^4.1.5",
"typescript": "^3.8.3"
}

How to use sound

It is very nice work, I am just struggling to use sound via loadSound. I am unable to get it working with the starter. Any idea, please ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.