我想补充的onclick,onmouseover以及onmouseout在canvas元素事件单个形状.
我尝试过以不同的方式使用SVG,并发现没有一种方法可以在所有主流浏览器中使用.
也许,有没有一种简单的方法可以onclick为画布形状添加一个可能的其他事件?
有人可以告诉我如何添加onclick?
这是我的代码:
canvas
{
background:gainsboro;
border:10px ridge green;
}
Run Code Online (Sandbox Code Playgroud)
<canvas id="Canvas1"></canvas>
Run Code Online (Sandbox Code Playgroud)
var c=document.getElementById("Canvas1");
var ctx=c.getContext("2d");
ctx.fillStyle="blue";
ctx.fillRect(10,10,60,60);
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(80,60,60,60);
// these need an onclick to fire them up. How do I add the onclick
function blue()
{
alert("hello from blue square")
}
function red()
{
alert("hello from red square")
}
Run Code Online (Sandbox Code Playgroud)