jCanvas includes a jCanvas object as part of the jQuery object.
$.jCanvasThe jCanvas object contains the following properties/methods:
While the jCanvas object stores properties and methods, it is itself a function. For more, see the Preferences section.
All methods are regular jQuery methods, and are used in the same manner.
You can call a jCanvas method on any selected canvas object, in which you would usually pass in an object of properties.
$("canvas").drawArc({ fillStyle: "black", x: 100, y: 100, radius: 25 });jCanvas keeps a set of preferences, which act as defaults for any property not specified.
These are descriptions of every jCanvas property, their supported methods, and their possible values. Default values are listed first in bold.
This is a reference of every jCanvas method, for quick access.
The $.jCanvas() method sets properties of the jCanvas prefs object, so jCanvas methods called in the future can use those properties as defaults.
$.jCanvas({ fillStyle: "green", x: 50, y: 50, radius: 20 }); $("canvas").drawArc()If nothing is passed, all properties will reset to their original values.
$.jCanvas()Any properties included when calling a jCanvas method override their respective defaults.
In the past, jCanvas has deprecated methods in favor of a different method name.
To use these methods with the same name, call the retrofit() method of the jCanvas object.
$.jCanvas.retrofit();If nothing is passed, the method returns the canvas's 2D context.
$("canvas").loadCanvas() $("canvas").loadCanvas("2d")All methods automatically call load(), so there's no need to call it before using those methods.
This method clears all, or any section of the canvas.
If nothing is passed, the entire canvas is cleared.
$("canvas").clearCanvas()Clearing a section works in the same way as drawing a rectangle, with the rectangle being drawn from its center (by default).
$("canvas") .drawEllipse({ fillStyle: "#000", x: 50, y: 50, width: 200, height: 100, }) .clearCanvas({ x: 50, y: 50, width: 50, height: 50 })The saveCanvas() method saves the current state of the canvas.
$("canvas").saveCanvas()To revert to the canvas's state before saving, use the restoreCanvas() method.
The restoreCanvas() method restores the current state of the canvas.
$("canvas").restoreCanvas()The translateCanvas() method translates the canvas from the included point.
$("canvas").translateCanvas({ x: 100, y: 100 }) .drawRect({ fillStyle: "#000", x: 100, y: 100, width: 100, height: 50 }) .restoreCanvas();To revert to the canvas's state before translating, use the restoreCanvas() method.
The rotateCanvas() method rotates the canvas from the included point.
The x and y properties include the center of rotation.
$("canvas").rotateCanvas({ angle: 45, x: 100, y: 100 }) .drawRect({ fillStyle: "#000", x: 100, y: 100, width: 100, height: 50 }) .restoreCanvas();To revert to the canvas's state before rotating, use the restoreCanvas() method.
The scaleCanvas() method scales the canvas from the included point.
$("canvas").scaleCanvas({ x: 100, y: 100, width: 1.5, height: 1.5 }) .drawArc({ fillStyle: "#000", x: 100, y: 100, radius: 50 }) .restoreCanvas();To revert to the canvas's state before scaling, use the restoreCanvas() method.
The width and height values are multiples of the canvas's current width/height (i.e. the original size is 1).
Angles are measured in degrees by default
The start and end values default to a full circle.
Zero degrees is measured from the top of the arc
You can draw an ellipse using the same basic properties used to draw a rectangle.
$("canvas").drawEllipse({ fillStyle: "#000", x: 150, y: 100, width: 200, height: 100, fromCenter: false })You can round the corners of a line by including the rounded property.
$("canvas").drawLine({ strokeStyle: "#000", strokeWidth: 10, rounded: true, x1: 50, y1: 50, x2: 100, y2: 150, x3: 200, y3: 100, x4: 150, y4: 200 })Every quadratic curve consists of a start point, a control point (to make the curve), and an end point (which becomes the next start point)
$("canvas").drawQuad({ strokeStyle: "#000", strokeWidth: 5, x1: 50, y1: 50, cx1: 200, cy1: 50, x2: 200, y2: 200 })Every Bézier curve consists of a start point, two control points (to make the curve), and an end point (which becomes the next start point)
$("canvas").drawBezier({ strokeStyle: "#000", strokeWidth: 5, x1: 50, y1: 50, cx1: 200, cy1: 50, cx2: 50, cy2: 150, x2: 200, y2: 150, cx3: 300, cy3: 150, cx4: 150, cy4: 1, x3: 350, y3: 50 })If the width or height is left unincluded, that value is calculated.
A callback function may also be run
$("canvas").drawImage({ source: "images/image.jpg", x: 150, y: 150 })Missing dimensions are calculated proportionally based on the image's original dimensions
The drawPolygon() method creates a regular (equal-angled) polygon.
$("canvas").drawPolygon({ fillStyle: "black", x: 200, y: 100, radius: 50, sides: 3, }) $("canvas").drawPolygon({ fillStyle: "#69a", x: 100, y: 100, radius: 50, sides: 5, angle: 25 })To create concave polygons (polygons that point inward), include the projection property.
A value greater than 0 will cause the sides to projection outward. A value less than 0 will cause the sides to projection inward. A value equal to 0 does not project any sides.
$("canvas").drawPolygon({ fillStyle: "#36b", x: 100, y: 100, radius: 50, sides: 5, projection: -0.5 }) $("canvas").drawPolygon({ fillStyle: "#3b6", x: 100, y: 100, radius: 50, sides: 50, projection: 0.1 })If the projection property is equal to -1, the polygon will be unable to be seen. Any other numeric value works, though.
To create a shape which masks other objects, use the mask property.
$("canvas").drawArc({ fillStyle: "#000", x: 150, y: 150, radius: 50, mask: true }) .drawRect({ fillStyle: "#ef2929", x: 100, y: 120, width: 100, height: 100 }) .restoreCanvas();To prevent masking of further shapes, call the restoreCanvas() method.
Radial gradients are created when r1 or r2 is included.
var radial = $("canvas").gradient({ x1: 50, y1: 50, x2: 50, y2: 50, r1: 10, r2: 30, c1: "#729fcf", c2: "#3465a4" }); $("canvas").drawArc({ fillStyle: radial, x: 50, y: 50, radius: 30 })Color stops are optional, and are measured from 0 to 1.
Calling the pattern() method returns a canvas pattern object, which can be used as a fill or stroke style for any drawing.
function draw(patt) { $("canvas").drawEllipse({ fillStyle: patt, x: 200, y: 100, width: 300, height: 100 }) } var patt = $("canvas").pattern({ source: "images/pattern.jpg", repeat: "repeat", // draw ellipse when pattern loads load: draw })By default, the pattern repeats on the x and y axis
The draw() method is used for quick drawing on the canvas manually (usually using native canvas methods).
The draw() method accepts a function, and the function accepts the canvas context as a parameter.
$("canvas").draw(function(ctx) { ctx.beginPath(); ctx.fillStyle = "#000"; ctx.rect(50, 50, 100, 100); ctx.fill(); ctx.closePath(); })The keyword this in the callback function refers to the selected canvas DOM element.
The setPixels() method allows for modification of a included section of pixels from the canvas.
To modify the pixels, you can loop through each pixel by includeing the each method.
The each callback function accepts 4 color values as parameters (red, green, blue, alpha). It must return an array containing the new values.
function invert() { $("canvas").setPixels({ x: 150, y: 100, width: 100, height: 75, // loop through each pixel each: function(r, g, b, a) { r = 255 - r; g = 255 - g; b = 255 - b; return [r, g, b, a]; } }) } $("canvas").drawImage({ source: "images/image.jpg", x: 150, y: 100, load: invert })If the x, y, width, and height properties are not specified, the method loops through the entire canvas's pixels.
Extending jCanvas is as easy as creating a jQuery plugin, as well as a few extra steps to give your plugin full integration with jCanvas.
If you've never created a jQuery plugin, read this first.Below is a model of how most jCanvas methods are structured:
// jCanvas method $.fn.drawShape = function(args) { // Merge arguments with preferences var params = $.extend({}, $.jCanvas.prefs, args); // Loop through selected elements this.each(function(i, item) { var ctx = $(item).loadCanvas(); // Set the canvas's style properties $.jCanvas.setGlobals(ctx, params); // Your code here }); return this; };These variables are necessary to integrate your plugin with jCanvas:
You may use these jCanvas methods to integrate with existing properties:
You can set defaults for your plugin the same way you set jCanvas preferences, except you pass true as a second argument.
$.jCanvas({ myProp: true }, true);It's good practice to wrap your jQuery (and jCanvas) plugins in a closure. This makes your code easier to write and more efficient.
(function($, jC) { // Your plugin here }(jQuery, jQuery.jCanvas));Now, you can reference jQuery as $ and jQuery.jCanvas as jC in your plugin.
jCanvas allows you to create, maintain, and manipulate layers for any canvas. Each drawing (shape, text, etc.) is its own layer.
To create a new layer, use the jCanvas.addLayer() method, which returns an object formed from merging your arguments with the jCanvas preferences:
To tell jCanvas which method to use, include the fn property.
var myLayer = $.jCanvas.addLayer({ fn: "drawRect", fillStyle: "#37b", x: 100, y: 100, width: 80, height: 40 })The jCanvas object stores an array containing every jCanvas layer. Anything in the array can later be drawn on a canvas.
$.jCanvas.layers // returns [myLayer]To put this to use, you can draw these layers on any canvas with jCanvas.drawLayers().
$("canvas").drawLayers() // draws the layers from first to lastSo because we created a rectangle object (and therefore, is in the queue), drawLayers() will draw a rectangle on the canvas.s
Here is a complete example of how the jCanvas queue is used:
// Create a rectangle object var myLayer = $.jCanvas.addLayer({ fn: "drawRect", fillStyle: "#37b", x: 100, y: 100, width: 80, height: 40 }); // Draw anything that's in the queue, which in this case, is a rectangle $("canvas").drawLayers();Whenever you change a property of myLayer and call drawLayers(), the property changes will be reflected when drawing.
Passing in true as an argument for drawLayers() will clear the canvas before drawing
Normally, you can animate the properties of an object with the animate() method. Animating drawings on a canvas works (mostly) the same way.
// Create the object of properties var myObj = { fillStyle: "#006", x: 50, y: 50, radius: 50 } // Function to draw circle function draw() { $("canvas") // Canvas must be cleared for every frame .clearCanvas() .drawArc(myObj) } // Draw initial circle draw(); // Animate object properties $(myObj) .delay(250) .animate({ x: 200, y: 150 }, { // Draw every frame duration: 500, step: draw })The step property of the second object accepts a callback function, which is run for every frame of the animation. For more information, see the jQuery documentation.