📜  如何在画布 html5 中添加按钮 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:08.140000             🧑  作者: Mango

代码示例1
/*You can solve this multiple ways like using html button
 suggested in the comments.

But if you do need to handle click events inside your canvas
 you can do something like this:

Add a click handler to the canvas and when the mouse pointer
 is inside your bounding rectangle you can fire your click function:
*/

//Function to get the mouse position
function getMousePos(canvas, event) {
    var rect = canvas.getBoundingClientRect();
    return {
        x: event.clientX - rect.left,
        y: event.clientY - rect.top
    };
}
//Function to check whether a point is inside a rectangle
function isInside(pos, rect){
    return pos.x > rect.x && pos.x < rect.x+rect.width && pos.y < rect.y+rect.height && pos.y > rect.y
}

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
//The rectangle should have x,y,width,height properties
var rect = {
    x:250,
    y:350,
    width:200,
    height:100
};
//Binding the click event on the canvas
canvas.addEventListener('click', function(evt) {
    var mousePos = getMousePos(canvas, evt);

    if (isInside(mousePos,rect)) {
        alert('clicked inside rect');
    }else{
        alert('clicked outside rect');
    }   
}, false);