I have canvas. I have paint tools pencil and eraser. How i can erase drawings without overwrite(overdraw) with white color.
this my code eraser over drawing with white color: http://jsfiddle.net/66z12xb0/
I have in back end save image after drawing.
<?php
$images = scandir(ROOT_FS . FINISH_DRAW_PATH, 1);
$imageData = $GLOBALS['HTTP_RAW_POST_DATA'];
$filteredData = substr($imageData, strpos($imageData, ",") + 1);
$unencodedData = base64_decode($filteredData);
$fileName = "photo.png";
$fp = fopen(ROOT_FS . SAVE_DRAW_PATH . $fileName, 'wb');
fwrite($fp, $unencodedData);
fclose($fp);
?>
Open with windows photo viewer and see this result:
additional upload foto:
$("#upload_foto").click(function() {
var data = canvas.toDataURL('image/png');
var ajax = new XMLHttpRequest();
ajax.open('POST', 'backend.php', false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(data);
});
<button type='button' id='upload_foto'>Upload</button>
Your idea of using compositing to create an eraser is a good idea.
destination-out
will remove existing drawings where a new drawing overlaps those existing drawings.If you want to erase the line than you have two options:
ctx.clearRect()
or overdraw with a colour that matches the Background,If you need to have a Background image, so that it is not possible to erase with a single color, layers come in handy as a general solution. If you decide to use layers I recommend to use a framework like paper.js or kineticJS, they have this functionality already built in. If you decide to implement layers on your own you could create another
<canvas>
above the one for your background, or, you need to store drawing information in a list and redraw the whole canvas each time.