P2 TW2 - questão 1

In this example below you will see how to do a P2 TW2 - questão 1 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by phcacique, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright phcacique ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>P2 TW2 - quest√£o 1</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <canvas id='canvas'></canvas>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/phcacique/p2-tw2-questandxe3o-1-bYWXGy */
html, body{
	margin:0;
	height: 100%;
}

body{
	display: flex;
	justify-content: center;
	align-items: center;
	background-color: #202020;
}

canvas{
	background-color: #fff;
}

/*Downloaded from https://www.codeseek.co/phcacique/p2-tw2-questandxe3o-1-bYWXGy */
var canvas, ctx;
var isCircle = true;

window.onload = function(){
	canvas = document.getElementById('canvas');
	canvas.width = innerWidth*0.5;
	canvas.height = innerHeight*0.5;
	ctx = canvas.getContext(`2d`);
	
	canvas.onclick = draw;
}

function draw(){
	var x = event.clientX - canvas.offsetLeft;
	var y = event.clientY - canvas.offsetTop;
	
	ctx.beginPath();
	if(isCircle){
		ctx.fillStyle = '#f00';
		ctx.arc(x,y,20,0,2*Math.PI);
		ctx.fill();
	} else {
		ctx.fillStyle = '#00f';
		ctx.rect(x,y,40,40);
		ctx.fill();
	}
	ctx.closePath();
	
	isCircle = !isCircle;
	
}

Comments