使用 HTML5 在 Canvas 上绘制图钉

Sea*_*n D -2 html canvas

我需要在画布上使用 HTML5绘制如下图钉:http : //www.clipartbest.com/clipart-9czEGGdRi。

这是我所拥有的:http : //jsfiddle.net/u5jNR/

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var startAngle = .9 * Math.PI;
var endAngle = 2.1 * Math.PI;
var counterClockwise = false;
context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
context.lineWidth = 15;
// line color
context.strokeStyle = 'black';
context.stroke();


var radius = 20;

context.beginPath();
context.arc(x, y, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
context.stroke();
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我不知道如何延长两端。

提前致谢

mar*_*rkE 5

这是使用路径命令绘制图钉的示例。

假设您有一个定义 pin 的 x、y 和颜色的对象:

var pin = { x:x, y:y, color:color };
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样画出那个图钉:

function drawPin(pin){

    ctx.save();
    ctx.translate(pin.x,pin.y);

    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.bezierCurveTo(2,-10,-20,-25,0,-30);
    ctx.bezierCurveTo(20,-25,-2,-10,0,0);
    ctx.fillStyle=pin.color;
    ctx.fill();
    ctx.strokeStyle="black";
    ctx.lineWidth=1.5;
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(0,-21,3,0,Math.PI*2);
    ctx.closePath();
    ctx.fillStyle="black";
    ctx.fill();

    ctx.restore();
}
Run Code Online (Sandbox Code Playgroud)