简体   繁体   中英

After Clearing HTML canvas new shape isn't showing up

I am trying to make an emoji in HTML Canvas.I included a button to add wink functionality for the right eye of the emoji.

But I am facing an issue here,after canvas is cleared new modified emoji is'nt showing up.

Can Someone help me out?

Below is my JavaScript Code to fulfill the purpose.

Basically wink function clears canvas and then recreate emoji but replacing right eye with a line.

Initially Emoji looks like this. 在此处输入图片说明

I am trying to make this.

在此处输入图片说明

var canvas=document.querySelector('canvas')
var ctx=canvas.getContext('2d');
canvas.width=innerWidth;
canvas.height=innerHeight;
ctx.lineWidth=3;
ctx.strokeStyle='white'; 
ctx.fillStyle='cyan';
var col=['yellow','crimson','cyan','pink','orange'];

//Emoji face
ctx.beginPath();
ctx.arc(750,200,100,0,Math.PI*2,false);
ctx.fill();
ctx.stroke();

//left eye
ctx.beginPath();
ctx.arc(700,150,15,0,Math.PI*2,false);
ctx.fillStyle='white'
ctx.fill();
ctx.stroke()

//Right eye
ctx.beginPath();
ctx.arc(800,150,15,0,Math.PI*2,false);
ctx.fillStyle='white'
ctx.fill();
ctx.stroke()

//Nose
ctx.beginPath();
ctx.moveTo(735,200);
ctx.lineTo(765,200);
ctx.lineTo(750,230);
ctx.lineTo(735,200);
ctx.fillStyle='white'
ctx.fill();
ctx.stroke();

//Mouth
ctx.beginPath();
ctx.arc(750,230,50,0,Math.PI,false);
ctx.stroke();

//Making new emoji with right eye closed
function wink()
{
ctx.clearRect(0,0,innerWidth,innerHeight);

//Emoji face
ctx.beginPath();
ctx.arc(750,200,100,0,Math.PI*2,false);
ctx.fill();
ctx.stroke();

//left eye
ctx.beginPath();
ctx.arc(700,150,15,0,Math.PI*2,false);
ctx.fillStyle='white'
ctx.fill();
ctx.stroke()

//Right eye
ctx.beginPath();
ctx.moveTo(785,150);
ctx.lineTo(815,150);
ctx.stroke();

//Nose
ctx.beginPath();
ctx.moveTo(735,200);
ctx.lineTo(765,200);
ctx.lineTo(750,230);
ctx.lineTo(735,200);
ctx.fillStyle='white'
ctx.fill();
ctx.stroke();

//Mouth
ctx.beginPath();
ctx.arc(750,230,50,0,Math.PI,false);
ctx.stroke();

}

The fillStyle is set to white for the nose part of the emoji during the initial canvas paint which is not reset. Set fillStyle: cyan in the start of the wink() .

function wink() {
  ctx.fillStyle = 'cyan';
  /*
    Rest of the code
    ...
    ...
  */
}

Hope this solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM