HTML5 lets you draw graphics straight into your web page using the
In this post, I’m going to introduce you to jCanvas, a free and open source jQuery-based library for the HTML5 Canvas API.
If you develop with jQuery, jCanvas makes it easier and quicker to code cool canvas drawings and interactive applications using jQuery syntax.
The jCanvas website explains:
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 enables you to do everything you can do with the native Canvas API and more. If you prefer, you can also use native HTML5 Canvas API methods with jCanvas. The draw() method serves just this purpose. Furthermore, you can easily extend jCanvas with your own methods and properties using its extend() feature.
To include jCanvas in your project, download the script from the official website or the GitHub page, then include it in your project folder. As mentioned, jCanvas needs jQuery to work, so be sure to include that too.
Your project’s script files will look something like this:
<scriptjs/jcanvas.min.js><span><span><span></script</span>></span> </span><scriptmyCanvas" width<span>="600"</span> height<span>="300"</span>> <span><span><span><p</span>></span>This is fallback content </span> for users of assistive technologies or of browsers that don't have full support for the Canvas API.<span><span><span></p</span>></span> </span><span><span><span></canvas</span>></span></span>
Here are a few points of interest about the code snippet above.
If you were to use the native Canvas API, your JavaScript would look something like this:
<scriptjs/jcanvas.min.js><span><span><span></script</span>></span> </span><scriptmyCanvas" width<span>="600"</span> height<span>="300"</span>> <span><span><span><p</span>></span>This is fallback content </span> for users of assistive technologies or of browsers that don't have full support for the Canvas API.<span><span><span></p</span>></span> </span><span><span><span></canvas</span>></span></span>
The snippet above caches the canvas object into a variable called $myCanvas. The properties inside the drawRect() method are mostly self-explanatory, but here’s a brief rundown:
Here is a demo with the rectangle shape:
See the Pen jCanvas example: Rectangle by SitePoint (@SitePoint) on CodePen.
Arcs are portions of the rim of a circle. With jCanvas, drawing arcs is just a matter of setting the desired values for a few properties inside the drawArc() method:
<script src="js/jquery.min.js><span><span><span></script</span>></span> </span><script src="js/jcanvas.min.js><span><span><span></script</span>></span> </span><script src="js/script.js><span><span><span></script</span>></span></span>
Drawing arcs involves setting a value for the radius property as well as the start and end angles in degrees. If you’d like the direction of your arc to be counterclockwise, add the ccw property to the code above and set it to true.
Here’s a CodePen demo of the above code:
See the Pen jCanvas example: Arc by SitePoint (@SitePoint) on CodePen.
Drawing a circle is as easy as omitting both start and end properties from the drawArc() method.
For instance, here’s how you can draw a simple graphic of a smiling face using only arc shapes:
<span><span><span><canvas</span> id<span>="myCanvas"</span> width<span>="600"</span> height<span>="300"</span>></span> </span> <span><span><span><p</span>></span>This is fallback content </span> for users of assistive technologies or of browsers that don't have full support for the Canvas API.<span><span><span></p</span>></span> </span><span><span><span></canvas</span>></span></span>
Remember that jCanvas is based on the jQuery library, therefore you can chain jCanvas methods the same way you can chain jQuery methods.
Here’s how the code above renders in the browser:
See the Pen jCanvas example: Smiling Face by SitePoint (@SitePoint) on CodePen.
You can quickly draw lines with jCanvas using the drawLine() method and plotting a series of points to which your lines are going to connect.
<span>var canvas = document.getElementById('myCanvas'), </span> context <span>= canvas.getContext('2d');</span>
The code above sets the rounded and closed properties to true, thereby rounding the corners of the line and closing the traced path.
See the Pen jCanvas example: Connected lines by SitePoint (@SitePoint) on CodePen.
You can also draw flexible paths using the drawPath() method. Think of a path as one or more connected lines, arcs, curves, or vectors.
The drawPath() method accepts a map of points and a map of x and y coordinates for the sub-paths inside each point. You also need to specify the type of path you’re going to draw, e.g., line, arc, etc.
Here’s how you can draw a pair of connected horizontally and vertically pointing arrows using drawPath() and draw arrows(), the latter of which is a handy jCanvas method that enables you to quickly draw an arrow shape on the canvas:
<span>// Store the canvas object into a variable </span><span>var $myCanvas = $('#myCanvas'); </span> <span>// rectangle shape </span>$myCanvas<span>.drawRect({ </span> <span>fillStyle: 'steelblue', </span> <span>strokeStyle: 'blue', </span> <span>strokeWidth: 4, </span> <span>x: 150, y: 100, </span> <span>fromCenter: false, </span> <span>width: 200, </span> <span>height: 100 </span><span>});</span>
The x and y coordinates of each sub-path are relative to the x and y coordinates of the entire path.
And here’s the result:
See the Pen jCanvas example: Connected Arrows by SitePoint (@SitePoint) on CodePen.
You can quickly draw text on the canvas with the aptly named drawText() method. The main properties you need for this to work are:
Here’s the sample code:
<script src="js/jquery.min.js><span><span><span></script</span>></span> </span><script src="js/jcanvas.min.js><span><span><span></script</span>></span> </span><script src="js/script.js><span><span><span></script</span>></span></span>
And this is what it looks like inside the
See the Pen jCanvas example: Drawing text by SitePoint (@SitePoint) on CodePen.
You can import and manipulate images using the drawImage() method. Here’s an example:
<span><span><span><canvas</span> id<span>="myCanvas"</span> width<span>="600"</span> height<span>="300"</span>></span> </span> <span><span><span><p</span>></span>This is fallback content </span> for users of assistive technologies or of browsers that don't have full support for the Canvas API.<span><span><span></p</span>></span> </span><span><span><span></canvas</span>></span></span>
And this is how the code above renders:
See the Pen jCanvas example: Importing and manipulating an image by SitePoint (@SitePoint) on CodePen.
You can view and fiddle around with all the examples above combined into a single CodePen demo here.
If you’ve ever used an image editor application like Photoshop or Gimp, you’re already familiar with layers. The cool part of working with layers is that you gain the ability to manipulate each image on your canvas individually, by drawing it on its own layer.
jCanvas offers a powerful layer API that adds flexibility to your canvas-based designs.
Here’s an overview of how to use jCanvas layers.
You can only draw one object on each layer. You can add layers to your jCanvas project in either of two ways:
Here’s how you apply the first technique to draw a blue rectangle.
<span>var canvas = document.getElementById('myCanvas'), </span> context <span>= canvas.getContext('2d');</span>
See the Pen PZeNGp by SitePoint (@SitePoint) on CodePen.
And here’s how you apply the second method to draw the same rectangle:
<span>// Store the canvas object into a variable </span><span>var $myCanvas = $('#myCanvas'); </span> <span>// rectangle shape </span>$myCanvas<span>.drawRect({ </span> <span>fillStyle: 'steelblue', </span> <span>strokeStyle: 'blue', </span> <span>strokeWidth: 4, </span> <span>x: 150, y: 100, </span> <span>fromCenter: false, </span> <span>width: 200, </span> <span>height: 100 </span><span>});</span>
See the Pen zrjqKb by SitePoint (@SitePoint) on CodePen.
As you can see, with each of the above, we get the same result.
The important point to notice in both code samples above is that the layer has a name that you set via the name property. This makes it easy to refer to this layer in code and do all sorts of cool things with it, like changing its index value, animating it, removing it, etc.
Let’s see how we can do this.
You can quickly add animations to your layer-based drawings with jCanvas using the animateLayer() method. This method accepts the following parameters:
Let’s see the animateLayer() method in action. We’ll draw a semi-transparent orange circle on a layer, then animate its position, color, and opacity properties:
<script src="js/jquery.min.js><span><span><span></script</span>></span> </span><script src="js/jcanvas.min.js><span><span><span></script</span>></span> </span><script src="js/script.js><span><span><span></script</span>></span></span>
Check out the demo below to see the animation:
See the Pen jCanvas example: Animating Layers by SitePoint (@SitePoint) on CodePen.
You can view all the three previous examples combined in this CodePen demo.
One more cool feature I’d like to draw your attention to is the ability to turn a regular jCanvas layer into a draggable layer by simply setting the draggable property of the layer to true.
Here’s how:
<span><span><span><canvas</span> id<span>="myCanvas"</span> width<span>="600"</span> height<span>="300"</span>></span> </span> <span><span><span><p</span>></span>This is fallback content </span> for users of assistive technologies or of browsers that don't have full support for the Canvas API.<span><span><span></p</span>></span> </span><span><span><span></canvas</span>></span></span>
The snippet above draws two draggable rectangle layers by incorporating: draggable: true. Also, note the use of the bringToFront property, which ensures that when you drag a layer, it automatically gets pushed to the front of all other existing layers.
Finally, the code rotates the layers and sets a box shadow, just to show how you can quickly add these effects to your jCanvas drawings.
The result looks like this:
If you’d like your app to do something before, during, or after moving a draggable layer, jCanvas makes it easy to accomplish this by supporting callback functions during the relevant events:
Let’s say you’d like to display a message on the page after the user finishes dragging a layer. You can reuse the code snippet above by adding a callback function to the dragstop event, like so:
<script src="js/jquery.min.js><span><span><span></script</span>></span> </span><script src="js/jcanvas.min.js><span><span><span></script</span>></span> </span><script src="js/script.js><span><span><span></script</span>></span></span>
After dragging each square, you’ll see a message on the page telling you which color square you just dropped. Try it out in the demo below:
See the Pen Draggable jCanvas Layers with Callback Function by SitePoint (@SitePoint) on CodePen.
In this post I’ve introduced you to jCanvas, a new jQuery-based library that works with the HTML5 Canvas API. I’ve illustrated some of jCanvas methods and properties that quickly enable you to draw on the canvas surface, add visual effects, animations, and draggable layers.
There’s so much more that you can do with jCanvas. You can visit the jCanvas documentation for detailed guidance and examples, which you can quickly test in the sandbox on the jCanvas website.
To accompany this article, I’ve put together a basic painting application on CodePen using the jCanvas drawLine() method:
See the Pen jCanvas Painting App by SitePoint (@SitePoint) on CodePen.
Feel free to make it better by adding more features and sharing your results with the SitePoint community.
jCanvas is a powerful JavaScript library that combines the capabilities of jQuery and HTML5 Canvas to simplify the process of creating complex graphics on a web page. It leverages the simplicity and versatility of jQuery, a fast, small, and feature-rich JavaScript library, and the graphical power of HTML5 Canvas, which is an HTML element used to draw graphics on a web page. jCanvas provides a simple and consistent API for drawing shapes, creating animations, handling events, and much more.
To get started with jCanvas, you first need to include the jQuery library and the jCanvas library in your HTML file. You can download these libraries from their respective websites or use a Content Delivery Network (CDN). Once you have included these libraries, you can start using jCanvas functions to draw on the canvas.
jCanvas provides several functions to draw shapes on the canvas. For example, you can use the drawRect() function to draw a rectangle, the drawArc() function to draw an arc, and the drawPolygon() function to draw a polygon. Each of these functions accepts a properties object that specifies the characteristics of the shape, such as its position, size, color, and so on.
Yes, jCanvas provides a powerful animation feature that allows you to animate shapes on the canvas. You can use the animate() function to animate the properties of a shape over a specified duration. This function uses the jQuery animation engine, so it supports all the easing functions provided by jQuery.
jCanvas provides several functions to handle events on the canvas. For example, you can use the click() function to handle click events, the mouseover() function to handle mouseover events, and the mousedown() function to handle mousedown events. Each of these functions accepts a callback function that is called when the event occurs.
Yes, jCanvas is designed to simplify the process of creating complex graphics on a web page. It provides a wide range of functions to draw shapes, create animations, handle events, and much more. With jCanvas, you can create anything from simple shapes to complex animations and interactive graphics.
jCanvas is compatible with all modern browsers that support the HTML5 Canvas element. This includes the latest versions of Google Chrome, Mozilla Firefox, Apple Safari, and Microsoft Edge. However, it does not support Internet Explorer 8 or earlier, as these browsers do not support the HTML5 Canvas element.
If you encounter issues with jCanvas, you can check the console for error messages, as jCanvas provides detailed error messages to help you troubleshoot issues. You can also refer to the jCanvas documentation, which provides comprehensive information about the library and its functions.
Yes, jCanvas is released under the MIT License, which means you can use it in both personal and commercial projects for free. However, you must include the original copyright notice and disclaimer in any copy of the software or its documentation.
You can find more resources about jCanvas on its official website, which provides a comprehensive documentation, examples, and tutorials. You can also find useful information on web development websites and forums, such as Stack Overflow and SitePoint.
The above is the detailed content of Introduction to jCanvas: jQuery Meets HTML5 Canvas. For more information, please follow other related articles on the PHP Chinese website!