Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>clearCanvas</code> is working as intended, the issue here is that you are adding five layers to <code>$canvas</code> on each call to <code>render</code>. So, when <code>drawLayers</code> is called, all the layers added to <code>$canvas</code> are drawn; the five updated ones along with all those layers which were added in previous <code>render</code> calls. </p> <p>One way to solve this issue would be to only alter the layers in the <code>render</code> method with <a href="http://calebevans.me/projects/jcanvas/docs/manipulateLayers/" rel="nofollow"><code>setLayer</code></a>, and add them in a method which is called only once, possibly <code>init</code>. </p> <p>So, <code>init</code> becomes:</p> <pre><code>function init() { $canvas = $('canvas'); ctx = document.getElementById("canvas").getContext("2d"); scaleToWindowDimensions(); console.log($canvas); //Adding layers in init //and defining all the //static properties which //won't change on each render $canvas.addLayer({ name: "l0", //Unique name for access visible: true, method: 'drawArc', strokeStyle: "#000", strokeWidth: 1, fillStyle: '#c33', x: middleX, y: middleY, closed: true, radius: 50, // Event bindings mousedown: function (target) { alert('You pushed RED!'); }, mouseup: function (target) { target.fillStyle = '#c33'; }, mouseover: function (target) { target.fillStyle = "#888"; }, mouseout: function (target) { target.fillStyle = "#c33"; } }) .addLayer({ name: "l1", //Unique name for access visible: true, method: "drawText", strokeStyle: "#000", fromCenter: true, strokeWidth: 1, fillStyle: "#333", fontSize: "18pt", fontFamily: "Verdana", x: middleX, y: middleY, text: "Man", data: { "id": 1, "word": "Man" }, mousedown: function (target) { alert($(this).id); } }) .addLayer({ name: "l2", //Unique name for access visible: true, method: 'drawArc', strokeStyle: "#000", strokeWidth: 1, fillStyle: '#d88', x: 500, y: 100, closed: true, radius: 40, // Event bindings mousedown: function (target) { alert('You pushed RED!'); target.fillStyle = '#333'; }, mouseup: function (target) { target.fillStyle = '#d88'; }, mouseover: function (target) { target.fillStyle = "#888"; }, mouseout: function (target) { target.fillStyle = "#d88"; } }) .addLayer({ name: "l3", //Unique name for access visible: true, method: "drawText", strokeStyle: "#000", fromCenter: true, strokeWidth: 1, fillStyle: "#333", fontSize: "16pt", fontFamily: "Verdana", x: 500, y: 100, text: "Men", data: { "id": 2, "word": "Men" }, mousedown: function (target) { alert($(this).id); } }) .addLayer({ name: "l4", //Unique name for access visible: true, method: 'drawLine', strokeStyle: "#222", strokeWidth: 1, x1: middleX, y1: middleY, x2: 500, y2: 100, radius: 40, }); } </code></pre> <p>And <code>render</code> becomes:</p> <pre><code>function render() { if (!isLoaded) { return; } $canvas.clearCanvas(); // ctx.clearRect(0, 0, $canvas.width(), $canvas.height()); //Here, use setLayer to change //properties which are supposed to change //with render //We use the unique name of each layer //set in init to access them. $canvas.setLayer("l0", { x: middleX, y: middleY, }); $canvas.setLayer("l1", { x: middleX, y: middleY }); //Layer l2 and l3 don't //change anything, so they are //not changed with render. $canvas.setLayer("l4", { x1: middleX, y1: middleY }); $canvas.drawLayers(); } </code></pre> <p><strong><a href="http://jsfiddle.net/m3yNH/" rel="nofollow">Working Example</a></strong>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload