所以我试图在画布上用Javascript绘制面孔,而微笑却在起作用ellipse(x, y, rX, rY, rot, start, end。我关闭了路径(closePath();),并通过笔划命令(stroke();)跟随了路径。绘制椭圆,看起来像一个半圆形。但这也画了一个连接半圆的两端的笔触,我不想。看起来像这样:
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var w = 500;
var h = 300;
window.onload = function() {
c.style.backgroundColor = '#aaa';
c.width = w;
c.height = h;
c.tabIndex = '1';
c.style.outline = 'none';
c.style.display = 'block';
c.style.margin = '0 auto';
c.style.position = 'relative';
c.style.top = '50px';
document.body.style.margin = '0';
document.body.appendChild(c);
}
setInterval(function() {
ctx.clearRect(0, 0, w, h);
ctx.beginPath();
ctx.ellipse(w / 2, h / 2, 30, 35, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = '#cf9454';
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.ellipse(w / 2 - 10, h / 2 - 10, 2, 2, 0, 0, 2 * Math.PI);
ctx.moveTo(w / 2 + 10, h / 2 - 10);
ctx.ellipse(w / 2 + 10, h / 2 - 10, 2, 2, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = '#000';
ctx.fill();
ctx.beginPath();
ctx.ellipse(w / 2, h / 2 + 10, 16, 12, 0, 0, 1 * Math.PI);
ctx.closePath();
ctx.stroke();
}, 30);Run Code Online (Sandbox Code Playgroud)
您应该简单地忽略最后一个closePath()呼叫。MDN的文档中描述了其意图:
CanvasRenderingContext2D.closePath()Canvas 2D API 的方法尝试从当前点到当前子路径的起点添加一条直线。
它有效地将该直线添加到“嘴”形。因此,只需将其省略:
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var w = 500;
var h = 140; // reduced to make the smiley appear within the snippet area
window.onload = function() {
c.width = w;
c.height = h;
c.tabIndex = '1';
c.style.backgroundColor = '#aaa';
c.style.outline = 'none';
c.style.display = 'block';
c.style.margin = '0 auto';
c.style.position = 'relative';
c.style.top = '50px';
document.body.style.margin = '0';
document.body.appendChild(c);
}
setInterval(function() {
ctx.clearRect(0, 0, w, h);
ctx.beginPath();
ctx.ellipse(w / 2, h / 2, 30, 35, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = '#cf9454';
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.ellipse(w / 2 - 10, h / 2 - 10, 2, 2, 0, 0, 2 * Math.PI);
ctx.moveTo(w / 2 + 10, h / 2 - 10);
ctx.ellipse(w / 2 + 10, h / 2 - 10, 2, 2, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = '#000';
ctx.fill();
ctx.beginPath();
ctx.ellipse(w / 2, h / 2 + 10, 16, 12, 0, 0, 1 * Math.PI);
//ctx.closePath(); //<----- remove this
ctx.stroke();
}, 30);Run Code Online (Sandbox Code Playgroud)
请注意,Unicode具有用于笑脸的字符。也许它们对您的项目有用吗?我还将一些样式移到CSS定义。在setInterval目前是没用的; 但我想您有它供将来使用。
例如:
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var w = 500;
var h = 140; // reduced to make the smiley appear within the snippet area
window.onload = function() {
c.width = w;
c.height = h;
c.tabIndex = 1;
document.body.appendChild(c);
ctx.font = "70px Verdana";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
setInterval(function() {
ctx.clearRect(0, 0, w, h);
ctx.fillText("", w/2, h/2);
}, 30);
}Run Code Online (Sandbox Code Playgroud)
canvas {
background-color: #aaa;
outline: none;
display: block;
margin: 0 auto;
position: relative;
top: 50px;
}
body { margin: 0; }Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |