Robin Minervini
Robin Minervini

Reputation: 38

p5.js | Syntaxe issue

I made a processing animation, but I don't know how to convert a part of this into Javascript (i'm using p5).

Here is my actual code :

var NUM_LINES = 10;
var j;
var t;

function setup() {
    createCanvas(200,200);
    background(20);
}

function draw() {
    background(255);
    strokeWeight(1);

    translate(width/2, height/2);

    for (var i = 0; i < NUM_LINES; i++) {
        stroke(60, 60, 60);
        line(x1(t + i), y1(t + i), x2(t + i), y2(t + i));
    }

    t += 0.5;
}

float x1(float t){
    return sin(t / 10) * 35 + sin(t / 5) * 20;
}

float y1(float t){
    return cos(t / 10) * 35;
}

float x2(float t){
    return sin(t / 10) * 70 + sin(t) * 2;
}

float y2(float t){
    return cos(t / 20) * 70 + cos(t / 12) * 20 ;
}
}

Do you know how to convert this code below into javascript ?

float x1(float t){
    return sin(t / 10) * 35 + sin(t / 5) * 20;
}

Upvotes: 0

Views: 48

Answers (2)

Cobain Ambrose
Cobain Ambrose

Reputation: 91

Just change the 'float' to 'function':

function x1 (t) {
    return sin(t / 10) * 35 + sin(t / 5) * 20;
}

Hope this helps!

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42176

You don't convert code by just translating it line by line. You translate code by understanding exactly what it's doing, and then figuring out how to do that in your target language.

But just looking at your function:

float x1(float t){
    return sin(t / 10) * 35 + sin(t / 5) * 20;
}

This defines a function named x1 that takes a parameter named t and then does some stuff that should be pretty much the same in every language. So it might look like this:

function x1(t){
    return sin(t / 10) * 35 + sin(t / 5) * 20;
}

Note that there are multiple ways to do this, so you could also do something like this:

var x1 = function(t){
    return sin(t / 10) * 35 + sin(t / 5) * 20;
}

You really should take a step back and learn some JavaScript basics instead of trying to translate things one line at a time.

Upvotes: 1

Related Questions