可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using html5 canvas element to draw a graph with dots denoting various points in here.
I want to display different tool-tip on different points on mouse hover.the text to be displayed as tool-tip will be provided by the user.
I tried but couldn't figure out how to add tool-tip to various points in the graph.The code I'm using for displaying dots is..
// Draw the dots c.fillStyle = '#333'; for (var i = 0; i
What addition should I make in this code so that i am able to display user input as tool-tip?
回答1:
回答2:
I tried markE's solution and it worked flawlessly, EXCEPT that when you scroll down just a little bit (e.g. when you have your canvas a little down the site).
Then the positions where your mouseover is recognized will shift to the bottom the same length and it could happen that they end up outside of the canvas and will not be recognized at all...
When you use mouseEvent.pageX and mouseEvent.pageY instead of .clientX and .clientY, you should be fine. For more context, here is my code:
// Filling the dots var dots = []; // [...] dots.push({ x: xCoord, y: yCoord, v: value, r: 5, tooltipRadius2: 7*7 // a little increased radius for making it easier to hit }); // [...] var tooltipCanvas = $('#tooltipCanvas')[0]; var tooltipCtx = tooltipCanvas.getContext('2d'); var canvasOffset = canvas.offset(); canvas.mousemove(function (e) { // getting the mouse position relative to the page - not the client var mouseX = parseInt(e.pageX - canvasOffset.left); var mouseY = parseInt(e.pageY - canvasOffset.top); var hit = false; for (var i = 0; i
回答3:
Short answer: as you've done it now, you can't.
Long answer: you can, but you need to get the exact mouse position every 30milliseconds or so. For each millisecond, you must check if the mouse is hovering over the dot, re-draw the screen and show the tooltip if he's doing it. Doing so by yourself can be tedious, this is why I use gee.js.
Check out this example: http://jsfiddle.net/Saturnix/Aexw4/
This is the expression which controls the mouse hovering:
g.mouseX x -r && g.mouseY > y -r && g.mouseY
回答4:
CSS ONLY method here. No javascript, JQUERY, or special library required. Lightweight, sexy.
HTML
