gpt4 book ai didi

javascript - 如何从数组存储访问 JS 函数?

转载 作者:行者123 更新时间:2023-11-30 08:34:55 25 4
gpt4 key购买 nike

我正在 html5 Canvas 中使用自定义“按钮”。因为我有很多按钮,所以我认为将它们存储在数组中更有意义。我的困境是,我不太确定如何实现“附加”到特定按钮的自定义功能。我看过this posting ,不确定这是否适用于此。

很明显 btn[i].function+"()"; 没有削减它。

Here's a JSFiddle.

如何在按钮数组中存储自定义函数,并在鼠标点击时成功调用它?

代码如下:

<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body {background-color: black; margin: 8px; }
#canvas {border: 1px solid gray;}
</style>
<script>
$(function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();
var cw = canvas.width;
var ch = canvas.height;

var btn = [{
x: 50,
y: 100,
width: 80,
height: 50,
display: "Hello There",
function: "functionA" // Is this right?
}, {
x: 150,
y: 100,
width: 80,
height: 50,
display: "Why Not?",
function: "functionB" // Is this right?
}, {
x: 250,
y: 100,
width: 80,
height: 50,
display: "Let's Go!",
function: "functionC" // Is this right?
}];

function drawAll() {
ctx.clearRect(0, 0, cw, ch);
for (var i = 0; i < btn.length; i++) {
drawButton(ctx, btn[i].x, btn[i].y, btn[i].width, btn[i].height, btn[i].display);
}
}

drawAll();

// listen for mouse events
$("#canvas").mousedown(function(e) {
handleMouseDown(e);
});

function handleMouseDown(e) {
// tell the browser we'll handle this event
e.preventDefault();
e.stopPropagation();

// save the mouse position
lastX = parseInt(e.clientX - offsetX);
lastY = parseInt(e.clientY - offsetY);

// hit all existing buttons
for (var i = 0; i < btn.length; i++) {
if ((lastX < (btn[i].x + btn[i].width)) &&
(lastX > btn[i].x) &&
(lastY < (btn[i].y + btn[i].height)) &&
(lastY > btn[i].y)) {
// execute button function
btn[i].function+"()"; // obviously this is just wrong.
console.log("Button #" + (i + 1) + " has been clicked!!" );
}
}
}

function drawButton(context, x, y, width, height, text) {
var myGradient = context.createLinearGradient(0, y, 0, y + height);
myGradient.addColorStop(0, '#999999');
myGradient.addColorStop(1, '#222222');
context.fillStyle = myGradient;
context.fillRect(x, y, width, height);
context.fillStyle = 'white';
// textAlign aligns text horizontally relative to placement
context.textAlign = 'center';
// textBaseline aligns text vertically relative to font style
context.textBaseline = 'middle';
context.font = 'bold 15px sans-serif';
context.fillText(text, x + width / 2, y + height / 2);
}

function functionA() {
alert("Yipee Function A!");
}

function functionB() {
alert("Yowza, it's Function B!");
}

function functionC() {
alert("Now showing Function C!");
}
})
</script>
</head>

<body>
<canvas id="canvas" width=400 height=300></canvas>
</body>
</html>

最佳答案

尝试将按钮更改为:

{
display: "Hello There",
action: functionA
}

并调用:

btn[i].action();

我将名称function 更改为action,因为function 是一个保留字,不能用作对象属性名称。

关于javascript - 如何从数组存储访问 JS 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32406680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com