简体   繁体   English

单击或悬停在画布中的多个图像上?

[英]Clicking or hovering over multiple images in canvas?

I am working on a project for my programming class. 我正在为我的编程课开发一个项目。 I'd like to essentially have a canvas with a background element (say a room.jpg) and then maybe three interactive objects in the room (lamp.jpg, couch.jpg, desk.jpg). 我实际上想要一个带有背景元素的画布(例如,room.jpg),然后在房间中设置三个交互对象(lamp.jpg,couch.jpg,desk.jpg)。 I'd like for it to be that if you hover over the lamp a small box or text pops out, giving you some information. 我希望它是,如果您将鼠标悬停在灯上,则会弹出一个小框或文本,为您提供一些信息。 Or maybe have it so if you click an image, the same concept happens. 也许有,所以如果您单击图像,也会发生相同的概念。 You know, something interactive with the objects in the canvas. 您知道,有些东西与画布中的对象互动。 Again, I'm new to canvas but we have to use it in our assignment. 同样,我是画布的新手,但我们必须在作业中使用它。 My current code is: 我当前的代码是:

function loadImages(sources, callback) {
            var images = {};
            var loadedImages = 0;
            var numImages = 0;
            // get num of sources
            for(var src in sources) {
                numImages++;
                }
            for(var src in sources) {
                images[src] = new Image();
                images[src].onload = function() {
                if(++loadedImages >= numImages) {
                callback(images);
                    }
                };
            images[src].src = sources[src];
                }
            }

            var sources = {
            room: 'room.jpg',
            title: 'title.jpg'
            };

            loadImages(sources, function(images) {
            context.drawImage(images.room, 0,0);
            context.drawImage(images.title, 0,0);
            });
            }

But from what I understand, it makes the two jpegs a "permanent" canvas element (unable to be messed with). 但是据我了解,它使两个jpeg成为“永久”画布元素(无法弄乱)。 I had been trying to get it so that when I clicked I'd go from the title.jpg to the room.jpg but I've since given up. 我一直在尝试获取它,以便当我单击时可以从title.jpg转到room.jpg,但此后我就放弃了。 Essentially, all I want now is just to have the room.jpg appear when the page is first loaded, and have three other png objects on top (as objects in the room). 本质上,我现在想要的只是在第一次加载页面时显示room.jpg,并在顶部放置其他三个png对象(作为房间中的对象)。 Are these able to be interacted with, or do I have to put the images into the canvas in another way? 这些可以交互吗?还是必须以其他方式将图像放入画布? Thanks for all your help and your patience! 感谢您的所有帮助和耐心等待!

// --- Image Loader ----
var images = {};
var loadedImages = 0;
var pictures = {
    room: 'room.jpg',
    title: 'title.jpg'
    lamp1: 'lampoff.jpg'
    lamp2: 'lampon.jpg'
};
function loadImages(sources, callback) {
    var numImages = 0;
    for(var src in sources)numImages++;

    for(var src in sources) {
        images[src] = new Image();
        images[src].onload = function() {
            if(++loadedImages >= numImages) {
                callback(images);
            }
        };
        images[src].src = sources[src];
    }
}



// --- Mouse Down Functionality ----
$('#canvas').addEventListener('mouseDown', function(e){
    if(e.clientX){
        var rect = this.getBoundingClientRect();
        if(rect)            clickCanvas(e.clientX - rect.left, e.clientY - rect.top)
        else                clickCanvas(e.clientX  - this.offsetLeft, e.clientY - this.offsetTop);
    }else if(e.offsetX)     clickCanvas(e.offsetX, e.offsetY);
    else if(e.layerX)       clickCanvas(e.layerX, e.layerY);
    else console.warn("Couldn't Determine Mouse Coordinates");
})


var lampOn;

function drawCanvas(showLamp){
    lampOn = showLamp;
    canvas.width = canvas.width //clears canvas
    context.drawImage(images.room, 0,0);
    context.drawImage(images.title, 0,0);
    if(lampOn){ 
        context.drawImage(images.lamp2, 100,100);
    }else{
        context.drawImage(images.lamp1, 100,100);
    }
}   

function clickCanvas(x,y){
    console.log('clicked canvas at:',x,y)
    if(clickedLamp){
        drawCanvas(!lampOn)
    }
}

loadImages(pictures, function(images) {
    drawCanvas(false)
});

Make sure to replace "clickedLamp" and "#canvas"! 确保替换“ clickedLamp”和“ #canvas”! The idea here is that you redraw the same canvas, using the same function. 这里的想法是您使用相同的功能重新绘制相同的画布。 Everytime you modify any of it, you rerender ALL of it. 每次修改任何内容时,都将其全部重新呈现。 See if you can get this example working, it will help clarify alot. 看看是否可以使该示例正常工作,这将有助于弄清很多问题。 If you don't understand something comment 如果您听不懂评论

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

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