https://github.com/caleb531/jcanvas

HTML5 可以直接在你的网页中使用 <canvas> 元素及其相关的 JavaScript API绘制的图形。

在这篇文章中,我将向你介绍 jCanvas,一个基于 jQuery的免费且开源的 HTML5的Canvas API。

如果你使用 jQuery 进行开发,jCanvas能够使用 jQuery更简单,更快速的完成一些非常炫酷的 canvas画布及交互效果。

什么是 jCanvas ?

jCanvas 官网是这样解释的:

“jCanvas is a JavaScript library, written using jQuery and for jQuery, that wraps around the HTML5 canvas API, adding new features and capabilities, many of which are customizable. Capabilities include layers, events, drag-and-drop, animation, and much more.

The result is a flexible API wrapped up in a sugary, jQuery-esque syntax that brings power and ease to the HTML5 canvas. ”

jCanvas 能让你做的一切事情,你都可以用原生的Canvas API来实现,甚至可以做更多的事情。如果你愿意的话,你也可以将原生的Canvas API方法和 jCanvas一起使用。draw()方法就可以这样使用。此外,你还可以非常轻松的用自己的方法结合 extend()函数来扩展jCanvas的功能。

添加jCanvas 到你的项目中

将jCanavs添加在你的项目中,从官方网站或GitHub的页面下载脚本,然后将脚本文件放在你的项目文件夹中。正如前面说的,jCanvas需要依赖 jQuery才能正常工作,所以还要确保引入了 jQuery文件。

项目的脚本文件将是这个样子:

  1. <script src="js/jquery.min.js></script>
  2. <script src="js/jcanvas.min.js></script>
  3. <script src="js/script.js></script>

最后,引入你自己的JavaScript 代码文件。现在,让我们开始jCanvas之旅吧。

设置 HTML文档

我们通过为 HTMl5文档添加一个<canvas>标签,来开始我们的示例。

  1. <canvas id="myCanvas" width="600" height="300">
  2. <p>This is fallback content
  3. for users of assistive technologies
  4. or of browsers that don't have
  5. full support for the Canvas API.</p>
  6. </canvas>

以下是关于上面的代码片段的几点说明。

  1. 默认情况下,<canvas>的尺寸300px x 150px,你可以在width 和 height 属性里修改默认的大小。

  2. id属性不是必须添加的,但是确是 JavaScript访问该元素的最简单的方法。

  3. 在<canvas>元素中的内容只是位图,这使得它无法被使用辅助技术的用户访问。另外,对不支持 Canvas API的浏览器,将不能够访问其内容或者任何方式的交互。因此,该技术旨在让<canvas>更容易被支持

如果你想使用原生的Canvas API,你的 JavaScript 代码将会这样的:

  1. var canvas = document.getElementById('myCanvas'),
  2. context = canvas.getContext('2d');

上述代码中的context变量存储了Canvas对象的一个2D上下文属性。正是这种特性,使得你可以访问 HTML5的 Canvas API提供的所有其他属性和方法。

如果你想了解的更多,你可以戳这里HTML5 Canvas 简介

jCanvas的方法和属性已经包含了2D上下文的引用,因此你可以直接的跳到绘制图片。

用jCanvas绘制一些图形

大多数的 jCanvas方法,接受键值对的形式,因此你可以根据你的需要,或你喜欢的顺序去使用它们。

让我们从绘制一个矩形开始吧。

矩形

下面是你怎样用 jCanvas对象的 drawRect() 方法绘制出一个矩形的方法。

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. // rectangle shape
  5. $myCanvas.drawRect({
  6. fillStyle: 'steelblue',
  7. strokeStyle: 'blue',
  8. strokeWidth: 4,
  9. x: 150, y: 100,
  10. fromCenter: false,
  11. width: 200,
  12. height: 100
  13. });

上面的代码片段表示,存储 Canvas对象到一个名为$myCanvas的变量中。里面的drawRect()方法的属性都是比较简单的,但是我们在这里简单的阐述一下:

  1. fillStyle 设置矩形的背景色;

  2. strokeStyle 设置它的边框颜色;

  3. strokeWidth 设置矩形的边框宽度;

  4. x 和 y设置对应矩形的坐标的水平和垂直的画布内测的位置。顶点的0值的分别为 x和y,也就是说,(0,0),对应于画布的左上角。x坐标向右增大,y坐标朝向画布的底部增加。默认情况下,jCanvas会以矩形的中心点作为x和y坐标的值;

  5. 要想改变这一点,以便x和y对应矩形的左上角,可以将fromCenter属性的值设置为 false;

  6. 最后,通过宽度和高度属性设置矩形的尺寸。

下面是矩形的示例代码:

HTML:

  1. <h2>jCanvas example: Rectangle</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. // rectangle shape
  5. $myCanvas.drawRect({
  6. fillStyle: 'steelblue',
  7. strokeStyle: 'blue',
  8. strokeWidth: 4,
  9. x: 190,
  10. y: 50,
  11. fromCenter: false,
  12. width: 200,
  13. height: 100
  14. });

Result:

jCanvas example: Rectangle

圆弧和圆

弧是一个圆的边缘部分。对于jCanvas来说,画一个圆弧仅仅是在 drawArc() 方法里设置几个所需的属性:

  1. $myCanvas.drawArc({
  2. strokeStyle: 'steelblue',
  3. strokeStyle: 'blue',
  4. strokeWidth: 4,
  5. x: 300, y: 100,
  6. radius: 50,
  7. // start and end angles in degrees
  8. start: 0, end: 200
  9. });

绘制弧形,需要设置半径属性的值,以及开始的角度和结束的角度。如果你希望弧形是逆时针方向的话,需要添加一个ccw属性,并将其属性值设置为true。

下面是上述代码块演示:

HTML:

  1. <h2>jCanvas example: Arc</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawArc({
  5. strokeStyle: 'steelblue',
  6. strokeStyle: 'blue',
  7. strokeWidth: 4,
  8. x: 300, y: 100,
  9. radius: 50,
  10. // start and end angles in degrees
  11. start: 0, end: 200
  12. });

Result:

jCanvas example: Arc

绘制一个圆形:

举例来说,下面是如何只使用圆弧形状来绘制出一个简单的笑脸图形:

  1. $myCanvas.drawArc({
  2. // draw the face
  3. fillStyle: 'yellow',
  4. strokeStyle: '#333',
  5. strokeWidth: 4,
  6. x: 300, y: 100,
  7. radius: 80
  8. }).drawArc({
  9. // draw the left eye
  10. fillStyle: '#333',
  11. strokeStyle: '#333',
  12. x: 250, y: 70,
  13. radius: 5
  14. }).drawArc({
  15. // draw the right eye
  16. fillStyle: '#333',
  17. strokeStyle: '#333',
  18. x: 350, y: 70,
  19. radius: 5
  20. }).drawArc({
  21. // draw the nose
  22. strokeStyle: '#333',
  23. strokeWidth: 4,
  24. ccw: true,
  25. x: 300, y: 100,
  26. radius: 30,
  27. start: 0,
  28. end: 200
  29. }).drawArc({
  30. // draw the smile
  31. strokeStyle: '#333',
  32. strokeWidth: 4,
  33. x: 300, y: 135,
  34. radius: 30,
  35. start: 90,
  36. end: 280
  37. });

请记住,jCanvas是基于jQuery的,因此,你可以像jQuery的链式操作一样,在jCanvas中也可以使用链式操作。

下面是以上代码在浏览器中的效果:

