简体   繁体   English

清除 HTML 画布后未显示新形状

[英]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.我正在尝试在 HTML Canvas 中制作表情符号。我包含一个按钮来为表情符号的右眼添加眨眼功能。

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.下面是我的 JavaScript 代码来实现这个目的。

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.在未重置的初始画布绘制期间,表情符号的鼻子部分的fillStyle设置为白色。 Set fillStyle: cyan in the start of the wink() .wink()的开头设置fillStyle: cyan

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

Hope this solves your problem.希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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