我在这里有一个问题,我想为我尝试一些关于html5 javascript代码的新内容。关于如何创建从另一个函数调用参数的函数。我想调用参数" circle"来自function try(){,在不同位置的画布上绘制另一个圆圈
这是我的逻辑:
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();
帮助并教我解决方案和一些例子:)
答案 0 :(得分:1)
只需将圆值存储在全局变量中。
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;
}