HTML:

  1. <h2>jCanvas example: Smiling Face</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawArc({
  5. // draw the face
  6. fillStyle: 'yellow',
  7. strokeStyle: '#333',
  8. strokeWidth: 4,
  9. x: 300, y: 100,
  10. radius: 80
  11. }).drawArc({
  12. // draw the left eye
  13. fillStyle: '#333',
  14. strokeStyle: '#333',
  15. x: 250, y: 70,
  16. radius: 5
  17. }).drawArc({
  18. // draw the right eye
  19. fillStyle: '#333',
  20. strokeStyle: '#333',
  21. x: 350, y: 70,
  22. radius: 5
  23. }).drawArc({
  24. // draw the nose
  25. strokeStyle: '#333',
  26. strokeWidth: 4,
  27. ccw: true,
  28. x: 300, y: 100,
  29. radius: 30,
  30. start: 0,
  31. end: 200
  32. }).drawArc({
  33. // draw the smile
  34. strokeStyle: '#333',
  35. strokeWidth: 4,
  36. x: 300, y: 135,
  37. radius: 30,
  38. start: 90,
  39. end: 280
  40. });

Result:

jCanvas example: Smiling Face

绘制线条和路径

你可以用drawLine()方法快速的绘制直线,或者定义一系列的线条的连接点。

  1. $myCanvas.drawLine({
  2. strokeStyle: 'steelblue',
  3. strokeWidth: 10,
  4. rounded: true,
  5. closed: true,
  6. x1: 100, y1: 28,
  7. x2: 50, y2: 200,
  8. x3: 300, y3: 200,
  9. x4: 200, y4: 109
  10. });

上面代码设置了 rounded和closed属性的值为true,从而所绘制的线和角都是闭合的。

HTML:

  1. <h2>jCanvas example: Connected lines</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawLine({
  5. strokeStyle: 'steelblue',
  6. strokeWidth: 10,
  7. rounded: true,
  8. closed: true,
  9. x1: 100,
  10. y1: 28,
  11. x2: 50,
  12. y2: 200,
  13. x3: 300,
  14. y3: 200,
  15. x4: 200,
  16. y4: 109
  17. });

Result:

jCanvas example: Connected lines

还可以使用drawPath()方法绘制路径。

该drawPath()方法设置 x 和 y值,你还需要制定你要绘制的路径的类型,例如直线,圆弧等。

下面教你如何使用 drawPath()方法和drawarrows()方法画出一对水平和垂直方向的箭头,后者是一个非常好用的jCanvas方法,能够使你快速的在画布上绘制一个箭头形状:

  1. $myCanvas.drawPath({
  2. strokeStyle: '#000',
  3. strokeWidth: 4,
  4. x: 10, y: 10,
  5. p1: {
  6. type: 'line',
  7. x1: 100, y1: 100,
  8. x2: 200, y2: 100
  9. },
  10. p2: {
  11. type: 'line',
  12. rounded: true,
  13. endArrow: true,
  14. arrowRadius: 25,
  15. arrowAngle: 90,
  16. x1: 200, y1: 100,
  17. x2: 290, y2: 100
  18. },
  19. p3: {
  20. type: 'line',
  21. rounded: true,
  22. endArrow: true,
  23. arrowRadius: 25,
  24. arrowAngle: 90,
  25. x1: 100, y1: 100,
  26. x2: 100, y2: 250
  27. }
  28. });

结果展示:

HTML:

  1. <h2>jCanvas example: Connected Arrows</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawPath({
  5. strokeStyle: '#000',
  6. strokeWidth: 4,
  7. x: 10, y: 10,
  8. p1: {
  9. type: 'line',
  10. x1: 100, y1: 100,
  11. x2: 200, y2: 100
  12. },
  13. p2: {
  14. type: 'line',
  15. rounded: true,
  16. endArrow: true,
  17. arrowRadius: 25,
  18. arrowAngle: 90,
  19. x1: 200, y1: 100,
  20. x2: 290, y2: 100
  21. },
  22. p3: {
  23. type: 'line',
  24. rounded: true,
  25. endArrow: true,
  26. arrowRadius: 25,
  27. arrowAngle: 90,
  28. x1: 100, y1: 100,
  29. x2: 100, y2: 250
  30. }
  31. });

Result:

jCanvas example: Connected Arrows

绘制文本

你可以使用drawText()方法快速的绘制出你需要的文字,这个方法的主要的功能:

  1. text:将此属性设置为你想要显示在画布上的文字内容:例如:‘Hello World’

  2. fontsize:此属性的值决定了在画布上的文字的大小。你可以为这个属性设置为一个数字,jCanvas默认为像素。另外,你也可以使用pt,但是在这种情况下,你需要用引号将属性值包括起来

  3. fontFamily:允许你指定您的文字图像的字体:'Verdana, sans-serif'。

这里的示例代码:

  1. $myCanvas.drawText({
  2. text: 'Canvas is fun',
  3. fontFamily: 'cursive',
  4. fontSize: 40,
  5. x: 290, y: 150,
  6. fillStyle: 'lightblue',
  7. strokeStyle: 'blue',
  8. strokeWidth: 1
  9. });

在浏览器中将是这样的效果:

HTML:

  1. <h2>jCanvas example: Drawing text</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawText({
  5. text: 'jCanvas is fun',
  6. fontFamily: 'cursive',
  7. fontSize: 40,
  8. x: 290, y: 150,
  9. fillStyle: 'lightblue',
  10. strokeStyle: 'blue',
  11. strokeWidth: 1
  12. });

Result:

jCanvas example: Drawing text

绘制图片

你可以使用drawImage()方法来导入和处理图片。下面是一个例子:

  1. $myCanvas.drawImage({
  2. source: 'imgs/cat.jpg',
  3. x: 250, y: 100,
  4. fromCenter: false,
  5. shadowColor: '#222',
  6. shadowBlur: 3,
  7. rotate: 40
  8. });

这是上面代码的呈现方式:

HTML:

  1. <h2>jCanvas example: Importing and manipulating an image</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawImage({
  5. source: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/cat.jpg',
  6. x: 250, y: 100,
  7. fromCenter: false,
  8. shadowColor: '#222',
  9. shadowBlur: 3,
  10. rotate: 40
  11. });

Result:

jCanvas example: Importing and manipulating an image

你可以随便的改变上面示例的代码,戳这里:CodePen demo点击预览

Canvas层

如果你曾经使用过,如Photoshop或Gimp图像编辑器类的应用程序,你可能会对图层有所了解,使用图层最爽的地方在于,你可以在画布上控制每个图像。

jCanvas提供了一个功能强大的API,基于你的画布增加了灵活性。

这里介绍了如何使用jCanvas的层。

添加图层

你只能在每一个层上绘制一个对象。在你的jCanvas项目中你有两种添加图层的方式:

  1. 使用 addLayer()方法,其次是drawLayers()方法

  2. 在任何的绘制方法里设置layer属性的值为true

下面是如何运用第一种技术来绘制一个蓝色矩形:

  1. $myCanvas.addLayer({
  2. type: 'rectangle',
  3. fillStyle: 'steelblue',
  4. fromCenter: false,
  5. name: 'blueRectangle',
  6. x: 50, y: 50,
  7. width: 400, height: 200
  8. }).drawLayers();

HTML:

  1. <h2>jCanvas example: Drawing a rectangle with addLayer()</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.addLayer({
  5. type: 'rectangle',
  6. fillStyle: 'steelblue',
  7. fromCenter: false,
  8. name: 'blueRectangle',
  9. x: 50, y: 50,
  10. width: 400, height: 200
  11. }).drawLayers();

Result:

这里是你如何得到同样矩形的第二种方法:

  1. $myCanvas.drawRect({
  2. fillStyle: 'steelblue',
  3. layer: true,
  4. name: 'blueRectangle',
  5. fromCenter: false,
  6. x: 50, y: 50,
  7. width: 400, height: 200
  8. });

