怎么用canvas 画出围棋棋子和上面的那些纹路,包括底下的阴影呢,就比如这样子,能够逼真一点
可以用ps画出黑白棋子,做成png(带透明颜色通道)
让后贴图,和背景的棋盘混合
首先,我们需要定义一些常量,如棋盘大小、棋子大小、线条宽度、棋盘背景色、棋子颜色等。然后,我们可以使用canvas的绘图API来绘制棋盘和棋子。
绘制棋盘:
ctx.fillStyle = boardBgColor;
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
// 绘制横线
for (let i = 0; i < boardSize; i++) {
ctx.beginPath();
ctx.moveTo(padding, padding + i * gridSize);
ctx.lineTo(padding + (boardSize - 1) * gridSize, padding + i * gridSize);
ctx.stroke();
}
// 绘制竖线
for (let i = 0; i < boardSize; i++) {
ctx.beginPath();
ctx.moveTo(padding + i * gridSize, padding);
ctx.lineTo(padding + i * gridSize, padding + (boardSize - 1) * gridSize);
ctx.stroke();
}
绘制棋子:
ctx.shadowColor = shadowColor;
ctx.shadowOffsetX = shadowOffsetX;
ctx.shadowOffsetY = shadowOffsetY;
ctx.shadowBlur = shadowBlur;
ctx.fillStyle = chessColor;
ctx.beginPath();
ctx.arc(x, y, chessRadius, 0, 2 * Math.PI);
ctx.fill();
ctx.strokeStyle = chessLineColor;
ctx.lineWidth = chessLineWidth;
ctx.beginPath();
ctx.arc(x, y, chessRadius - chessLineWidth / 2, 0, 2 * Math.PI);
ctx.stroke();
完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
canvas {
border: 1px solid #666;
}
</style>
</head>
<body>
<!-- 设置画布 -->
<canvas id="canvas" width="570" height="570"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const boardSize = 19; // 棋盘大小
const gridSize = 30; // 格子大小
const padding = 15; // 边距
const lineWidth = 2; // 线条宽度
const boardBgColor = '#DDBB83'; // 棋盘背景色
const lineColor = '#000'; // 线条颜色
const chessRadius = 12; // 棋子半径
const chessColor = '#000'; // 棋子颜色
const shadowColor = 'rgba(0, 0, 0, 0.5)'; // 阴影颜色
const shadowOffsetX = 2; // 阴影X偏移量
const shadowOffsetY = 2; // 阴影Y偏移量
const shadowBlur = 3; // 阴影模糊度
const chessLineColor = '#fff'; // 棋子纹路颜色
const chessLineWidth = 1; // 棋子纹路宽度
function drawBoard() {
ctx.fillStyle = boardBgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
// 绘制横线
for (let i = 0; i < boardSize; i++) {
ctx.beginPath();
ctx.moveTo(padding, padding + i * gridSize);
ctx.lineTo(padding + (boardSize - 1) * gridSize, padding + i * gridSize);
ctx.stroke();
}
// 绘制竖线
for (let i = 0; i < boardSize; i++) {
ctx.beginPath();
ctx.moveTo(padding + i * gridSize, padding);
ctx.lineTo(padding + i * gridSize, padding + (boardSize - 1) * gridSize);
ctx.stroke();
}
}
function drawChess(x, y) {
ctx.shadowColor = shadowColor;
ctx.shadowOffsetX = shadowOffsetX;
ctx.shadowOffsetY = shadowOffsetY;
ctx.shadowBlur = shadowBlur;
ctx.fillStyle = chessColor;
ctx.beginPath();
ctx.arc(x, y, chessRadius, 0, 2 * Math.PI);
ctx.fill();
ctx.strokeStyle = chessLineColor;
ctx.lineWidth = chessLineWidth;
ctx.beginPath();
ctx.arc(x, y, chessRadius - chessLineWidth / 2, 0, 2 * Math.PI);
ctx.stroke();
}
drawBoard();
drawChess(padding + 3 * gridSize, padding + 3 * gridSize);
drawChess(padding + 9 * gridSize, padding + 9 * gridSize);
</script>
</body>
</html>