I am using jCanvas to build a HTML5 app, I need to remove a layer, which is a black circle on the canvas, you can see the code here.
var cvLingualWidth = 945;
var cvLingualHeight = 100;
var cvLingual = document.getElementById("cvLingual");
function drawGrid() {
var contextLingual = cvLingual.getContext("2d");
for (var y = 0.5; y < cvLingualHeight; y += 6) {
contextLingual.moveTo(0, y);
contextLingual.lineTo(cvLingualWidth, y);
}
contextLingual.strokeStyle = "#DDD";
contextLingual.stroke();
}
function drawCircle() {
$("#cvLingual").drawArc({
layer: true,
name: "circleLayer_18",
strokeStyle: "#000",
strokeWidth: 2,
x: 42,
y: 70,
radius: 8
});
}
function clearCircle() {
var circleLayer = $("#cvLingual").getLayer("circleLayer_18");
// TODO
}
$(function () {
drawGrid();
drawCircle();
$("#clear").click(function(){
clearCircle();
});
})
I tried removeLayer() but its not working. If I clear the canvas, the entire UI will be gone.
How can I clear the circle without affecting the background gridlines?
According to the
removeLayer()
documentation, theremoveLayer()
method does not update the canvas automatically. You will need to do this afterwards using thedrawLayers()
method.However, the
drawLayers()
method works by clearing the canvas and redrawing all jCanvas layers, which means that your gridlines will disappear. To fix this, use jCanvas'sdrawLine()
method to draw your gridlines, like so:As a side note, if you are planning on drawing the circle again later (after removing it), I recommend setting the layer's
visible
property tofalse
temporarily. Then, when you are ready to show the circle again, set itsvisible
property totrue
. Note that you will need to calldrawLayers()
to update the canvas in both instances.Hide the circle:
Show the circle again:
Finally, for your convenience, I have forked your fiddle and have implemented the above suggestions.