We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Folks:
Have a look at the following code: var element
function drawarectangle(x, y, w, h){
rect(x, y, w, h);
}
function setup() {
createCanvas(500, 500);
background(0);
element = createGraphics(400,400);
element.background(100);
element.fill(255);
element.drawarectangle(100,100,200,200);
}
function draw() {
image(element, 50, 50);
}
ok. so basically i want to call a function inside a Graphics Object. For the sake of the easiness I wrote a completely useless function call "drawarectagle" and I want to call this function inside the graphics object "element". How can I do this, because I cant just say element.drawarectangle(100,100,200,200);
?
Answers
I suppose you could do this:
element.drawarectangle(100,100,200,200);
But that feels a little hacky. Why don't you just change your
drawarectangle()
function to take anelement
argument?https://OpenProcessing.org/sketch/528626
But... That works for the madeup function i created, but not for all functions. or am i misstaken?
Who are you asking that question?
And what happened when you tried?
We can only give out examples based on what you had already told us. :-@
If your needs are more complex, you should provide/post more detailed info about them. :-\"
The
function
above is merely a very basic template on how to add methods to both p5.js' p5 & p5.Graphics classes via JS' prototype inheritance chain: :ar!In your given example, drawRectangle() merely acts as an alias to p5::rect().
That's why just assigning rect() to drawRectangle() works as well: :-j
p5.prototype.drawRectangle = p5.prototype.rect;
But if you've got more complex stuff to add to p5.js' API in mind, you should consider creating your own separate 3rd-party library for it. *-:)
Ok. so here is one of the functions i want to draw in the graphics element:
So it creates a vertical slider. I looked at the links, but I can't really see helpful tips that I understand X_X
Some notes about naming convention for both Java & JS: L-)
Even though you could force your function to become a method for p5.js' p5 & p5.Graphics classes:
p5.prototype.vSlider = function (length, max, value, active) {};
Plus prefixing all the p5.js API used there w/
this.
:For example,
textSize(20);
becomesthis.textSize(20);
And
mouseIsPressed
becomesthis.mouseIsPressed
, and so on.Your function is completely outta place as a member of p5.js API for various reasons: :-O
Instead create a
class
as a proper place to store & manage those 2 states w/ methods: *-:)https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
BtW, haven't I already told you can simply CSS "rotate" any p5.Element via its method style(): :-\"
https://Forum.Processing.org/two/discussion/25855/vertical-slider#Item_1
createSlider(0, 0xff, 0o200).style('rotate', -90);
Ok. That answers my question. I wanted to transform the vSlider functions anyways and i am going to do it now. but why?
Why is bad if I change fill,stroke,font sizes etc.?
Why is it bad if my function checks mouse position?
Why is returning an object bad?
And why should i follow lowerCamelCase?
But otherwise: Thank you!
Yeah. But why not just create my own funtion? Just because I can :-\"
You're mixing up functions, methods & API as if those 3 terms were the same thing development-wise.
You've asked about turning your custom functions into methods of p5.js' API.
However, it turns out your function acts as a whole GUI slider "library", completely at odds w/the rest of p5.js' API: https://p5js.org/reference/
Your vertical slider function even got 2 states which it returns & expects them to be re-passed again when it is re-invoked! @-)
And your function doesn't respect & even mutates canvas' current attributes like fill, stroke, etc.
Therefore, even though you can hack your way for it, your function is completely unfit as a p5.js API method, in comparison to the type of features p5.js API offers! 8-X
As I had already advised you, you should instead create a class to store those 2 state values and make your function a method of that class. :-B
Do you mean something like this?
and then u can activate it with this: