javascript - Responding to click event on <canvas> (Moving object) -


i new javascript (2 weeks) apologise of obvious question:

i have managed 2 red 'balls' bounce around canvas. attach sort of onclick event them if 1 clicked changes colour, pretty simple stuff..

i tried add onlick event -

p.onclick="gogreen(this, 'green')";** 

which triggers function -

function gogreen(elmnt,clr) { elmnt.style.color = clr; document.getelementbyid("demo").innerhtml =  "ok clicked ball"; } 

but doesn't work. no errors or nothing happens when objects clicked.

i have tried add listener wasn't successful either. advice can give me appreciated have been banging head against few hours now.

html:

<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>changing red green</title>  <style type="text/css"> </style>  </head>  <body> <p id="demo"></p> <div id="main_div">  <canvas id="canvas" width="500" height="500">  </canvas> </div>  </body> </html> 

javascript:

<script type="text/javascript"> //initialise canvas var canvas = document.getelementbyid("canvas"); var ctx = canvas.getcontext("2d");  ///put canvas dimesnions variables later use var w = 500;  var h = 500;  //create array can pop out particles var particles = []; for(var = 0; < 2; i++) { //this add particle array particles.push(new create_particle());  }   //this creates unique(ish) particle each time called function create_particle() {  //random position on canvas this.x = math.random()*w; this.y = math.random()*h;      //this code stop ball getting 'stuck' @ edges,         happens lot      if (this.x < 35) {     this.x = this.x+35     }      if (this.x > w - 50) {     this.x = this.x-35     }      if (this.y < 35) {     this.y = this.y+35     }      if (this.y > h - 50) {     this.y = this.y-35     }      //add random velocity each particle this.vx = math.random()*20-10; this.vy = math.random()*20-10;  //random size this.radius = math.random()*20+20;  }   //change color green , change text on page function gogreen(elmnt,clr) { elmnt.style.color = clr; document.getelementbyid("demo").innerhtml =  "ok clicked ball";  }   // draws particles  function draw() {  //paint canvas black each draw ctx.globalcompositeoperation = "source-over"; ctx.fillstyle = "rgba(0, 0, 0, .3)"; ctx.fillrect(0, 0, w, h); ctx.globalcompositeoperation = "lighter";  //draw particles array for(var t = 0; t < particles.length; t++) {     var p = particles[t];      ctx.beginpath();     ctx.fillstyle = "red";     ctx.arc(p.x, p.y, p.radius, math.pi*2, false);     ctx.fill();      //apply movement     p.x += p.vx;     p.y += p.vy;      //add onclick event here     p.onclick="gogreen(this, 'red')";       //bounce if ball hits edges     if(p.x < p.radius) p.vx *= -1;     if(p.y < p.radius) p.vy *= -1;     if(p.x > w-p.radius) p.vx *= -1;     if(p.y > h-p.radius) p.vy *= -1;   } }  //starts draw , sets refresh rate setinterval(draw, 33);  </script> 


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -