一个在h5的canvas元素中画扑克牌jquery插件实现

  1 //非架构
  2 ; (function ($) {
  3 var aspect = 5.7 / 8.8;//扑克宽和高比例
  4 function PaintBoder(canv, x, y, h,poker) {
  5 var boder = new Boder(canv, h);
  6 boder.X = x;
  7 boder.Y = y;
  8 var c = canv.getContext("2d");
  9 c.save()
 10 c.lineWidth = 1;
 11 boder.Y = y + c.lineWidth;
 12 c.strokeStyle = "green";
 13 c.fillStyle = "white";
 14 if (poker.name.length==0) {
 15 c.fillStyle = "darkgrey";
 16 c.strokeStyle = "white";
 17 }
 18 c.beginPath();
 19 //画top boder
 20 var r = boder.width / 8;
 21 c.moveTo(boder.X + r, boder.Y);
 22 c.lineTo(boder.X + boder.width - r, boder.Y);
 23 //画top right 圆角
 24 c.arcTo(boder.X + boder.width, boder.Y, boder.X + boder.width, boder.Y + r, r)
 25 //left boder
 26 c.lineTo(boder.X + boder.width, boder.Y + boder.height - r);
 27 //bottom left radius
 28 c.arcTo(boder.X + boder.width, boder.Y + boder.height, boder.X + boder.width - r, boder.Y + boder.height, r)
 29 //bottom boder
 30 c.lineTo(boder.X + r, boder.Y + boder.height);
 31 //bottom right radius
 32 c.arcTo(boder.X, boder.Y + boder.height, boder.X, boder.Y + boder.height - r, r)
 33 //right boder
 34 c.lineTo(boder.X, boder.Y + r);
 35 //top right radius
 36 c.arcTo(boder.X, boder.Y, boder.X + r, boder.Y, r);
 37 c.closePath();
 38 c.stroke();
 39 c.fill();
 40 c.restore();
 41 return boder;
 42 }//画边框
 43 function PaintCardColor(boder, Poker, deg) {
 44 //字体大小
 45 var fontSize = boder.height / 2.5;
 46 //颜色
 47 var fontColor = GetPaintColor(Poker);
 48 //点数
 49 var colorImage = "";
 50 switch (Poker.CardColor) {
 51 case"s" :
 52 colorImage = "?";
 53 break;
 54 case "h" :
 55 colorImage = "?";
 56 break;
 57 case "d" :
 58 colorImage = "?";
 59 break;
 60 case "c":
 61 colorImage = "?";
 62 break;
 63 case "z":
 64 colorImage = "?";
 65 case "y":
 66 colorImage = "?";
 67 break;
 68 default:
 69 colorImage = "";
 70 }
 71 var canvasContext = boder.canvasContext;
 72 var fontSize = boder.width * 0.4;
 73 var x = boder.X + 2;
 74 var y = boder.Y + boder.width * 0.4;
 75 canvasContext.font = (fontSize + "px Arial");
 76 canvasContext.textAlign = "left";
 77 canvasContext.textBaseline = "hanging";
 78 canvasContext.fillStyle = fontColor;
 79 canvasContext.fillText(colorImage, x, y);
 80 }
 81 function PaintName(boder, Poker) {
 82 //字体大小
 83 var fontSize = boder.width * 0.4;
 84 var fontWeight = "normal";
 85 if (Poker.name=="10") {
 86 fontSize = boder.width * 0.35;
 87 fontWeight = "bolder";
 88 }
 89
 90 //颜色
 91 var fontColor = GetPaintColor(Poker);
 92 //点数
 93 var name = Poker.name;
 94 //画布
 95 var canvasContext = boder.canvasContext;
 96 //位置
 97
 98 var x = boder.X+1;
 99 var y = boder.Y+4;
100 canvasContext.font = (fontWeight+" "+fontSize + "px Arial");
101 canvasContext.textAlign = "left";
102 canvasContext.textBaseline = "hanging";
103 canvasContext.fillStyle = fontColor;
104 canvasContext.fillText(name, x, y);
105 }//画点数
106 function GetPaintColor(Poker) {
107 var color = "red";
108 switch (Poker.CardColor) {
109 // "h", "s", "d", "c" ;//hearts红桃、spades黑桃、diamonds梅花,clubs方块
110 case "s": color = "black"; break;
111 case "d": color = "black"; break;
112 case "y": color = "black"; break;
113 default: color = "red"
114 }
115 return color;
116 }//画花色
117 function Boder(canv, h) {
118 this.canvasContext = canv.getContext("2d");
119 this.height = h;
120 this.width = this.height * aspect ;
121 this.X;
122 this.Y;
123 }//边框对象
124 var drawPoker = function (canv,x, y, poker,h,deg) {
125 var context = canv.getContext("2d");
126 context.save();
127 //取中心
128 var centerx = x + h * aspect / 2;
129 var centery = y + h / 2;
130 context.globalCompositeOperation = "source-over";//source-over
131 context.translate(centerx, centery);
132 //旋转
133 context.rotate(deg* Math.PI / 180);
134 context.translate(-centerx, -centery);
135 //还原中心
136 //画边框
137
138 var boder = PaintBoder(canv, x, y, h, poker);
139 PaintName(boder, poker);
140
141 PaintCardColor(boder, poker, deg);
142
143 context.translate(centerx, centery);
144 context.rotate(180 * Math.PI / 180);
145 context.translate(-centerx, -centery);
146 PaintName(boder, poker);
147 PaintCardColor(boder, poker, deg + 180);
148 //context.translate(-centerx, -centery);
149 context.restore();
150
151 };
152 function toRight (pokers, pokerHeight, deg, pile, x, y,canv) {
153 var pokerWidth = pokerHeight*aspect;
154 var space = pokerWidth * 0.7;
155 var xx = x;
156 var yy = y;
157 $.each(pokers, function (i, n) {
158 drawPoker(canv, xx, yy, n, pokerHeight, deg);
159 if (pile == true) {
160 xx = xx + space;
161 } else {
162 xx = xx + pokerWidth * 1.1;
163 }
164 })
165
166 };
167 function toBottom(pokers, pokerHeight, deg, pile, x, y, canv) {
168 var pokerWidth = pokerHeight * aspect;
169 var space = pokerWidth * 0.7;
170 var xx = x;
171 var yy = y;
172 $.each(pokers, function (i, n) {
173 drawPoker(canv, xx, yy, n, pokerHeight, deg);
174 if (pile == true) {
175 yy = yy + space;
176 } else {
177 yy = yy + pokerWidth * 1.1;
178 }
179 })
180
181 };
182 var methods = {
183 init: function (options) {
184 var defaults = {
185 pokers: [{ name: "error", CardColor: "h" }],
186 pokerHeight: 75,
187 deg: 0,
188 pile: true,
189 x: 0,
190 y: 0,
191 }
192 var settings = $.extend({}, defaults, options);
193 if (settings.direction=="toRight") {
194 toRight(settings.pokers,
195 settings.pokerHeight,
196 settings.deg,
197 settings.pile,
198 settings.x,
199 settings.y,
200 this.get(0))
201 } else if (settings.direction == "toBottom") {
202 toBottom(settings.pokers,
203 settings.pokerHeight,
204 settings.deg,
205 settings.pile,
206 settings.x,
207 settings.y,
208 this.get(0))
209 } else {
210 toRight(settings.pokers,
211 settings.pokerHeight,
212 settings.deg,
213 settings.pile,
214 settings.x,
215 settings.y,
216 this.get(0))
217 }
218
219 },
220 toRight: function (pokers, pokerHeight, deg, pile, x, y) {
221 var canv = this.get(0);
222 toRight(pokers, pokerHeight, deg, pile, x, y, canv);
223 },
224 toBottom: function (pokers, pokerHeight, deg, pile, x, y) {
225 var canv = this.get(0);
226 toBottom(pokers, pokerHeight, deg, pile, x, y, canv);
227 }
228 };
229 $.fn.drawPoker = function (direction,pokers, pokerHeight, deg, pile, x, y) {
230 var method = arguments[0];
231
232 if (methods[method]) {
233 method = methods[method];
234 arguments = Array.prototype.slice.call(arguments, 1);
235 } else if (typeof (method) == ‘object‘ || !method) {
236 method = methods.init;
237 } else {
238 $.error(‘Mehod ‘ + method + ‘ does not exist on this object‘);
239 return this;
240 }
241 return method.apply(this, arguments);
242 };
243 })(jQuery);
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
 6 <script src="./jquery-ant-canvas-poker/jquery.ant.canvas.poker.js"></script>
 7 </head>
 8 <body>
 9 <canvas id="canv" width="500" height="500" style="border:2px solid red;"></canvas>
