[HTML5 e JS] Cliccare sull'oggetto
Inviato: sabato 24 maggio 2014, 17:33
Salve, volevo sapere se qualcuno mi aiutasse ad sistemare il codice che sto modificando da un'esempio:
http://www.html5freecode.com/Canvas-_Select_an_Object_by_Mouse_Click_(Hit_Detection).htm
il mio codice è cosi:
c'è qualcuno che sa come sistemare e non fare tante funzioni per ogni colore?
grazie mille.
http://www.html5freecode.com/Canvas-_Select_an_Object_by_Mouse_Click_(Hit_Detection).htm
il mio codice è cosi:
Codice: Seleziona tutto
<!-- This Script is from www.html5freecode.com, Coded by: Kerixa Inc-->
<br>
<canvas id="myCanvas" width="400" height="230" onclick="DefinePaths(event)"></canvas>
<script>
var cnv = document.getElementById('myCanvas');
var ctx = cnv.getContext('2d');
var deg = Math.PI/180;
DefinePaths(null);
function drawRect(x, y, w, h, fillColor, strokeColor)
{
strokeColor = strokeColor || '#000000';
ctx.fillStyle = fillColor;
ctx.fillRect(x, y, w, h);
ctx.strokeStyle = strokeColor;
ctx.strokeRect(x, y, w, h);
}
function DefinePaths(event){
drawRect(100, 40, 50, 50, 'blue', '');
if (event!=null){
if (IsInPath(event))
color_blue();
else color_blue();
}
drawRect(200, 40, 50, 50, 'red', '');
if (event!=null){
if (IsInPath(event))
color_red();
else color_red();
}
}
function IsInPath(event) {
var bb, x, y
bb = cnv.getBoundingClientRect()
x = (event.clientX-bb.left) * (cnv.width/bb.width)
y = (event.clientY-bb.top) * (cnv.height/bb.height)
return ctx.isPointInPath(x,y)
}
function color_blue(){
drawRect(100, 40, 50, 50, 'darkblue', '#ffbf18');
console.log('blue');
alert('blue');
}
function color_red(){
drawRect(100, 40, 50, 50, 'darkred', '#ffbf18');
console.log('red');
alert('red');
}
</script>grazie mille.