-2
var mybuttons  = document.getElementById('buttons').addEventListener('click', function(){
var myContext = context.clearRect(0,0, canvas.width, canvas.height);
}, false);

This is the code I have used to clear the canvas the button is created but it doesn't clear the canvas

j08691
  • 190,436
  • 28
  • 232
  • 252
Kun Silva
  • 29
  • 5
  • 2
    We might not have enough information to determine why your canvas is not being cleared. Can you try to provide your HTML, or at least enough of it that allows someone to easily reproduce the exact issue you are seeing? – Zack Oct 18 '16 at 21:02
  • 1
    It looks like you are using the exact same clear logic as [this accepted answer to a related quesiton](http://stackoverflow.com/a/2142549/1804496), so there is probably something wrong with part of your code that we haven't seen yet. – Zack Oct 18 '16 at 21:04

1 Answers1

0

Your code is incomplete, so we can't tell exactly where you are going wrong here. It might just be a variable name mismatch somewhere causing the event listener to not be attached. Regardless, here is a working example of using a button to clear a canvas element to hopefully steer you in the right direction.

var canvas = document.getElementById('canvasA');
var context = canvas.getContext("2d");
context.moveTo(25, 25);
context.lineTo(275, 275);
context.stroke();
context.moveTo(25, 275);
context.lineTo(275, 25);
context.stroke();

var mybuttons = document.getElementById('buttons');
mybuttons.addEventListener('click', function() {
  var canvas = document.getElementById('canvasA');
  var context = canvas.getContext("2d");
  context.clearRect(0, 0, canvas.width, canvas.height);
}, false);
<canvas id="canvasA" height="300px" width="300px" style="border:1px solid black"></canvas>
<br />
<input type="button" id="buttons" value="Clear" />
Zack
  • 2,619
  • 30
  • 58