HTML:

  1. <h2>jCanvas example: Using drawing method with layer set to "true"</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. $myCanvas.drawRect({
  5. fillStyle: 'steelblue',
  6. layer: true,
  7. name: 'blueRectangle',
  8. fromCenter: false,
  9. x: 50, y: 50,
  10. width: 400, height: 200
  11. });

Result:

正如你所看到的,上面的两种方法,我们得到了相同的结果。

最重要的一点是在上面两个代码样本中可以发现,上面的层你通过name设置的一个名称。这使得他易于参照本层的代码做出各种炫酷的东西,像改变其索引值,动画,删除等等。

让我们看看如何能够做到这一点。

动画层

你可以使用jCanvas的 animateLayer()方法,快速的在你的基础图层上添加动画,此方法接受以下参数:

  1. 该层的 index 或者 name

  2. 具有键值对的动画对象

  3. 以毫秒为单位的动画时长(duration)。这是个默认的参数,如果不设置,默认为400

  4. 动画的运动方式(easing )。这也是一个可选的参数,如果不设置,则默认为摇摆

  5. 动画完成之后的回调函数(callback),也是可选的。

让我们来看一下animateLayer() 方法的效果,我们将在一个层上绘制一个半透明的橙色圆圈,然后设置动画的位置,颜色以及透明度属性:

  1. // Draw circle
  2. $myCanvas.drawArc({
  3. name: 'orangeCircle',
  4. layer: true,
  5. x: 50, y: 50,
  6. radius: 100,
  7. fillStyle: 'orange',
  8. opacity: 0.5
  9. });
  10.  
  11. // Animate the circle layer
  12. $myCanvas.animateLayer('orangeCircle', {
  13. x: 150, y: 150,
  14. radius: 50,
  15. }, 1000, function(layer) { // Callback function
  16. $(this).animateLayer(layer, {
  17. fillStyle: 'darkred',
  18. x: 250, y: 100,
  19. opacity: 1
  20. }, 'slow', 'ease-in-out');
  21. });

看一下下面例子中的动画:

HTML:

  1. <h2>jCanvas example: Animating Layers</h2>
  2.  
  3. <canvas id="myCanvas" width="600" height="300">
  4. <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  5. </canvas>

CSS:

  1. body {
  2. text-align: center;
  3. }
  4.  
  5. canvas {
  6. margin: auto;
  7. display: block;
  8. }

JS:

  1. // Store the canvas object into a variable
  2. var $myCanvas = $('#myCanvas');
  3.  
  4. // Draw circle
  5. $myCanvas.drawArc({
  6. name: 'orangeCircle',
  7. layer: true,
  8. x: 50, y: 50,
  9. radius: 100,
  10. fillStyle: 'orange',
  11. opacity: 0.5
  12. });
  13.  
  14. // Animate the circle layer
  15. $myCanvas.animateLayer('orangeCircle', {
  16. x: 150, y: 150,
  17. radius: 50,
  18. }, 1000, function(layer) { // Callback function
  19. $(this).animateLayer(layer, {
  20. fillStyle: 'darkred',
  21. x: 250, y: 100,
  22. opacity: 1
  23. }, 'slow', 'ease-in-out');
  24. });

Result:

jCanvas example: Animating Layers

可拖动图层

我想提醒你注意的是它还有一个很酷的功能,你可以在可拖动层里设置draggable属性和layer 属性的值为true,就可以将一个普通的jCanvas层变成可拖动的层了。

