You need to enable JavaScript to run this app. 如何用JavaScript和Canvas在棋盘格上绘制仿真跳棋圆形棋子
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript和Canvas在棋盘格上绘制仿真跳棋圆形棋子

跳棋棋盘与棋子绘制代码修改方案

核心调整方向

  • 重构原有零散绘制的棋盘格逻辑,生成标准8*8交错深浅格的跳棋棋盘,消除原有代码棋盘不连贯的问题
  • 新增带立体感的圆形棋子绘制逻辑,严格匹配玩家1红色、玩家2黑色的颜色要求
  • 按照跳棋常规初始布局摆放棋子,还原真实跳棋的视觉效果

具体修改步骤

  1. 重设棋盘绘制规则
    原有代码仅零散绘制间隔200px的100px灰色块,无法形成连续交错棋盘。修改后先把整画布铺为白色底色,再按照100px为单个格子尺寸,遍历棋盘所有位置,仅当格子的行索引+列索引为奇数时绘制深灰色块,形成标准交错棋盘格。
  2. 新增圆形棋子绘制逻辑
    棋子需放置在每个深色格子的正中心,直径取格子边长的80%,避免紧贴格子边缘。为模拟真实棋子的光滑质感,不用纯平涂色块,改用径向渐变绘制亮部+基础色的过渡效果,搭配同色系深色细描边强化轮廓。
  3. 按规则摆放初始棋子
    严格遵循标准跳棋初始布局:棋盘上3行的深色格放置玩家2的黑色棋子,棋盘下3行的深色格放置玩家1的红色棋子,中间3行深色格留空作为移动区域,避免满盘摆放导致不符合常规跳棋的视觉效果。

修改后可直接运行的完整代码

const canvas = document.querySelector('canvas')
// 固定单格尺寸,避免窗口缩放导致格子比例变形
const CELL_SIZE = 100 
// 生成标准8*8跳棋棋盘的画布尺寸
canvas.width = 8 * CELL_SIZE
canvas.height = 8 * CELL_SIZE
const c = canvas.getContext('2d')

// 绘制棋盘白色底色
c.fillStyle = '#ffffff'
c.fillRect(0, 0, canvas.width, canvas.height)

// 绘制交错深灰色棋盘格
c.fillStyle = 'rgba(128,128,128,1.0)'
for (let row = 0; row < 8; row++) {
  for (let col = 0; col < 8; col++) {
    if ((row + col) % 2 === 1) {
      c.fillRect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE)
    }
  }
}

// 定义带立体感的圆形棋子类
function CheckersPiece(centerX, centerY, radius, baseColor) {
  this.centerX = centerX
  this.centerY = centerY
  this.radius = radius
  this.baseColor = baseColor

  // 工具方法:把基础色调亮,用于制作棋子高光
  this.lightenColor = (hex, percent) => {
    const num = parseInt(hex.slice(1), 16)
    const amt = Math.round(2.55 * percent)
    const R = Math.min(255, (num >> 16) + amt)
    const G = Math.min(255, ((num >> 8) & 0x00FF) + amt)
    const B = Math.min(255, (num & 0x0000FF) + amt)
    return `rgb(${R}, ${G}, ${B})`
  }

  // 工具方法:把基础色调暗,用于制作棋子描边
  this.darkenColor = (hex, percent) => {
    const num = parseInt(hex.slice(1), 16)
    const amt = Math.round(2.55 * percent)
    const R = Math.max(0, (num >> 16) - amt)
    const G = Math.max(0, ((num >> 8) & 0x00FF) - amt)
    const B = Math.max(0, (num & 0x0000FF) - amt)
    return `rgb(${R}, ${G}, ${B})`
  }

  this.draw = function() {
    // 径向渐变模拟棋子立体高光
    const highlightGradient = c.createRadialGradient(
      this.centerX - this.radius*0.3,
      this.centerY - this.radius*0.3,
      this.radius*0.1,
      this.centerX,
      this.centerY,
      this.radius
    )
    highlightGradient.addColorStop(0, this.lightenColor(this.baseColor, 30))
    highlightGradient.addColorStop(1, this.baseColor)

    c.beginPath()
    c.arc(this.centerX, this.centerY, this.radius, 0, Math.PI * 2)
    c.fillStyle = highlightGradient
    c.fill()

    // 细描边强化棋子轮廓
    c.strokeStyle = this.darkenColor(this.baseColor, 20)
    c.lineWidth = 2
    c.stroke()
  }
}

// 按跳棋初始布局摆放棋子
const PIECE_RADIUS = CELL_SIZE * 0.4
for (let row = 0; row < 8; row++) {
  for (let col = 0; col < 8; col++) {
    // 仅在深色棋盘格上摆放棋子
    if ((row + col) % 2 === 1) {
      const cellCenterX = col * CELL_SIZE + CELL_SIZE / 2
      const cellCenterY = row * CELL_SIZE + CELL_SIZE / 2
      // 上3行放置玩家2的黑色棋子
      if (row < 3) {
        new CheckersPiece(cellCenterX, cellCenterY, PIECE_RADIUS, '#000000').draw()
      }
      // 下3行放置玩家1的红色棋子
      if (row > 4) {
        new CheckersPiece(cellCenterX, cellCenterY, PIECE_RADIUS, '#e63946').draw()
      }
    }
  }
}

内容的提问来源于stack exchange,提问作者user14187680

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.29 12:24:04