今天写了一个canvas画图的象棋 。js基础不行,只画了个图,以后补充。。。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>chess</title>
<style>
canvas{
display: block;
margin:50px auto;
border:1px solid #EAC591;
border-radius: 20px;
box-shadow:2px 2px 30px #080808;
}
</style>
</head>
<body>
<canvas id="canvas" width="460" height="510"></canvas>
<script>
var chess = {}
chess.init = function () {
//获取上下文
var canvas = document.getElementById("canvas");
this.ctx = canvas.getContext("2d");
//初始化
this.padding= 30; //预留外边框的空隙
this.cell= 50; //棋盘空隙
this.chessRadius= 20; //棋子半径
this.fontSize= 36; //棋子文字大小
this.width=400; //棋盘的宽度 50*8
this.height= 450; //棋盘高度 50*9
this.offsetWidth = 460;
this.offsetHeight = 510;
this.array = [
["车","马","相","士","将","士","相","马","车"],
[" "," "," "," "," "," "," "," "," "],
[" ","炮"," "," "," "," "," ","炮"," "],
["兵"," ","兵"," ","兵"," ","兵"," ","兵"],
[" "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "],
["卒"," ","卒"," ","卒"," ","卒"," ","卒"],
[" ","炮"," "," "," "," "," ","炮"," "],
[" "," "," "," "," "," "," "," "," "],
["車","馬","象","仕","帅","仕","象","馬","車"] ]
this.init_back();
this.init_piece();
this.addEvent();
}
//棋盘初始化
chess.init_back =function () {
var p = this.padding;
var c = this.cell;
var w = this.width;
var h = this.height;
var ow =this.offsetWidth;
var oh = this.offsetHeight;
this.drawBg(0,0,ow,oh);
//画横线
for(var i=0;i<=9;i++){
this.drawLine(p,p+c*i,p+w,p+c*i)
}
//画上半部分竖线
for(var i =0;i<=8;i++){
this.drawLine(p+c*i,p,p+c*i,p+c*4)
}
//画下半部分竖线
for(var i =0;i<=8;i++){
this.drawLine(p+c*i,p +c*5,p+c*i,p+c*9)
}
//画上部分X
this.drawLine(p+c*3,p,p+c*5,p+c*2);
this.drawLine(p+c*5,p,p+c*3,p+c*2);
//画下部分X
this.drawLine(p+c*3,p+h,p+c*5,p+c*7);
this.drawLine(p+c*5,p+h,p+c*3,p+c*7);
//画#标记点
this.drawRound(p+c,p+c*2);
this.drawRound(p+c*7,p+c*2);
this.drawRound(p+c,p+c*7);
this.drawRound(p+c*7,p+c*7);
for(var i =0;i<=9;i++){
if(i%2!=1){
this.drawRound(p+c*i,p+c*3);
this.drawRound(p+c*i,p+c*6);
}
}
//画楚河汉界
this.drawText(p+c*2,p+c*4.5,"楚 河");
this.drawText(p+c*6,p+c*4.5,"汉 界"); } //棋子初始化
chess.init_piece = function () {
var p =this.padding;
var r = this.chessRadius;
var c = this.cell;
var a = this.array;
for(var i =0;i<a.length;i++){
for(var j=0;j<a[i].length;j++){
if(a[i][j] !=" "){
var t = a[i][j];
this.drawPiece(p+c*j,p+c*i,r,t);
}
}
}
}
//画背景
chess.drawBg =function (x,y,endX,endY) {
this.ctx.beginPath();
this.ctx.fillStyle = "#f9f9f9";
this.ctx.rect(x,y,endX,endY);
this.ctx.fill();
this.ctx.closePath();
} //画直线
chess.drawLine =function (x,y,endX,endY) {
this.ctx.beginPath();
this.ctx.lineWidth = 2;
this.ctx.strokeStyle = "#ff0000";
this.ctx.moveTo(x,y);
this.ctx.lineTo(endX,endY);
this.ctx.stroke();
this.ctx.closePath();
} //画标记点
chess.drawRound = function (x,y) {
var w = this.width;
var p = this.padding;
this.ctx.beginPath();
this.ctx.lineWidth = 2;
this.ctx.strokeStyle = "#ff0000"; if(x!=p){
//左上
this.ctx.moveTo(x-5,y-10);
this.ctx.lineTo(x-5,y-5);
this.ctx.lineTo(x-10,y-5);
//左下
this.ctx.moveTo(x-5,y+10);
this.ctx.lineTo(x-5,y+5);
this.ctx.lineTo(x-10,y+5);
}
if(x!=p+w){
//右上
this.ctx.moveTo(x+5,y-10);
this.ctx.lineTo(x+5,y-5);
this.ctx.lineTo(x+10,y-5);
//右下
this.ctx.moveTo(x+5,y+10);
this.ctx.lineTo(x+5,y+5);
this.ctx.lineTo(x+10,y+5); } this.ctx.stroke();
this.ctx.closePath();
} //写字
chess.drawText = function (x,y,name) {
this.ctx.font="28px 隶书"
this.ctx.fillStyle="#000";
this.ctx.textAlign="center";
this.ctx.textBaseline="middle";
this.ctx.fillText(name, x, y);
} //画单个棋子
chess.drawPiece =function (x,y,r,t) {
this.ctx.beginPath();
this.ctx.fillStyle = "#fff";
this.ctx.strokeStyle = "#ccc";
this.ctx.lineWidth =2;
this.ctx.arc(x,y,r,0,Math.PI*2,true);
this.ctx.fillText(t,x,y)
this.ctx.closePath();
this.ctx.fill();
this.ctx.stroke();
chess.drawText(x,y,t); } //画棋子的选中状态
chess.onPiece = function (x,y,r,t) {
this.ctx.beginPath();
this.ctx.strokeStyle ="#ff0000";
this.ctx.lineWidth =1;
this.ctx.moveTo(x-8,y-23);
this.ctx.lineTo(x-23,y-23);
this.ctx.lineTo(x-23,y-8); this.ctx.moveTo(x+8,y-23);
this.ctx.lineTo(x+23,y-23);
this.ctx.lineTo(x+23,y-8); this.ctx.moveTo(x-8,y+23);
this.ctx.lineTo(x-23,y+23);
this.ctx.lineTo(x-23,y+8); this.ctx.moveTo(x+8,y+23);
this.ctx.lineTo(x+23,y+23);
this.ctx.lineTo(x+23,y+8); this.ctx.stroke();
this.ctx.closePath(); this.ctx.beginPath();
this.ctx.fillStyle = "#fff";
this.ctx.strokeStyle = "#ccc";
this.ctx.lineWidth =2;
this.ctx.arc(x,y,r,0,Math.PI*2,true); this.ctx.shadowOffsetX = 1; // 阴影Y轴偏移
this.ctx.shadowOffsetY = 1; // 阴影X轴偏移
this.ctx.shadowBlur = 4; // 模糊尺寸
this.ctx.shadowColor = 'rgba(0, 0, 0, 1)'; // 颜色
this.ctx.fillText(t,x,y)
this.ctx.closePath();
this.ctx.fill();
this.ctx.stroke();
chess.drawText(x,y,t); } //增加点击事件
chess.addEvent = function () {
var _this = this;
canvas.addEventListener("click",function (event) {
var k = _this.getMousePos(event);
console.log(Math.round(k.x))
});
} //获取鼠标点击坐标
chess.getMousePos = function(event) {
var dx = canvas.getBoundingClientRect().left;
var dy = canvas.getBoundingClientRect().top;
var e = event || window.event;
var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
var x = e.pageX || e.clientX + scrollX;
var y = e.pageY || e.clientY + scrollY;
//alert('x: ' + x + '\ny: ' + y);
return { 'x': x-dx, 'y': y-dy };
} chess.init(); </script>
</body>
</html>

canvas象棋 画图的更多相关文章

  1. canvas 在线画图

    canvas 在线画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. 使用Canvas制作画图工具

      前  言 JRedu canvas是HTML5中重要的元素之一,canvas元素使用JavaScript在网页上绘制图像,画布是一个矩形区域,我们可以控制其每一个元素,并且canvas拥有多种的绘 ...

  3. 微信小程序 base64图片在canvas上画图

    上代码 wxml <canvas canvas-id="myCanvas" style="width:400px;height:400px;">&l ...

  4. html5 canvas 自定义画图裁剪图片

    html5 给我们带来了极大惊喜的canvas标签,有了它我们可以在浏览器客户端处理图片,不需要经过服务器周转.可以实现: 1.照片本地处理,ps有的一些基本功能都有 2.结合js可以实现一些很炫的动 ...

  5. canvas基本画图

    <img src="img/lamp.gif" id="lamp"/> <img src="img/eg_tulip.jpg&quo ...

  6. HTML Canvas 鼠标画图

    原文来自:http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app(已被墙) 译文: http: ...

  7. canvas防画图工具

    <style> body {   background: black;   text-align: center; } #cans {   background: white; } < ...

  8. html5 canvas 实现简单的画图

    今天早上看了一下 canvas 前端画图,数据可视化, 百度的 echart.js  , d3等 js 库都已经提供了强大的绘制各种图形的 API. 下面记录一下 有关canvas 绘图的基本知识: ...

  9. 兼容小程序的canvas画图组件jmGraph

    基于CANVAS的简单画图组件让你用类似于dom的方式,在canvas上画图,感觉会不会很爽. 主页:http://graph.jm47.com/示例:http://graph.jm47.com/ex ...

随机推荐

  1. Tensorflow机器学习入门——读取数据

    TensorFlow 中可以通过三种方式读取数据: 一.通过feed_dict传递数据: input1 = tf.placeholder(tf.float32) input2 = tf.placeho ...

  2. acm数论之旅(转载)---最大公约数与最小公倍数

    gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •̀∀•́ ) ...

  3. web.xml中的welcome-file-list标签作用

    welcome-file-list是一个配置在web.xml中的一个欢迎页,用于当用户在url中输入项目名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面. ...

  4. 【转】shell处理mysql增删改查

    这几天做一个任务,比对两个数据表中的数据,昨天用PHP写了一个版本,但考虑到有的机器没有php或者php没有编译mysql扩展,就无法使用mysql系列的函数,脚本就无效了,今天写个shell版本的, ...

  5. @ResponseBody是如何起作用的

    前言 最近参与的项目中,接口中返回的日期格式不对,发现项目中配置了fastjson作为spring的数据转换器,于是使用了fastjson的字段格式化转换注解 发现不起作用.这让我很疑惑,然后在fas ...

  6. 谈一谈并查集QAQ(上)

    最近几日理了理学过的很多oi知识...发现不知不觉就有很多的知识忘记了... 在聊聊并查集的时候顺便当作巩固吧.... 什么是并查集呢? ( Union Find Set ) 是一种用于处理分离集合的 ...

  7. Python笔记3

    类格式示例: class Student(): name = '' age = 0 def print_file(self): print('name:' + self.name) print('ag ...

  8. CSS背景图片设置

    *{ margin:0px; padding:0px; list-style: none; text-decoration: none; font-family: Arial,'Microsoft Y ...

  9. 操作系统OS,Python - 多进程(multiprocessing)、多线程(multithreading)

    多进程(multiprocessing) 参考: https://docs.python.org/3.6/library/multiprocessing.html 1. 多进程概念 multiproc ...

  10. 【协作式原创】自己动手写docker之run代码解析

    linux预备知识 urfave cli预备知识 准备工作 阿里云抢占式实例:centos7.4 每次实例释放后都要重新安装go wget https://dl.google.com/go/go1.1 ...