canvas画圆清除圆html,javascript,html5_js 用canvas 画圆清除问题?,javascript,html5 - phpStudy...

本文主要讨论了在使用JavaScript和HTML5 Canvas开发五子棋游戏中遇到的悔棋功能实现问题。作者通过clearRect()方法清除棋盘上的棋子,但在重新绘制时出现了一个额外的圆。问题可能源于清除与重绘的坐标不准确或者清除操作与绘制操作的顺序。同时,代码展示了布局、下棋和悔棋的函数实现。
摘要由CSDN通过智能技术生成

js 用canvas 画圆清除问题?

我在用js写一个五子棋游戏,利用canvas画棋盘和棋子,为了实现悔棋功能,我用clearRect()清除了原来的棋子,再重新画被清除的部分棋盘,却总是出现了一个圆,请大神帮帮忙,谢谢!

Chess by sheepbao

结束游戏

悔棋

var cell, half_cell;

var chess = document.getElementById('chess');

var context = chess.getContext('2d');

var undo = document.getElementById('undo');

function layout() {

var avaih = window.innerHeight,

avaiw = window.innerWidth;

var min = Math.min(avaih, avaiw);

/* chess.style.width = (min - 5) + 'px';

chess.style.height = (min - 5) + 'px';*/

var w = min - 5;

chess.width = w;

chess.height = w;

cell = Math.floor(w / 15);

half_cell = cell / 2;

console.log(w, cell, half_cell);

// context.strokeStyle = "#BFBFBF";

for (var i = 0; i < 15; i++) {

context.moveTo(half_cell, half_cell + (cell * i));

context.lineTo(w - half_cell, half_cell + (cell * i));

context.moveTo(half_cell + cell * i, half_cell);

context.lineTo(half_cell + cell * i, w - half_cell);

context.stroke();

}

}

layout();

//chesspiece

var oneStep = function(x, y, black) {

console.log("oneStep", x, y, black);

context.beginPath();

context.arc(half_cell + cell * x, half_cell + cell * y, half_cell * 0.8, 0, 2 * Math.PI);

context.closePath();

var gradient = context.createRadialGradient(half_cell + cell * x + 2, half_cell + cell * y - 2, 13, half_cell + cell * x + 2, half_cell + cell * y - 2, 0);

if (black) {

//black

gradient.addColorStop(0, "#0A0A0A");

gradient.addColorStop(1, "#636766");

} else {

gradient.addColorStop(0, "#D1D1D1");

gradient.addColorStop(1, "#F9F9F9");

}

context.fillStyle = gradient;

context.fill();

context.stroke();

}

function undoStep(x, y) {

console.log(chess.width, chess.height);

context.clearRect((x + 0.1) * cell, (y + 0.1) * cell, 0.8 * cell, 0.8 * cell);

console.log("undoStep:", x, y,

"rect:", (x + 0.1) * cell, (y + 0.1) * cell, 0.8 * cell, 0.8 * cell);

// context.globalCompositeOperation = "source-over";

context.moveTo(cell * (x + 0.1), (0.5 + y) * cell);

context.lineTo(cell * (x + 0.9), (0.5 + y) * cell);

context.moveTo((0.5 + x) * cell, cell * (y + 0.1));

context.lineTo((0.5 + x) * cell, cell * (y + 0.9));

context.stroke();

}

chess.onclick = function(e) {

var x = e.offsetX;

var y = e.offsetY;

meX = Math.floor(x / cell);

meY = Math.floor(y / cell);

console.log("click:", meX, meY);

oneStep(meX, meY, true);

}

undo.onclick = function(e) {

undoStep(meX, meY);

}

相关阅读:

使用fstab挂载一块外置的tf卡.重启后无法显示,但是使用mount却能挂载

angularjs http请求问题

关于js引用类型赋值的问题

【求助】python 遇到采集、正则问题,一直获取不到内容!

gulp和webpack结合使用如何分工?

嵌入app的h5与普通的h5有何不同?

无法打开包括文件nan.h

javascript 获取DOM节点的困惑,谢谢大神详解

Django Template Error : Could not parse the remainder

直播贴的后台设计架构!

if (function f(){})中function f(){}为何会是true?

用 vagrant 安装laravel/homestead 按照官网的步骤一步步来的,启动的时候出现问题

通过myeclipse启动tomcat的话,在catalina中设置的参数就不起作用?

scrapy写爬虫 却返回不出东西

有哪些大型网站的移动端架构采用zepto?

PHP长时间运行的守护脚本内存溢出,来大神分析一下。

php的一个正则

IE 跨域 Cookie 失效怎么处理?

用section标签后,input会失效是什么原因

源码编译apache2.4.23无法启用selinux保护

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值