具体方法如下:

  1. $myCanvas.drawRect({
  2. layer: true,
  3. draggable: true,
  4. bringToFront: true,
  5. name: 'blueSquare',
  6. fillStyle: 'steelblue',
  7. x: 250, y: 150,
  8. width: 100, height: 100,
  9. rotate: 80,
  10. shadowX: -1, shadowY: 8,
  11. shadowBlur: 2,
  12. shadowColor: 'rgba(0, 0, 0, 0.8)'
  13. })
  14. .drawRect({
  15. layer: true,
  16. draggable: true,
  17. bringToFront: true,
  18. name: 'redSquare',
  19. fillStyle: 'red',
  20. x: 190, y: 100,
  21. width: 100, height: 100,
  22. rotate: 130,
  23. shadowX: -2, shadowY: 5,
  24. shadowBlur: 3,
  25. shadowColor: 'rgba(0, 0, 0, 0.5)'
  26. });

在上面的代码段中,通过把属性draggable设置为true,绘制出了两个可拖动的矩形层。此外,请小心使用bringToFront属性,以确保当你拖动层时,他会被自动拖到所有其他现有的图层的前面。

最后,在上述代码段中添加旋转图层的代码并且设置一个盒子阴影,只是为了告诉你如何快速的在你的jCanvas图纸上添加一些特效。

结果会是这样的:

如果你想在在你拖动图层之前,之间或者之后做一些事情的话,jCanvas 可以很容易的利用相关的回调函数来实现这一点:

  1. dragstart:当你开始拖动图层的时候的触发器

  2. drag:当你正在拖动图层时发生

  3. dragstop:当你停止拖动图层时的触发器

  4. dragcancel:当你拖动的图层到了画布表面的边界时发生

比方说,当用户完成拖动层之后,你想在页面上显示一条消息,你可以通过添加一个回调函数dragstop来实现,就像这样:

  1. $myCanvas.drawRect({
  2. layer: true,
  3.  
  4. // Rest of the code as shown above...
  5.  
  6. // Callback function
  7. dragstop: function(layer) {
  8. var layerName = layer.name;
  9. el.innerHTML = 'The ' + layerName + ' layer has been dropped.';
  10. }
  11. })
  12. .drawRect({
  13. layer: true,
  14.  
  15. // Rest of the code...
  16.  
  17. // Callback function
  18. dragstop: function(layer) {
  19. var layerName = layer.name;
  20. el.innerHTML = 'The ' + layerName + ' layer has been dropped.';
  21. }
  22. });

结论

在这篇文章中,我向你介绍了jCanvas,一个新的基于jQuery能与HTML5的 Canvas API一起使用的库。我已经简单的介绍了一些jCanvas的属性和方法,能够让你快速的在画布和是哪个绘制图形,增加视觉效果,动画和拖动图层。

你可以访问jCanvas文档,这里有很多的详细指导和示例。你要可以在 jCanvas网站的 sandbox上进行快速测试。

https://segmentfault.com/a/1190000006615569

简介 jCanvas:当 jQuery遇上HTML5 Canvas的更多相关文章

  1. 当动态桌面遇上 HTML5

    DreamScene2 + HTML5 = 无限可能.时隔一周,DreamScene2 动态桌面经过几个小版本的迭代,修复了一些问题并且功能也得到了增强.欢迎 Star 和 Fork https:// ...

  2. HTML5+Canvas+jQuery调用手机拍照功能实现图片上传(二)

    上一篇仅仅讲到前台操作,这篇专门涉及到Java后台处理.前台通过Ajax提交将Base64编码过的图片数据信息传到Java后台,然后Java这边进行接收处理.通过对图片数据信息进行Base64解码,之 ...

  3. 基于HTML5 Canvas和jQuery 的绘图工具的实现

    简单介绍 HTML5 提供了强大的Canvas元素.使用Canvas并结合Javascript 能够实现一些很强大的功能.本文就介绍一下基于HTML5 Canvas 的绘图工具的实现.废话少说,先看成 ...

  4. html5 canvas画布上合成

    source-over 默认.在目标图像上显示源图像. source-atop 在目标图像顶部显示源图像.源图像位于目标图像之外的部分是不可见的. source-in 在目标图像中显示源图像.只有目标 ...

  5. HTML5<canvas>标签:使用canvas元素在网页上绘制渐变和图像(2)

    详细解释HTML5 Canvas中渐进填充的参数设置与使用,Canvas中透明度的设置与使用,结合渐进填充与透明度支持,实现图像的Mask效果. 一:渐进填充(Gradient Fill) Canva ...

  6. 导出HTML5 Canvas图片并上传服务器功能

    这篇文章主要介绍了导出HTML5 Canvas图片并上传服务器功能,文中通过实例代码给大家介绍了HTML5 Canvas转化成图片后上传服务器,代码简单易懂非常不错,具有一定的参考借鉴价值,需要的朋友 ...

  7. html5 canvas贝塞尔曲线篇(上)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. JQuery文件上传插件JQuery.upload.js的用法简介

    JQuery文件上传插件,这个插件很小,用法很简单,效果却很棒.注意:JQuery版本要求1.8及以上,大家执行如果没效果,则检查JQuery版本,如果是1.8及以上,则该插件源码中的.size()需 ...

  9. 使用 HTML5 Canvas 绘制出惊艳的水滴效果

    HTML5 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ...

