I'm using jCanvas to paint on a HTML canvas
element. First I initialize a x * x table, with each cell holding a canvas:
function init(size) {
// Adding the board table to the body
$("#content").append("<table class=\"board\">");
for(i=0; i<size; i++) {
$(".board").append("<tr>")
for(j=0; j<size ; j++) {
$(".board tr:last-child").append("<td class=\"square\">");
}
}
// Setting size to match width or height, whichever i smallest
var h = $("#content").height();
var w = $("#content").width();
var rez = (w > h) ? h : w;
var cSize = rez / size;
$(".board td").append("<canvas width="+cSize+" height="+cSize+" />");
$(".board").css("width",rez);
$(".board").css("height",rez);
// Drawing icons on the board
var c = $("canvas");
var icns = new Array(11);
for(i=0; i<11; i++)
icns[i] = "svg/"+ params["hvor"] +"/"+ (i+1) +".svg";
for(i=0; i<c.length; i++) {
var rand = Math.floor(Math.random()*(icns.length-1));
var icn = icns[rand];
icns.splice($.inArray(icns[rand], icns), 1);
$(c[i]).drawImage({
source: icn,
height: cSize,
fromCenter: false
});
}
}
This works fine, and the images gets drawn on the canvas. I then try to add a layer with a new shape (it has to be removed later, that's why I'm using a layer).
$('td').click(function(){
var h = $(this).height() -2;
var o = h / 3.5;
var t = h / 10;
$(this).children("canvas").addLayer({
method: "drawLine",
strokeStyle: "#5cfe15",
strokeWidth: t,
x1: o, y1: o*2,
x2: h/2, y2: h-o,
x3: o*3, y3: o-t
});
});
That's when I get the error Uncaught TypeError: Object [object Object] has no method 'addLayer'
. if i just use drawLine()
in stead of addLayer()
it actually draws the line, so the object is clearly a canvas.
What am I missing here?
Make sure you're using jCanvas v5.0, since it introduced the addLayer() method for jQuery collections (that is, $.fn).
-Caleb