【发布时间】:2014-03-12 02:51:06
【问题描述】:
我想在这段 javascript 代码中使用 onmouseover。因此,每当我将鼠标移到正方形上时,changeColor 函数都会执行并每次从两种给定颜色中选择一种颜色。
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
<body>
<h1>Canvas Art Gallary</h1>
<canvas id="myCanvas" width="400" height="300" style="border:10px solid #c3c3c3">
</canvas>
<script type="text/javascript">
function changeColor(){
ctx.fillStyle = "#FFFFFF";
ctx.fillStyle = "#04B45F";
}
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(150,100,100,100);
</script>
</body>
</html>
谢谢
【问题讨论】:
-
您的问题是“如何在 javascript 中创建 mouseover 事件处理程序”? (在代码中没有看到任何鼠标悬停事件)欢呼
-
我不相信画布元素上的鼠标事件有任何原生支持。但是我使用了 Kinetic JS 库,它确实实现了事件处理:html5canvastutorials.com/kineticjs/html5-canvas-path-mouseover
-
是的,rob,我将如何创建它?这就是我想要的??我知道我必须在代码中的某处使用 onmouseover 并将函数 changeColor 放在那里,但我会在哪里以及如何???
-
你应该参考矩形在画布中的位置,然后在鼠标悬停(htmlcanvaselement显然已经支持鼠标悬停事件)上,检查你的鼠标指针是否在矩形的边界上,另一种方法是使用像 Fabricjs 或 Paperjs 这样的画布框架...
标签: javascript canvas