Following Code works if I remove this part:
checkboxPencil.onchange = function() {
if(checkboxPencil.checked) {
If it works I can draw on my canvas, if not I can't.
Since I want to do something onchange I Need to wrap my function with the onchange Event and check if a Checkbox is checked.
But I do not understand why this does not work with the 2 lines above. I get no error in Chrome developer console.
Again my Code works if I remove the 2 lines above, but it does not work if I add them and I do not understand why.
Full Code:
if(window.addEventListener) {
window.addEventListener('load', function () {
...
// Initialization sequence.
function init () {
canvas = document.getElementById('imageView');
tool = new tool_pencil();
canvas.addEventListener('mousedown', ev_canvas, false);
canvas.addEventListener('mousemove', ev_canvas, false);
canvas.addEventListener('mouseup', ev_canvas, false);
}
function tool_pencil() {
var tool = this;
this.started = false;
checkboxPencil.onchange = function() {
if(checkboxPencil.checked) {
console.log("Pencil checked");
this.mousedown = function (ev) {
context.beginPath();
context.moveTo(ev._x, ev._y);
tool.started = true;
};
this.mousemove = function (ev) {
if (tool.started) {
context.lineTo(ev._x, ev._y);
context.stroke();
}
};
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
}
};
} else {
console.log("Pencil not checked");
}
}
}