10 <input type="button" id="toRight" value="横排"/>
11 <input type="button" id="toBottom" value="竖排" />
12 <input type="button" id="dispersale" value="分散" />
13 <input type="button" id="rotation" value="旋转" />
14 <script>
15 var testpoker = [
16 { name: 1, CardColor: "s" },
17 { name: 2, CardColor: "h" },
18 { name: "", CardColor: "" },
19 { name: "", CardColor: "" },
20 { name: "", CardColor: "" },
21 { name: 10, CardColor: "d" },
22 { name: "J", CardColor: "c" },
23 { name: "Q", CardColor: "s" },
24 { name: "A", CardColor: "s" },
25 { name: "X", CardColor: "y" },
26 { name: "XX", CardColor: "z" }
27 ];
28 function toRight() {
29 $("#canv").drawPoker({ direction: "toRight", pokers: testpoker });
30 }
31 function toBottom() {
32 $("#canv").drawPoker({ direction:"toBottom", pokers: testpoker,x:200 });
33 }
34 function dispersale() {
35 $("#canv").drawPoker({ direction: "toRight", pokers: testpoker, pokerHeight: 70, deg:0, pile: false, y:400 });
36 }
37 function rotation() {
38 $("#canv").drawPoker({ direction: "toRight", pokers: testpoker, pokerHeight:70, deg:60, pile:true, y:300 });
39 }
40 $(function () {
41 $("#toRight").on("click", toRight);
42 $("#toBottom").on("click", toBottom);
43 $("#dispersale").on("click", dispersale);
44 $("#rotation").on("click", rotation);
45 })
46 </script>
47 </body>
48 </html>

 

