簡體   English   中英

為畫布上的圖像創建事件

[英]Create event for an image that's on canvas

我試圖做到這一點:當我單擊畫布上的圖像時,會彈出一條消息或顯示另一幅圖像(某些事件)。 我不明白為什么我的代碼無法正常工作。 有人能幫我嗎? 提前致謝!

<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
    canvas {
        border: 5px solid #d3d3d3;
        background-color: #F08080;
    }
    </style>
    </head>
    <body onload="startGame()">
    <script>

    var backgroundMusic;

    function startGame() {
        myGameArea.start();
        myPlayer = new component(300,300,"dort.png", 10, 120, "image");
        backgroundMusic = new sound("panjabi.mp3")
        backgroundMusic.play();
    }

    var myGameArea = {
        canvas : document.createElement("canvas"),
        start : function() {
            this.canvas.width = 1200;
            this.canvas.height = 700;
            this.context = this.canvas.getContext("2d");
            document.body.insertBefore(this.canvas, document.body.childNodes[0]);
            this.interval = setInterval(updateGameArea, 20);
            window.addEventListener('keydown', function(e) {
                myGameArea.key = e.keyCode;
            })
            window.addEventListener('keyup', function(e){
                myGameArea.key = false;
            })
        },
        clear : function() {
            this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
        }
    }

    function component(width, height, color, x, y, type) {
        this.type = type;
        if (type == "image") {
            this.image = new Image();
            this.image.src = color;
        }
        this.gamearea = myGameArea;
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speedX = 0;
        this.speedY = 0;
        this.left = x
        this.update = function() {
        ctx = myGameArea.context;
        if (type == "image") {
            ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
        } else {
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height); } }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
    }

    function updateGameArea() {
        myGameArea.clear();
        myPlayer.speedX = 0;
        myPlayer.speedY = 0;
        if (myGameArea.key && myGameArea.key == 37) {myPlayer.speedX = -10}
        if (myGameArea.key && myGameArea.key == 39) {myPlayer.speedX = 10}
        if (myGameArea.key && myGameArea.key == 38) {myPlayer.speedY = -10}
        if (myGameArea.key && myGameArea.key == 40) {myPlayer.speedY = 10}
        myPlayer.newPos();
        myPlayer.update();
    }

    function sound(src) {
        this.sound = document.createElement("audio");
        this.sound.src = src;
        this.sound.setAttribute("preload", "auto");
        this.sound.setAttribute("controls", "none");
        this.sound.style.display = "none";
        document.body.appendChild(this.sound);
        this.play = function() {
            this.sound.play();
        }
        this.stop = function(){
            this.sound.pause();
        }
    }
    $('#canvas').click(function (e) {
        var clickedX = e.pageX - this.offsetLeft;
        var clickedY = e.pageY - this.offsetTop;
        for(var i = 0; i < myPlayer[i].length; i++) {
            if(clickedX < myPlayer[i].right && clickedX > myPlayer[i].left && 
                    clickedY > myPlayer[i].top && clickedY < myPlayer[i].bottom) {
                alert('clicked number');
                    }
        }
    });
    </script>
    </body>
    </html>

我的第一個猜測: $('#canvas')找到id為“ canvas”的元素,但是您的canvas元素沒有任何id設置。 如果您在啟動函數中執行this.canvas.id = 'canvas'這樣的事情,它會起作用嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM