Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>animazione mostro</title>
<style>
canvas{
	border:0px solid;
}
body{
	background:#CFF;
}
</style>
</head>
  
<body onload="init();">
<canvas width="1200" height="500" id="mycanvas"></canvas>
<script>
  // Michel : I added a global var rectColor here
  var x, y, speed, canvas, context, mousePos, rectColor='red';
  
  
    reqAnimFrame = window.mozRequestAnimationFrame    || 
                window.webkitRequestAnimationFrame ||
                window.msRequestAnimationFrame     ||
                window.oRequestAnimationFrame
                ;
  
  
  function init() {
   
    x =  200;
    y = 150;
    speed = 4;
   
   
    canvas  = document.getElementById("mycanvas");
    context = canvas.getContext("2d");
	

    // Michel : add these listeners here, the canvas will be defined for sure
    // as the init method is called only when the page is loaded
  canvas.addEventListener('mousedown', paint_redrect_yellow);
  canvas.addEventListener('mouseup', paint_redrect_red);
    
    
    canvas.addEventListener('mousemove', function (evt) {
        
        mousePos = getMousePos(canvas, evt);    
        }, false);
		

    animate();
  }
  
  
  // MICHEL : you just need to set the global variable, not set the style or do
  // any drawing. The global variable will be takjen into account by the 
  // drawMonster function
   function paint_redrect_yellow() {
 	rectColor='yellow';
   }
    
	function paint_redrect_red() {
 	  rectColor='red';
    }
     
  
function animate() {
 
  	context.clearRect(0, 0, canvas.width, canvas.height);

 
  drawMonster();
  
  
    x += speed;

    if(x <= 110 || x >= 655){
        speed = -speed; 
    }
    
  
   reqAnimFrame(animate);
}


  
function drawMonster() { 
  
    context.save();  
  	context.translate(x, y);
	context.beginPath()
	context.fillStyle ='rgba(51,51,255,1)';
	context.fillRect(0, 0, 400, 250);	
  
 

  <!--red rectangle -->
    // Michel : use the global variable here
   context.fillStyle =rectColor;
   context.fillRect(25,25,350,200); 
   
  
	
 //right yellow eye
context.beginPath();
context.arc(300,0,70,0, Math.PI/0.5);
context.fillStyle="yellow";
context.fill()
context.strokeStyle = "#3333ff";
context.lineWidth = 5;
context.stroke();

 //right black eye

  /*context.beginPath();
context.arc(280,0,30,0, Math.PI/0.5);
context.fillStyle="#000000";
context.fill()
 */
  //left yellow eye
 context.beginPath();
context.arc(100,0,70,0, Math.PI/0.5);
context.fillStyle="yellow";
context.fill()
context.strokeStyle = "#3333ff";
context.lineWidth = 5;
context.stroke();
  
  
  	
 <!--sopracciglia sn -->
context.beginPath();
context.arc(115,-15,80,0,5, true);
context.strokeStyle="#000000";
context.lineWidth = 13;
context.lineCap ="round";
context.stroke();


<!--sopracciglia dx -->
context.beginPath();
context.arc(290,-20,80,4.2,9.3, true);
context.strokeStyle="#000000";
context.lineWidth = 13;
context.stroke();

  
 <!--mouse first part -->
context.beginPath();
context.moveTo(50,120);
context.quadraticCurveTo(50,150,200,200);
<!--mouse second part -->
context.quadraticCurveTo(350,240,350,150);
context.closePath();

context.save();
context.fillStyle='black';
context.fill();
context.restore();


<!--teeth above-->
context.beginPath()
context.moveTo(180,135);
context.lineTo(190,180);
context.lineTo(210,138);

 context.moveTo(245,142);
context.lineTo(290,180);
context.lineTo(330,150);

<!--teeth below -->
context.moveTo(260,210);
context.lineTo(210,200);
context.lineTo(240,180);

context.fillStyle ="#fff";
context.fill();	

<!--left arm -->
context.beginPath()
context.moveTo(-50,150);
context.lineTo(0,150);

<!--right arm -->
context.moveTo(480,180);
context.lineTo(400,180);

context.lineWidth = 15;
context.strokeStyle = "#3333ff";
context.lineCap ="round";
context.stroke();

<!--tenaglia braccio sn -->
context.beginPath()
context.arc(-80,150,30,0, Math.PI/1.5, true);
<!--tenaglia braccio dx -->
context.moveTo(480,150);
context.lineTo(480,230);
context.lineTo(540,230);
context.lineTo(540,180);


context.lineWidth = 15;
context.stroketyle="#000000";
context.stroke()

context.restore();
  
  

 
  // You told me I have to change x, y + the drawings in the drawEyeThatFollowsMousePos
  // so that the eye has the right colors/position
  // I was only able to change the radius but if i put number instead of x and y the eye does not follow the animation anymore
  //What's wrong with this?
  // Michel ; try x+ something, like the x+150 below
  if(mousePos != undefined) {
     drawEyeThatFollowsMousePos(x+100, y, 50, mousePos.x, mousePos.y)
     drawEyeThatFollowsMousePos(x+300, y, 50, mousePos.x, mousePos.y)
  } else {
    // If the mouse is not here, look at 0, 0 for example...
    drawEyeThatFollowsMousePos(x+100, y, 50, 300, 300)
    drawEyeThatFollowsMousePos(x+300, y, 50, 300, 300)
  }
  
}	

 
    function getMousePos(canvas, evt) {
       
        var obj = canvas;
        var top = 0;
        var left = 0;
 
        while (obj && obj.tagName != 'BODY') {
            top += obj.offsetTop;
            left += obj.offsetLeft;
            obj = obj.offsetParent;
        }
       
        var mouseX = evt.clientX - left + window.pageXOffset;
        var mouseY = evt.clientY - top + window.pageYOffset;
        return {
            x:mouseX,
            y:mouseY
        };
     }
  
  
 function drawEyeThatFollowsMousePos(cx, cy, radius, mouseX, mouseY) {      
      // Draw external eye outline
      context.beginPath();
      context.arc(cx, cy, 50, 0, 2*Math.PI, false); 
      context.stroke();
      
      
      var angle = Math.atan2(cx-mouseX, cy-mouseY)
      
      
      var dist = distanceFromEyeCenterForDrawingEye(cx, cy, mouseX, mouseY, 15);
      
      
      var dx = dist*Math.sin(angle);
      var dy = dist*Math.cos(angle);
      
      
      context.beginPath();
      context.arc(cx-dx, cy-dy, 30, 0, 2*Math.PI, false);
      context.fill();
    }
    
    
    function distanceFromEyeCenterForDrawingEye(cx, cy, x, y, treshold) {
      var xd=(cx-x), yd=(cy-y);
      
      var distMouseEyeCenter = Math.sqrt(xd*xd + yd*yd);
      return Math.min(treshold, distMouseEyeCenter);
    }
  
</script>
</body onload="init();">
</html>

</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console