Home > Web Front-end > H5 Tutorial > Html5 Canvas Preliminary Study Notes (15) - Mouse Events

Html5 Canvas Preliminary Study Notes (15) - Mouse Events

黄舟
Release: 2017-02-28 16:15:14
Original
1379 people have browsed it

This article completes such an example, letting the protagonist move with the mouse, the effect is as follows:



The update function is the same as the previous article, the code is as follows:



Then look at The initialization part of init is the same as the example in the previous article. The code is as follows:


Then Define mousedown, mousemove, mouseup respectively, corresponding to mouse pressing, moving and lifting respectively. rise. The codes are as follows:



First use the following code to obtain the coordinates. The following code is used in various browsers:

var mx,my;
if(ev.layerX || ev.layerX==0){
mx = ev.layerX;
my = ev.layerY;
}else if(ev.offsetX || ev.offsetX==0){
mx = ev.offsetX;
my = ev.offsetY;
}
Copy after login

Then check whether the mouse coordinates are within the range of the picture, and obtain the coordinate offset if within the range.



##If

mousedown is within the picture range, in Re-obtain the image coordinates in move, release it when lifting the function, and then register the function to canvas to complete this example/

The above is the content of Html5 Canvas preliminary study notes (15) - mouse events. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template