Tutorial on adding mouse events to elements drawn in Html5 Canva

Source: Internet
Author: User

Add mouse events to the elements drawn in HTML5 Canva using Jtopo

When using HTML5, what is drawn on canvas is not appropriate for mouse events, but adding events using Jtopo is very simple, and the effect is as follows:


code example:

var node = new jtopo.node ("Hello"); Node.setLocation (
409, 269); Node.mousedown (function (event) {if (event.button == 2) {node.text =  ' right key ';} Else if (event.button == 1) {node.text =  ' Press the Middle key ';}
Else if (event.button == 0) {node.text =  ' press the left key ';}}); Node.mouseup (function (event) {if (event.button == 2) {node.text =  ' release right key ';} Else if (event.button == 1) {node.text =  ' loosen the middle key ';}
Else if (event.button == 0) {node.text =  ' release left Key ';}});
Node.click (function (event) {Console.log ("click");});
Node.dbclick (function (event) {Console.log ("double-click");});
Node.mousedrag (function (event) {Console.log ("drag");});
Node.mouseover (function (event) {Console.log ("mouseover");});
Node.mousemove (function (event) {Console.log ("MouseMove");}); Node.mouseout (function (event) {Console.log ("mouseout");}); 



Example Two


Q: with the HTML5 canvas picture, how to add mouse events (such as: Move in, click), thank you for your advice!


Answer one:

Object-oriented can be

One is you look for the canvas framework of the object, like Canvasjs.

One is you write your own object structure

Simply put, you canvas all the elements in the picture, like the circle, the line.

Each object has to implement mouse events Mouseon and so on each object to know its own coordinates and size (rectangle size)

But how do you trigger it? Bind all mouse events to canvas and then check the coordinates to see that the object is touched and then invoke the corresponding mouse event of the object


Answer two:

Instead of adding a mouse event to the graphic, you determine which shape is under the mouse in the mouse event. Such as

<! doctype> 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.