随机推荐

  1. [linux] grep awk sort uniq学习

    grep的-A-B-选项详解grep能找出带有关键字的行,但是工作中有时需要找出该行前后的行,下面是解释1. grep -A1 keyword filename找出filename中带有keyword ...

  2. 【zz】面试题之寻找丢失的数字

    据传说是MS/Google等等IT名企业的面试题: 有一组数字,从1到n,中减少了一个数,顺序也被打乱,放在一个n-1的数组里 请找出丢失的数字,最好能有程序,最好算法比较快 BTW1: 有很多种方法 ...

  3. Oracle 第一天

    Oracle 第一天 1.oracle数据库下载.安装和配置 1.1 下载压缩包后解压并将压缩包2里面的文件覆盖至压缩包1中 1.2 按照步骤逐步安装 1.3 设置管理员密码时,默认情况下四个管理员是 ...

  4. VR技术驱动智慧旅游,自由漫步西湖不是梦

         虚拟现实技术是今年最热门的科技之一,不少行业都在嫁接VR功能,其中旅游业也具备这方面的优势.VR+旅游,能让你足不出户即可感受清净唯美的名胜古迹.据591ARVR资讯网www.591arvr ...

  5. web前端~~浏览器兼容问题(百度)

    所谓的浏览器兼容性问题,是指因为不同的浏览器对同一段代码有不同的解析,造成页面显示效果不统一的情况.在大多数情况下,我们的需求是,无论用户用什么浏览器来查看我们的网站或者登陆我们的系统,都应该是统一的 ...

  6. iOS开发中常见问题集锦

    在iOS开发中,会出现各种各样的问题.今天,就把这些常见的问题以及各位大牛的解决方案汇总下,方便以后查阅: 常见错误: 1. linker command failed with exit code ...

  7. 李洪强iOS经典面试题144-数据存储

    李洪强iOS经典面试题144-数据存储   数据存储 sqlite中插入特殊字符的方法和接收到处理方法. 除'其他的都是在特殊字符前面加"/",而 ' -> '' .方法:k ...

  8. 李洪强iOS经典面试题139-Swift

    李洪强iOS经典面试题139-Swift Swift 网上有很多Swift的语法题,但是Swift现在语法还未稳定,所以在这里暂时不贴出语法题,可以自行搜索. Swift和Objective-C的联系 ...

  9. Areas on the Cross-Section Diagram

    Areas on the Cross-Section Diagram  Aizu - ALDS1_3_D Areas on the Cross-Section Diagram 地域の治水対策として.洪 ...

  10. convert与int.parse int

    1,convert :适合将object 转换 int:简单数据转换 int.parse:将string类型转换为int 2,convert:对于空值返回0 不会报异常 int.parse:将会抛出异 ...