user2207
user2207

Reputation: 19

Create a new function and call parameter from another function

I have a question here, I want to try something new for me about html5 javascript code. About how to create a function that call parameter from another function. I want to call parameter "circle" from function try() {, to draw another circle in canvas with different position

Here's my logic:

var canvas = ...;
var ctx = ....;    
x = 0;
y = 0;

function try(circle,rect) {
  (...)
  circle[0] = .....; //x coordinates
  circle[1] = .....; //y coordinates

  rect[0] = .....;
  rect[1] = .....;

  ctx.arc(circle[0].circle[1],circle[2],0,Math.PI*2);
  ctx.fill();

  ctx.fillRect(0, 0, rect[0], rect[1]);
}

try([x,y,30], [x,y]);

function callParam() {
//Here i want to call parameter "circle", 
//to draw another circle in canvas with different position

//can i put a code like this? :
anotherCircleX = try.circle[0] +200;
anotherCircley = try.circle[1] + 100;
}
callParam();

Help and teach me with solution and some example :)

Upvotes: 0

Views: 40

Answers (1)

brumbo
brumbo

Reputation: 33

Just store the circle value in global variable.

var circle_latest = [];
...
function try(circle, rect){
(...)
    circle[0] = .....; //x coordinates
    circle[1] = .....; //y coordinates
    circle_latest = circle;
(...)
}

function callParam() {
    anotherCircleX = circle_latest[0] +200;
    anotherCircley = circle_latest[1] + 100;
}

Upvotes: 1

Related Questions