时间: 2024-08-01 10:46:05

一个在h5的canvas元素中画扑克牌jquery插件实现的相关文章

无法更新 EntitySet“W_ReceiveData”,因为它有一个 DefiningQuery,而 &lt;ModificationFunctionMapping&gt; 元素中没有支持当前操作的 &lt;InsertFunction&gt; 元素。

无法更新 EntitySet“W_ReceiveData”,因为它有一个 DefiningQuery,而 <ModificationFunctionMapping> 元素中没有支持当前操作的 <InsertFunction> 元素. 原因:未为表定义主键! 去sql server中为表添加主键并更新edmx文件

一个友好的文本框内显示提示语 jquery 插件

插件实现文本框内默认显示提示语,当文本框获得焦点时提示语消失. 如果没有输入或输入为空则失去焦点时提示语再次出现. 同时它的使用非常舒适简单,引入插件及 jquery 后,在原有的文本框内加上样式类(class="prompt-input")以及设置值(value="Your prompt")为提示语就可以了. 像这样: 1 <input class="prompt-input" type="text" value='Y

pasteimg浏览器中粘贴图片jQuery插件

pasteimg是一款可以在浏览器中实现图片粘贴的jQuery插件,兼容Chrome.Firefox.IE11以及其他使用这些内核的浏览器,比如,国内著名的360浏览器. pasteimg可以识别浏览器中直接复制的图片,也可以识别复制的富文本中的图片.仅仅可以识别在浏览器中复制的内容,操作系统中复制的图片是不能识别的. pasteimg依赖jQuery,简单易用,引入jQuery和paste.image.js后,调用方式如下: 1 //在需要监听粘贴事件的dom元素上调用pasteImage方法

无法更新 EntitySet“SoreInfo_Table”,因为它有一个 DefiningQuery,而 &lt;ModificationFunctionMapping&gt; 元素中没有支持当前操作的 &lt;InsertFunction&gt; 元素。

1:实体中的表必须有主键(这里指示T_User表中必须有主键),如果没有,会有这样的提示 2:主键设置好后,运行还是会出现类似问题,那就一个郁闷 1):方法一先从EF中删除刚设置主键的模型,然后再重新添加到EF中,That's Ok. 2):如果方法一都没有成功,那么用这个更土的方法:创新创建一个工程,再重新关联EF,这时候这个问题就解决了或者删除*.edmx文件,再重新关联数据库也OK都测试过,完全通过

canvas元素大小与绘图表面大小

原文链接:canvas总结:元素大小与绘图表面大小 前言 我们使用canvas的时候一般在canvas元素中直接设置它的width和height: 1 <canvas id="myCanvas" width="300" height="150">browser don't support canvas</canvas> 当然,也可以不在canvas中进行设置,直接在css样式中设置,因为canvas本身也是一个html节点

JQueryUI-拖动(Draggable)-在DOM 元素中约束运动

定义和用法 通过定义 draggable 区域的边界来约束每个 draggable 的运动,使用 containment 选项来指定一个父级的 DOM 元素或者一 个 jQuery 选择器 示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge&

50个jQuery插件可将你的网站带到另一个高度

Web领域一直在发生变化并且其边界在过去的每一天都在发生变化(甚至不能以小时为计),随着其边界的扩展取得了许多新发展.在这些进步之中,开发者的不断工作创造了更大和更好的脚本,这些脚本以插件方式带来更好的终端用户体验,它们比原来更轻量级,还有更强的处理能力. 关键是这些新发展起来的脚本和插件是能构建响应式Web的,而且还不会丧失它们原有的功能特性——除了更优秀和更轻巧(就文件大小而言)之外,它们还不会增加页面加载的时间. 通过浏览文档,掌握JQuery的语法是很容易的.它可以支持选择DOM元素,创

基于HTML5 canvas圆形倒计时器jQuery插件

这是一款基于html5 canvas的圆形倒计时器jQuery插件.它可以使你非常轻松的创建圆形的倒计时器.该jQuery倒计时器插件有12种themes,它们基于 HTML5 canvas 来渲染各种圆环. 这个jQuery环形倒计时器插件依赖于jquery.knob.js和jquery.throttle.js两个外部插件. 在线演示:http://www.htmleaf.com/Demo/201502111367.html 下载地址:http://www.htmleaf.com/html5/

HTML5中的&lt;canvas&gt;画布:使用canvas元素在网页上绘制四分之一圆(3)

前几天自己做了个四分之一的圆,放到手机里面测试.效果不是很好.于是今天通过查资料,找到了canvas.自己研究了一天,发现可以使用canvas画圆.代码如下: 1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 7 </head> 8 <body&g