开发者

How to Attach Event to HTML5 Canvas?

开发者 https://www.devze.com 2023-03-01 14:25 出处:网络
So I know you can directly do: <开发者_高级运维;canvas id=\"someCanvas\" onmousedown=\"blah();\">

So I know you can directly do:

<开发者_高级运维;canvas id="someCanvas" onmousedown="blah();">

<script>
function blah() {
dosomething();
}
</script>

How would I attach the event in code only?

I tried doing:

var canvas = document.getElementById("someCanvas");

canvas.onmousedown = function() {
  doSomething();
}

But it does not work and doSomething does not get called... what am I doing wrong?


Try using AddEventListener, as such:

canvas.addEventListener("mousedown", function() {} );

https://developer.mozilla.org/en/DOM/element.addEventListener

0

精彩评论

暂无评论...
验证码 换一张
取 消