简体   繁体   English

JavaScript函数显然没有被调用

[英]JavaScript function apparently not being called

I have a web page with an HTML5 canvas that is displaying a number of images. 我有一个带有HTML5画布的网页,其中显示了许多图像。 I have made it possible to drag and drop the images around the canvas by using the kineticJS library. 通过使用dynamicJS库,可以将图像拖放到画布周围。

However, I am using a copy of the library that I have downloaded and saved locally, since there were one or two changes that I wanted to make to its functionality. 但是,由于要对它的功能进行一两个更改,因此我使用的是我在本地下载并保存的库的副本。 (For example, before I had altered it, when a click was detected on an image on the canvas, anything that had previously been drawn to the canvas by anything other than the Kinetic library would be cleared from the canvas. I removed this call to clear(), since there were a few other things that I had drawn to the canvas which I wanted to remain there.) (例如,在我更改它之前,当在画布上的图像上检测到单击时,将从Kinetic库以外的任何其他东西绘制到画布上的东西都将从画布上清除。我删除了对此调用clear(),因为我还想在画布上绘制其他一些东西,所以我想留在那里。)

Now, I want to add some functionality to my code, so that when a click is detected on one of the images that has been drawn to the canvas, a textual description of the image that has been clicked will appear on the the web page outside the canvas. 现在,我想在代码中添加一些功能,以便在检测到已绘制到画布上的图像之一上单击时,已单击图像的文字描述将显示在外部网页上。画布。

I have the following function, which I am using to detect whether a click has been detected on one of the images: 我具有以下功能,用于检测是否在其中一张图像上检测到单击:

function isClickOnImage(event){
    var clickX = event.clientX;
    var clickY = event.clientY;

    //var imageCheckIteration = 0;
    while(imageCheckIteration < sources.length){
        if((clickX > sources[imageCheckIteration].x && clickX < sources[imageCheckIteration].x + imageWidth) &&
        (clickY > sources[imageCheckIteration].y && clickY < sources[imageCheckIteration].y + imageHeight)){
            /*This is where I need to print the variable that holds the text I want to display, but I need to display its contents
            outside the canvas, in the <p></p> tags below. */
            console.log("Click on image detected");
            document.getElementById("tipsParagraph").innerHTML = tips[imageCheckIteration];
        } else {
            document.getElementById("tipsParagraph").innerHTML = "";
        }
    }
}

The images displayed on the canvas are all stored in an array called 'sources'. 画布上显示的图像全部存储在名为“源”的数组中。 So, the while loop in this section of my code should check whether the x & y coordinates of the click were in a location that has an image drawn to it, starting with the first image in the array, and going all the way through to the last. 因此,我的代码这一部分的while循环应检查单击的x和y坐标是否位于绘制有图像的位置,从数组中的第一个图像开始,一直到最后。

If the loop determines that a click was in a position that has an image drawn to it, then the console should log the message "Click on image detected", and the value of my tipsParagraph variable should be updated to the value of whatever is stored in the 'tips' array, at the same position as that of the image that's been clicked on in the images array (the images array has been called 'sources'). 如果循环确定单击位于绘制有图像的位置,则控制台应记录以下消息:“单击检测到的图像”,并且我的tipsParagraph变量的值应更新为存储的值在“提示”数组中,与在images数组中被单击的图像的位置相同(images数组被称为“ sources”)。 (The text stored in each position of the 'tips' array corresponds to the image at the same position in the 'sources' array). (存储在“提示”数组每个位置的文本与“源”数组中相同位置的图像相对应)。

As I said previously, I am using a local copy of the KineticJS library, and I've edited its 'mousedown' function so that it now looks like this: 就像我之前说的,我正在使用KineticJS库的本地副本,并且我已经对其“ mousedown”函数进行了编辑,因此现在看起来像这样:

_mousedown: function(evt) {
    this._setUserPosition(evt);
    var obj = this.getIntersection(this.getUserPosition());
    if(obj && obj.shape) {
        var shape = obj.shape;
        this.clickStart = true;
        shape._handleEvent('mousedown', evt);
        isClickOnImage(evt);
    }

    //init stage drag and drop
    if(Kinetic.DD && this.attrs.draggable) {
        this._initDrag();
    }
}

ie I've added a call to my isClickOnImage function. 即我已经添加了对我的isClickOnImage函数的调用。

However, when I click on an image on the canvas, the text in the 'tipsParagraph' section of my page is not updated. 但是,当我单击画布上的图像时,页面的“ tipsParagraph”部分中的文本不会更新。 The 'tipsParagraph' is simply the ID I've given to a <p> tag displayed below the canvas. “ tipsParagraph”只是我给显示在画布下方的<p>标记的ID。

This is the line of code I have for the 'tipsParagraph': 这是我为“ tipsParagraph”编写的代码行:

<p id = "tipsParagraph">This is where the text will be displayed. <script>$('#tipsParagraph').text(tips[imageCheckIteration]);</script></p>

I assumed that the JS I've added to this line would ensure that the text displayed is the text from the tips array, at whatever position the value of the variable imageCheckIteration determines. 我假设我添加到此行的JS将确保显示的文本是tips数组中的文本,无论变量imageCheckIteration的值确定在什么位置。 However, for some reason, the tip being displayed on my page is always the first element from the tips array. 但是,由于某些原因,在我的页面上显示的提示始终是tips数组中的第一个元素。 This is the element displayed as soon as the page loads (ie before a click on an image), and it does not change when a click on an image is detected. 这是元素在页面加载后立即显示(即在单击图像之前),并且当检测到单击图像时它不会更改。 So it seems to me that my isClickOnImage function is never being called by the mousedown function in the library, but rather that the value of the tipsParagraph is being set straight away when the page loads, and not being changed after that, regardless of user interaction with the page... 因此在我看来,库中的mousedown函数从未调用过isClickOnImage函数,而是在页面加载时直接设置了tipsParagraph的值,此后无论用户进行任何交互都不会对其进行更改与页面...

Can anyone point out to my why this is? 谁能指出我的原因? What is it I'm doing wrong, and what do I need to do to get the text displayed to be the text corresponding to the image that the user clicks on? 我在做什么错了,我需要怎么做才能使显示的文本成为与用户单击的图像相对应的文本?

Taking a look at 看看

function isClickOnImage(event){
 var clickX = event.clientX;
 var clickY = event.clientY;

 //var imageCheckIteration = 0;  // <-- why is this 0 commented out?
 while(imageCheckIteration < sources.length){ // <-- imageCheckIteration is never incremented, so it never increases, hence the check never occurs
    if((clickX > sources[imageCheckIteration].x && clickX < sources[imageCheckIteration].x + imageWidth) &&
    (clickY > sources[imageCheckIteration].y && clickY < sources[imageCheckIteration].y + imageHeight)){
        /*This is where I need to print the variable that holds the text I want to display, but I need to display its contents
        outside the canvas, in the <p></p> tags below. */
        console.log("Click on image detected");
        document.getElementById("tipsParagraph").innerHTML = tips[imageCheckIteration];
    } else {
        document.getElementById("tipsParagraph").innerHTML = "";
    }
 }
}

try something like: the for loop structure to control incrementing: 尝试类似:for循环结构来控制增量:

for( var i=0; i<sources.length; i++){ // this will go through all items in the sources array
    if((clickX > sources[i].x ... ) //and other checks
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM