Click here to Skip to main content
15,909,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,
how can I make mouseevent only when I cross shape(made by lineTo(),not whole canvas)?
HTML
var canvas_attic = document.getElementById('attic');
            var context_attic = canvas_attic.getContext('2d');
            canvas_attic.addEventListener('mousemove', function(evt) {
                context_attic.fillStyle = "rgb(500,0,0)";
                context_attic.fill();
            }, false);
 
            context_attic.beginPath();
            context_attic.moveTo(17,100);
 
            context_attic.lineTo(262,2);
            context_attic.lineTo(322,53);
            context_attic.lineTo(400,53);
            context_attic.lineTo(570,138);
            context_attic.lineTo(570,170);
            context_attic.lineTo(407,198);
            context_attic.lineTo(17,178);
 
 
            context_attic.closePath();
            context_attic.lineWidth = 1;
            context_attic.strokeStyle = 'blue';
            context_attic.stroke();
            context_attic.fillStyle = "rgb(200,0,0)";
            context_attic.fill();


Regards,Mac
Posted

1 solution

You can't. You have to translate the mouse position into a position on your canvas, then determine whether that position is inside or outside your figure. The isPointInPath function[^] should help you out.

Or you could use a library to do all the hard work for you: http://www.html5canvastutorials.com/kineticjs/html5-canvas-path-mouseover/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900