股票场内基金交易,没时间盯盘?
原文地址: http://www.sitepoint.com/introduction-to-jcanvas-jquery-meets-html5-canvas/
HTML5 让你可以通过 canvas 元素以及相关的 JavaScript API 直接在网页中画图。
在这片文章中,我会想你介绍 jCanvas,针对 HTML5 的 Canvas API 的一个基于 jQuery 的开源库。
如果你使用 jQuery 开发,jCanvas 可以更容易和更快的使用 jQuery 语法构建 Canvas 画图和交互式应用。
jCanvas 是什么
这是 jCanvas 官网的解释:
jCanvas 是一个 JavaScript 库,使用 jQuery 编写 并用于jQuery ,围绕 HTML5 的 canvas API ,增加了一些新的特性和功能,其中有很多是定制的,这些功能包含 图层、事件、拖放、动画等等。
总之是一个语法糖丰富的灵活的 API ,jQuery 式的语法使操作 HTML5 canvas 更加简单。
原生的 Canvas API 能做的事情,jCanvas 都可以做到,并且做的更多。如果你愿意,你也可以将 jCanvas 和原生的 Canvas API 方法混合使用,draw() 方法就是为了这个目的。此外,你也可以使用 jCanvas 提供的更多属性创建你自己的方法。
添加 jCanvas 到项目中
要引入 jCanvas 在你的工程中,你可以从官网或 Github 下载脚本,然后在你的工程中引入。正如之前提到的,jCanvas 需要使用 jQuery,所以也一定要引入 jQuery。
项目的脚本看起来应该是这样的:
1 2 3 4 |
<script src="js/jquery.min.js></script> <script src="js/jcanvas.min.js></script> <script src="js/script.js></script> |
设置 HTML 文件。
跟着这个例子,在 HTML5 文件中添加一个 canvas 元素标签。
1 2 3 4 |
<canvas id="myCanvas" width="600" height="300"> <p>这里放一些备用的内容,因为有些浏览器不支持 Canvas </p> </canvas> |
这里是上面的代码几个关键的地方:
-
默认情况下,canvas 绘图的大小是 300×500 px。你可以修改大小通过更改元素中的 width 和 height 属性。
-
添加一个 ID 不是必须的,但是会让你使用 JavaScript 访问这个元素时更加简单。
-
canvas 元素里面的内容只是一个位图图像。这使得它无法让使用辅助技术的用户使用。另外,不支持 Canvas API 的浏览器将无法访问其内容。因此,在 canvas 还没有被普及的时候,推荐做法是为这类用户添加一些备用内容。
如果你想使用原生的 Canvas API,你的 JavaScript 代码将是这个样子:
1 2 3 |
var canvas = document.getElementById(myCanvas), context = canvas.getContext('2d' ); |
其中的 context 变量设置了 Canvas 对象的 2d 属性;正是通过这个属性。使你可以访问 HTML5 的 Canvas API 提供的其他属性和方法。
如果你想了解更多,你可以查看 HTML5 Canvas 教程:介绍 。
jCanvas 方法和属性已经包含了 2D context 的引用。因此你可以跳过这一步,直接绘图。
用 jCanvas 绘制形状
大多数 jCanvas 方法接受一个 属性:值 形式的参数。你可以按照你喜欢的顺序排列这些属性。
让我们开始画一个 矩形。
矩形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//存放画布对象到一个变量 VAR $ myCanvas = $ ('#myCanvas' ); // 长方形 $myCanvas . drawRect ( { fillStyle : 'steelblue' ,//设置矩形的背景色 strokeStyle : 'blue' ,//设置边框颜色 strokeWidth : 4 ,//设置边框宽度 x : 150 , y : 100 ,//设置矩形在 canvas 中的位置 fromCenter : false ,//为了改变 x,y 坐标,设置 fromCenter 属性为 false width : 200 ,//宽 height : 100 //高 } ) ; |
下面是矩形的演示:
圆弧和圆
弧是一个圆的边缘部分。在 jCanvas 中,绘制弧形只需在 drawArc() 方法中设置一些属性值。
1 2 3 4 5 6 7 8 9 10 |
$myCanvas.drawArc({ strokeStyle: 'steelblue', strokeStyle: 'blue', strokeWidth: 4, x: 300, y: 100, radius: 50, // 开始和结束时的角度 start: 0, end: 200 }); |
绘制弧包括设置一个半径和圆弧开始和结束的角度。如果你希望圆弧的方向是逆时针,你可以添加一个 ccw 属性,并将其设置为 true。
下面是演示:
要想绘制一个圆形,只需忽略掉 drawArc() 方法中的 start 和 end 属性即可。
举例,这里是如何只用圆弧画一个笑脸。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
$myCanvas.drawArc({ // draw the face fillStyle: 'yellow', strokeStyle: '#333', strokeWidth: 4, x: 300, y: 100, radius: 80 }).drawArc({ // draw the left eye fillStyle: '#333', strokeStyle: '#333', x: 250, y: 70, radius: 5 }).drawArc({ // draw the right eye fillStyle: '#333', strokeStyle: '#333', x: 350, y: 70, radius: 5 }).drawArc({ // draw the nose strokeStyle: '#333', strokeWidth: 4, ccw: true, x: 300, y: 100, radius: 30, start: 0, end: 200 }).drawArc({ // draw the smile strokeStyle: '#333', strokeWidth: 4, x: 300, y: 135, radius: 30, start: 90, end: 280 }); |
请牢记 jCanvas 是基于 jQuery 的,务必引入 jQuery。
这里是上面的代码的示例:
画线和路径
你可以快速画线和点与点之间的连接,通过使用 jCanvas 的 drawLine() 方法。
1 2 3 4 5 6 7 8 9 10 11 |
$myCanvas.drawLine({ strokeStyle: 'steelblue', strokeWidth: 10, rounded: true, closed: true, x1: 100, y1: 28, x2: 50, y2: 200, x3: 300, y3: 200, x4: 200, y4: 109 }); |
上面的代码设置 rounded 和 closed 属性的值为真,因此,使得线条没有角度并且闭合。
还可以绘制更灵活的路径通过使用 drawPath() 方法。想象多个直线、圆弧、曲线或矢量路径连接起来的图形。
drawPath() 方法除了接收每个点 x y 坐标之外,你还要指定绘制的路径的类型,如:直线、圆弧等。
下面这个例子演示了绘制箭头:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
$myCanvas.drawPath({ strokeStyle: '#000', strokeWidth: 4, x: 10, y: 10, p1: { type: 'line', x1: 100, y1: 100, x2: 200, y2: 100 }, p2: { type: 'line', rounded: true, endArrow: true, arrowRadius: 25, arrowAngle: 90, x1: 200, y1: 100, x2: 290, y2: 100 }, p3: { type: 'line', rounded: true, endArrow: true, arrowRadius: 25, arrowAngle: 90, x1: 100, y1: 100, x2: 100, y2: 250 } }); |
绘制文本
你可以快速的绘制文本,通过使用 drawText() 方法。你需要设置这些主要属性:
- text
- fontSize
- fontFamily
示例:
1 2 3 4 5 6 7 8 9 10 |
$myCanvas.drawText({ text: 'Canvas is fun', fontFamily: 'cursive', fontSize: 40, x: 290, y: 150, fillStyle: 'lightblue', strokeStyle: 'blue', strokeWidth: 1 }); |
绘制图片
你可以导入和处理图像通过 drawImage() 方法。下面是例子:
1 2 3 4 5 6 7 8 9 |
$myCanvas . drawImage ( { source : 'imgs/cat.jpg' , x : 250 , y : 100 , fromCenter : false , shadowColor : '#222' , shadowBlur : 3 , rotate : 40 } ) ; |
想获得去掉 5 元限制的证券账户吗?

如果您想去掉最低交易佣金 5 元限制,使用微信扫描左边小程序二维码,访问微信小程序「优财助手」,点击底部菜单「福利」,阅读文章「通过优财开证券账户无最低交易佣金 5 元限制」,按照文章步骤操作即可获得免 5 元证券账户,股票基金交易手续费率万 2.5。
请注意,一定要按照文章描述严格操作,如错误开户是无法获得免 5 元证券账户的。