A Pen By
shundroid

` ````
<canvas id="canvas" width="200" height="200"></canvas>
```

` ````
#canvas {
border: 1px solid black;
}
```

` ````
var lineWidth = 5;
var points = [
{ x: 30, y: 50 },
{ x: 120, y: 150 }
];
var outline1 = [];
var outline2 = [];
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.strokeStyle = "red";
ctx.beginPath();
points.forEach((point, index) => {
ctx.lineTo(point.x, point.y);
if (index === 0) {
// 始点
var rad = Math.atan2(points[index + 1].y - point.y, points[index + 1].x - point.x) - Math.PI / 2;
var sin = Math.sin(rad) * lineWidth;
var cos = Math.cos(rad) * lineWidth;
outline1.push({ x: point.x + cos, y: point.y + sin });
outline2.push({ x: point.x - cos, y: point.y - sin });
} else if (index === points.length - 1) {
// 終点
var rad = Math.atan2(point.y - points[index - 1].y, point.x - points[index - 1].x) - Math.PI / 2;
var sin = Math.sin(rad) * lineWidth;
var cos = Math.cos(rad) * lineWidth;
outline1.push({ x: point.x + cos, y: point.y + sin });
outline2.push({ x: point.x - cos, y: point.y - sin });
}
});
ctx.stroke();
ctx.strokeStyle = "black";
ctx.beginPath();
outline1.forEach(point => {
ctx.lineTo(point.x, point.y);
});
ctx.stroke();
ctx.strokeStyle = "black";
ctx.beginPath();
outline2.forEach(point => {
ctx.lineTo(point.x, point.y);
});
ctx.stroke();
```

999px

Loading
..................

Alt F
Opt F
Find & Replace

Also see: Tab Triggers