小编Mr *_*SVG的帖子

将onclick和onmouseover添加到canvas元素

我想补充的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)

javascript canvas onclick onmouseover

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

canvas ×1

javascript ×1

onclick ×1

onmouseover ×1